-
Notifications
You must be signed in to change notification settings - Fork 49
Branches - Erika #33
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
base: master
Are you sure you want to change the base?
Branches - Erika #33
Changes from all commits
680a87d
dc0b47c
6822cab
3f7cbb6
c4ab31c
84aaf6a
c42dc87
de47dc4
b8da87e
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,13 +1,45 @@ | ||
|
||
require_relative './stack.rb' | ||
require 'pry' | ||
|
||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
def balanced(string) | ||
raise NotImplementedError, "Not implemented yet" | ||
|
||
stack = Stack.new | ||
index = 0 | ||
sets = { | ||
")" => "(", | ||
"}" => "{", | ||
"]" => "[" | ||
} | ||
|
||
while index <= string.length - 1 | ||
|
||
char = string[index] | ||
|
||
if sets.values.include?(char) | ||
stack.push(char) | ||
elsif sets.keys.include?(char) | ||
return false if stack.empty? | ||
last_in = stack.pop | ||
return false if last_in != sets[char] | ||
end | ||
index += 1 | ||
|
||
end | ||
|
||
return stack.empty? | ||
|
||
end | ||
|
||
|
||
|
||
|
||
|
||
|
||
# Time Complexity: ? | ||
# Space Complexity: ? | ||
def evaluate_postfix(postfix_expression) | ||
raise NotImplementedError, "Not implemented yet" | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,85 @@ | ||
class Queue | ||
|
||
def initialize | ||
# @store = ... | ||
raise NotImplementedError, "Not yet implemented" | ||
@store = Array.new(50) | ||
@front = @rear = -1 | ||
end | ||
|
||
def enqueue(element) | ||
raise NotImplementedError, "Not yet implemented" | ||
|
||
if (@rear + 1) % @store.length == @front | ||
return ArgumentError.new("Queue is full") | ||
end | ||
|
||
if @front == -1 && @rear == -1 | ||
@front = @rear = 0 | ||
@store[@front] = element | ||
else | ||
@rear = (@rear + 1) % @store.length | ||
@store[@rear] = element | ||
end | ||
|
||
end | ||
|
||
def dequeue | ||
raise NotImplementedError, "Not yet implemented" | ||
def dequeue | ||
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. 👍 |
||
|
||
if @front == -1 && @rear == -1 | ||
return ArgumentError.new("Queue is empty") | ||
end | ||
|
||
element = @store[@front] | ||
|
||
if @front == @rear | ||
@store[@front] = nil | ||
@front = @rear = -1 | ||
else | ||
@store[@front] = nil | ||
@front = (@front + 1) % @store.length | ||
end | ||
|
||
return element | ||
|
||
end | ||
|
||
def front | ||
raise NotImplementedError, "Not yet implemented" | ||
return @store[@front] | ||
end | ||
|
||
def size | ||
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. 👍 |
||
raise NotImplementedError, "Not yet implemented" | ||
|
||
if @front < @back | ||
return @back - @front | ||
else | ||
return @back - 1 + @store.length - (@front + 1) | ||
end | ||
|
||
end | ||
|
||
def empty? | ||
raise NotImplementedError, "Not yet implemented" | ||
return @front == @rear | ||
end | ||
Comment on lines
58
to
60
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. I think this should actually be:
|
||
|
||
|
||
def to_s | ||
return @store.to_s | ||
|
||
store_arr = [] | ||
temp = @front | ||
|
||
while temp <= @store.length - 1 && !@store[temp].nil? | ||
store_arr << @store[temp] | ||
temp += 1 | ||
end | ||
|
||
if @store[temp].nil? && @rear < @front | ||
temp = 0 | ||
while temp <= @rear | ||
store_arr << @store[temp] | ||
temp += 1 | ||
end | ||
end | ||
|
||
return store_arr.to_s | ||
end | ||
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.
👍 , Time & space complexity?