-
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
Lynn Griffin - Edge : passed all test; created helper function for postfix_expression logic #20
base: master
Are you sure you want to change the base?
Changes from all commits
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,76 @@ | ||
require_relative './stack.rb' | ||
require 'pry' | ||
|
||
def balanced(string) | ||
raise NotImplementedError, "Not implemented yet" | ||
if string == '' | ||
return true | ||
end | ||
if string.length % 2 != 0 | ||
return false | ||
end | ||
|
||
s = Stack.new(string.length) | ||
validator = { | ||
"(" => ")", | ||
"[" => "]", | ||
"{" => "}" | ||
} | ||
|
||
i = 0 | ||
while i < string.length | ||
char = string[i] | ||
if validator.key?(char) | ||
s.push(char) | ||
else | ||
if validator[s.peek] == char | ||
s.pop | ||
else | ||
false | ||
end | ||
end | ||
i += 1 | ||
end | ||
|
||
if !(s.empty?) | ||
return false | ||
else | ||
return true | ||
end | ||
end | ||
|
||
|
||
|
||
def evaluate_postfix(postfix_expression) | ||
raise NotImplementedError, "Not implemented yet" | ||
i = 0 | ||
num_stack = Stack.new(postfix_expression.length) | ||
|
||
while i < postfix_expression.length | ||
char = postfix_expression[i] | ||
if char == "*" || char == "+" || char == "-" || char == "/" | ||
s_num = num_stack.pop | ||
f_num = num_stack.pop | ||
new_num = arithmetic(f_num, s_num, char) | ||
num_stack.push(new_num) | ||
else | ||
num_stack.push(char.to_i) | ||
end | ||
i += 1 | ||
end | ||
|
||
if num_stack.size == 1 | ||
return num_stack.peek | ||
end | ||
end | ||
|
||
def arithmetic(first_num, second_num, exp) | ||
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. Nice helper method! |
||
case exp | ||
when "*" | ||
return (first_num.to_i * second_num.to_i) | ||
when "+" | ||
return (first_num.to_i + second_num.to_i) | ||
when "-" | ||
return (first_num.to_i - second_num.to_i) | ||
when "/" | ||
return (first_num.to_i / second_num.to_i) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,44 @@ | ||
class Queue | ||
attr_writer :buffer_size, :ari | ||
|
||
def initialize | ||
# @store = ... | ||
raise NotImplementedError, "Not yet implemented" | ||
def initialize(buffer_size) | ||
@ari_size = buffer_size + 1 | ||
@ari = Array.new(@ari_size) | ||
@front = 0 | ||
@back = 0 | ||
end | ||
|
||
def enqueue(element) | ||
raise NotImplementedError, "Not yet implemented" | ||
if @back == @front - 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. You need a little bit more math here, because the back can wrap around the array. This isn't quite right. |
||
print "full" | ||
else | ||
@ari[@back] = element | ||
@back += 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. Since the back can wrap around the array this should be |
||
end | ||
end | ||
|
||
def dequeue | ||
raise NotImplementedError, "Not yet implemented" | ||
if self.empty? | ||
print "empty" | ||
end | ||
removed = @ari[@front] | ||
@front += 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. Like the above you have to handle the situation where front goes past the end of the array: Also you should check to see if the queue is now empty. |
||
return removed | ||
end | ||
|
||
def front | ||
raise NotImplementedError, "Not yet implemented" | ||
|
||
end | ||
|
||
def size | ||
raise NotImplementedError, "Not yet implemented" | ||
return @back - @front | ||
end | ||
|
||
def empty? | ||
raise NotImplementedError, "Not yet implemented" | ||
return @front == @back | ||
end | ||
|
||
def to_s | ||
return @store.to_s | ||
return @ari[@front...@back].to_s | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,40 @@ | ||
class Stack | ||
def initialize | ||
# @store = ... | ||
raise NotImplementedError, "Not yet implemented" | ||
attr_accessor :store, :size | ||
|
||
def initialize(length) | ||
@store = Array.new(length) | ||
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. The stack was supposed to use a linked list. |
||
@size = 0 | ||
end | ||
|
||
def push(element) | ||
raise NotImplementedError, "Not yet implemented" | ||
if @size > @store.length - 1 | ||
return "Stack Overflow" | ||
else | ||
@store[@size] = element | ||
@size += 1 | ||
end | ||
return @store.compact | ||
end | ||
|
||
def pop | ||
raise NotImplementedError, "Not yet implemented" | ||
if self.empty? | ||
return "empty" | ||
end | ||
removed = @store[@size - 1] | ||
@store = @store[0...-1] | ||
@size -= 1 | ||
return removed | ||
end | ||
|
||
def empty? | ||
raise NotImplementedError, "Not yet implemented" | ||
return @size == 0 ? true : false | ||
end | ||
|
||
def to_s | ||
return @store.to_s | ||
return @store.compact.to_s | ||
end | ||
|
||
def peek | ||
return @store[@size - 1] | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,44 +5,48 @@ | |
|
||
describe "Test Stack Implementation" do | ||
it "creates a Stack" do | ||
s = Stack.new | ||
s = Stack.new(3) | ||
s.class.must_equal Stack | ||
end | ||
|
||
it "pushes something onto a empty Stack" do | ||
skip | ||
s = Stack.new | ||
s = Stack.new(3) | ||
s.push(10) | ||
s.to_s.must_equal "[10]" | ||
end | ||
|
||
it "pushes multiple somethings onto a Stack" do | ||
skip | ||
s = Stack.new | ||
s = Stack.new(3) | ||
s.push(10) | ||
s.push(20) | ||
s.push(30) | ||
s.to_s.must_equal "[10, 20, 30]" | ||
end | ||
|
||
it "raises Stack Overlow ;)"do | ||
s = Stack.new(3) | ||
s.push(1) | ||
s.push(2) | ||
s.push(3) | ||
s.to_s.must_equal "[1, 2, 3]" | ||
s.push(4).must_equal "Stack Overflow" #change this | ||
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. Maybe raise an exception in this circumstance. |
||
end | ||
|
||
it "starts the stack empty" do | ||
skip | ||
s = Stack.new | ||
s = Stack.new(3) | ||
s.empty?.must_equal true | ||
end | ||
|
||
it "removes something from the stack" do | ||
skip | ||
s = Stack.new | ||
s = Stack.new(3) | ||
s.push(5) | ||
removed = s.pop | ||
removed.must_equal 5 | ||
s.empty?.must_equal true | ||
end | ||
|
||
it "removes the right something (LIFO)" do | ||
skip | ||
s = Stack.new | ||
s = Stack.new(3) | ||
s.push(5) | ||
s.push(3) | ||
s.push(7) | ||
|
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!