-
Notifications
You must be signed in to change notification settings - Fork 42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Faiza #22
base: master
Are you sure you want to change the base?
Faiza #22
Changes from all commits
fe82474
79f022a
4ed8789
33ecd0f
f6bb9b0
28195f8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,65 @@ | ||
require_relative './stack.rb' | ||
|
||
def balanced(string) | ||
raise NotImplementedError, "Not implemented yet" | ||
if string == "" | ||
return true | ||
end | ||
|
||
parens = { | ||
"(" => ")", | ||
"[" => "]", | ||
"{" => "}", | ||
} | ||
|
||
stack = Stack.new | ||
string.each_char do |element| | ||
if parens[element] | ||
stack.push(parens[element]) | ||
else | ||
return false if element != stack.pop | ||
end | ||
end | ||
|
||
return stack.empty? | ||
|
||
# cannot do this either | ||
# if stack.empty? | ||
# return | ||
# end | ||
|
||
#undefined method `length' | ||
# if stack.length == 0 | ||
# return true | ||
# end | ||
|
||
end | ||
|
||
# open_paren = [] | ||
# close_parens = [] | ||
# PUSH PARENS INTO ARRAYS | ||
# string.each_char do |element| | ||
# if element == "(" || element == "{" || element == "[" | ||
# open.push(element); | ||
# else | ||
# close.push(element); | ||
# end | ||
# end | ||
# if open.length != close.length | ||
# return false | ||
# end | ||
# is_balanced = false | ||
# for index in (0...open.length) | ||
# if open[index] == "(" && close[(close.length - 1) - index] == ")" || | ||
# open[index] == "{" && close[(close.length - 1) - index] == "}" || | ||
# open[index] == "[" && close[(close.length - 1) - index] == "]" | ||
# is_balanced = true | ||
# else | ||
# return is_balanced | ||
# end | ||
# end | ||
# return is_balanced | ||
|
||
|
||
def evaluate_postfix(postfix_expression) | ||
raise NotImplementedError, "Not implemented yet" | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,76 @@ | ||
QUEUE_SIZE = 20 | ||
|
||
class Queue | ||
|
||
def initialize | ||
# @store = ... | ||
raise NotImplementedError, "Not yet implemented" | ||
@store = Array.new(QUEUE_SIZE) | ||
@front = @rear = -1 | ||
end | ||
|
||
# using circular buffer | ||
def enqueue(element) | ||
raise NotImplementedError, "Not yet implemented" | ||
if @front == -1 # Q is empty | ||
@rear = 1 | ||
@front = 0 | ||
@store[@front] = element | ||
|
||
elsif @front == @rear | ||
raise Error, "Queue is full." | ||
|
||
else # not empty | ||
# if we reach the end of the array, rear will once again be pointed to 0 | ||
new_rear = (@rear + 1) % QUEUE_SIZE | ||
@store[@rear] = element | ||
@rear = new_rear | ||
end | ||
end | ||
|
||
def dequeue | ||
raise NotImplementedError, "Not yet implemented" | ||
if @front == -1 # Q is empty | ||
return | ||
|
||
elsif @front == @rear # There's only 1 element | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When front and rear are equal, the Queue is not empty, it's full. When they're equal after removing an element, then the queue is empty. |
||
temp_node = @store[@front] # "remove" the element | ||
@front = -1 # points to the empty bucket | ||
@rear = -1 # points to the empty bucket | ||
return temp_node | ||
|
||
else # there's more than 1 element in the array | ||
temp_node = @store[@front] # "remove" the element | ||
@store[@front] = nil # store is now empty | ||
@front = (@front + 1) % QUEUE_SIZE # wrap front around array | ||
return temp_node | ||
end | ||
end | ||
|
||
def front | ||
raise NotImplementedError, "Not yet implemented" | ||
return @store[@front] | ||
end | ||
|
||
def size | ||
raise NotImplementedError, "Not yet implemented" | ||
if @front > @rear | ||
return QUEUE_SIZE - @front + @rear | ||
else | ||
return @rear - @front | ||
end | ||
end | ||
|
||
def empty? | ||
raise NotImplementedError, "Not yet implemented" | ||
if @store[@front].nil? | ||
return true | ||
else | ||
return false | ||
end | ||
end | ||
|
||
def to_s | ||
return @store.to_s | ||
array = [] | ||
@store.each_with_index do |element, i| | ||
next if i < @front | ||
break if element.nil? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if you've removed 1 element from the front of the queue. Then |
||
|
||
array << element | ||
end | ||
return array.to_s | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,29 @@ | ||
require_relative './linked_list.rb' | ||
|
||
class Stack | ||
def initialize | ||
# @store = ... | ||
raise NotImplementedError, "Not yet implemented" | ||
@store = LinkedList.new | ||
@top = nil | ||
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" | ||
if @store.empty? | ||
return true | ||
else | ||
return false | ||
end | ||
end | ||
|
||
def to_s | ||
return @store.to_s | ||
end | ||
end | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nicely done