-
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
Heather #14
base: master
Are you sure you want to change the base?
Heather #14
Changes from all commits
76f1f4e
d96328f
0b24fff
ce0b2e9
aa33604
1d0ab36
5d26fd9
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,54 @@ | ||
require_relative './stack.rb' | ||
require_relative "./stack.rb" | ||
|
||
def balanced(string) | ||
raise NotImplementedError, "Not implemented yet" | ||
return true if string == "" | ||
|
||
parens_hash = { | ||
"(" => ")", | ||
"[" => "]", | ||
"{" => "}", | ||
} | ||
|
||
stack_parens = Stack.new | ||
string.each_char do |paren| | ||
if parens_hash[paren] | ||
stack_parens.push(parens_hash[paren]) | ||
else | ||
return false if paren != stack_parens.pop | ||
end | ||
end | ||
|
||
return stack_parens.empty? | ||
end | ||
|
||
def evaluate_postfix(postfix_expression) | ||
raise NotImplementedError, "Not implemented yet" | ||
end | ||
operand_hash = { | ||
"+" => true, | ||
"-" => true, | ||
"*" => true, | ||
"/" => true, | ||
} | ||
|
||
stack_postfix = Stack.new | ||
postfix_expression.each_char do |char| | ||
if !operand_hash[char] | ||
stack_postfix.push(char) | ||
else | ||
digit2 = stack_postfix.pop.to_i | ||
digit1 = stack_postfix.pop.to_i | ||
|
||
case char | ||
when "+" | ||
value = digit1 + digit2 | ||
when "-" | ||
value = digit1 - digit2 | ||
when "*" | ||
value = digit1 * digit2 | ||
when "/" | ||
value = digit1 / digit2 | ||
end | ||
stack_postfix.push(value) | ||
end | ||
end | ||
return stack_postfix.pop | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,63 @@ | ||
class Queue | ||
QUEUE_SIZE = 20 | ||
|
||
def initialize | ||
# @store = ... | ||
raise NotImplementedError, "Not yet implemented" | ||
@store = Array.new(QUEUE_SIZE) | ||
@front = @rear = -1 #-1 because it is not index in array, bc front and rear float around we dont know there will be any particular value | ||
#use -1 to indicate the queue is empty | ||
end | ||
|
||
def enqueue(element) | ||
raise NotImplementedError, "Not yet implemented" | ||
if @front == -1 # queue is empty | ||
@front = 0 | ||
@rear = 0 | ||
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. Starting front and rear at the same number could cause problems if someone tries to dequeue a few times. |
||
@store[@rear] = element | ||
elsif @front == (@rear + 1) % @store.length # queue is full | ||
raise Error, "Q full" | ||
else # queue not empty | ||
@rear += 1 | ||
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. This needs to be |
||
@store[@rear] = element | ||
end | ||
end | ||
|
||
def dequeue | ||
raise NotImplementedError, "Not yet implemented" | ||
if @front == -1 | ||
raise Error, "Q is empty" | ||
elsif @front == @rear | ||
removed_value = @store[@front] | ||
@store[@front] = nil | ||
@front = @rear = -1 | ||
return removed_value | ||
else | ||
new_front = (@front + 1) % @store.length | ||
removed_value1 = @store[@front] | ||
@store[@front] = nil | ||
@front = new_front | ||
return removed_value1 | ||
end | ||
end | ||
|
||
def front | ||
raise NotImplementedError, "Not yet implemented" | ||
if @front == -1 | ||
raise Error, "Q is empty" | ||
else | ||
return @store[@front] | ||
end | ||
end | ||
|
||
def size | ||
raise NotImplementedError, "Not yet implemented" | ||
return @store[@front..@rear].length | ||
end | ||
|
||
def empty? | ||
raise NotImplementedError, "Not yet implemented" | ||
if @front == -1 | ||
return true | ||
else | ||
return false | ||
end | ||
end | ||
|
||
def to_s | ||
return @store.to_s | ||
return @store[@front..@rear].to_s | ||
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. 👍 |
||
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.
Good use of a hash!