Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions lib/problems.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,38 @@
require_relative './stack.rb'

def balanced(string)
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError, "Not implemented yet"
pairs = { ")" => "(", "]" => "[", "}" => "{" }
stack = Stack.new

string.each_char do |char|
if pairs.values.include?(char)
stack.push(char)
else
return false if stack.empty? or pairs[char] != stack.pop()
end
end
return stack.empty?
end

def evaluate_postfix(postfix_expression)
raise NotImplementedError, "Not implemented yet"
end
stack = Stack.new
postfix_expression.split("").each do |char|
if char == char.to_i.to_s
stack.push(char.to_i)
else
num1 = stack.pop()
num2 = stack.pop()
case(char)
when "+"
stack.push(num2 + num1)
when "-"
stack.push(num2 - num1)
when "/"
stack.push(num2 / num1)
when "*"
stack.push(num2 * num1)
end
end
end
return stack.pop()
end
40 changes: 32 additions & 8 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,55 @@
class Queue
QUEUE = 20

def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = Array.new(QUEUE)
@front = @rear = -1
end

def enqueue(element)
raise NotImplementedError, "Not yet implemented"
if @front == -1
@rear = 1
@front = 0
@store[@front] = element
elsif @front == @rear
raise Error, "Queue Full"
else
new_rear = (@rear + 1) % QUEUE
@store[@rear] = element
@rear = new_rear
end
end

def dequeue
raise NotImplementedError, "Not yet implemented"
raise Error, "Queue Empty" if @front == -1
value = @store[@front]
@store[@front] = nil
@front = (@front + 1) % QUEUE
@front = @rear = -1 if @front == @rear
return value
end

def front
raise NotImplementedError, "Not yet implemented"
rreturn @store[@head]
end

def size
raise NotImplementedError, "Not yet implemented"
if (@head == @tail && !empty?)
return @capacity
elsif @head == @tail
return 0
elsif @tail > @head
return @tail - @head
elsif @tail < @head
return @capacity - (@head - @tail)
end
Comment on lines +37 to +45
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well done!

end

def empty?
raise NotImplementedError, "Not yet implemented"
return @rear == -1
end

def to_s
return @store.to_s
return @store[@front...@rear].to_s
end
end
9 changes: 4 additions & 5 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
class Stack
def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = LinkedList.new()
end

def push(element)
raise NotImplementedError, "Not yet implemented"
@store.add_last(element)
end

def pop
raise NotImplementedError, "Not yet implemented"
@store.remove_last()
end

def empty?
raise NotImplementedError, "Not yet implemented"
return @store.empty?()
end

def to_s
Expand Down