Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
71 changes: 69 additions & 2 deletions lib/problems.rb
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 = {

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!

"(" => ")",
"[" => "]",
"{" => "}"
}

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)

Choose a reason for hiding this comment

The 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
31 changes: 22 additions & 9 deletions lib/queue.rb
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

Choose a reason for hiding this comment

The 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

Choose a reason for hiding this comment

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

Since the back can wrap around the array this should be @back = (@back + 1) % QUEUE_SIZE

end
end

def dequeue
raise NotImplementedError, "Not yet implemented"
if self.empty?
print "empty"
end
removed = @ari[@front]
@front += 1

Choose a reason for hiding this comment

The 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: @front = (@front + 1) % QUEUE_SIZE

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
32 changes: 25 additions & 7 deletions lib/stack.rb
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)

Choose a reason for hiding this comment

The 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
5 changes: 0 additions & 5 deletions test/problems_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,27 @@
describe "Test wave 3 problems" do
describe "balanced" do
it "Given balanced strings it should return true" do
skip
expect(balanced('(({}))')).must_equal true
end

it "regards an empty string as balanced" do
skip
expect(balanced('')).must_equal true
end

it "will return false for an unbalanced set of parens" do
skip
expect(balanced('(()')).must_equal false
expect(balanced('(()}')).must_equal false
expect(balanced('([]]')).must_equal false
end

it "also works for {} and []" do
skip
expect(balanced('[]')).must_equal true
expect(balanced('{}')).must_equal true
end
end

describe "postfix" do
it "can add a 2 numbers together" do
skip
expect(evaluate_postfix("34+")).must_equal 7
expect(evaluate_postfix("34*")).must_equal 12
expect(evaluate_postfix("34-")).must_equal -1
Expand Down
23 changes: 8 additions & 15 deletions test/queue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,44 +6,39 @@

describe "Test Queue Implementation" do
it "creates a Queue" do
q = Queue.new
q = Queue.new(5)
q.class.must_equal Queue
end

it "adds something to an empty Queue" do
skip
q = Queue.new
q = Queue.new(5)
q.enqueue(10)
q.to_s.must_equal "[10]"
end

it "adds multiple somethings to a Queue" do
skip
q = Queue.new
q = Queue.new(5)
q.enqueue(10)
q.enqueue(20)
q.enqueue(30)
q.to_s.must_equal "[10, 20, 30]"
end

it "starts the size of a Queue at 0" do
skip
q = Queue.new
q = Queue.new(5)
q.empty?.must_equal true
end

it "removes something from the Queue" do
skip
q = Queue.new
q = Queue.new(5)
q.enqueue(5)
removed = q.dequeue
removed.must_equal 5
q.empty?.must_equal true
end

it "removes the right something (LIFO)" do
skip
q = Queue.new
q = Queue.new(5)
q.enqueue(5)
q.enqueue(3)
q.enqueue(7)
Expand All @@ -53,8 +48,7 @@
end

it "properly adjusts the size with enqueueing and dequeueing" do
skip
q = Queue.new
q = Queue.new(5)
q.empty?.must_equal true
q.enqueue(-1)
q.enqueue(-60)
Expand All @@ -65,8 +59,7 @@
end

it "returns the front element in the Queue" do
skip
q = Queue.new
q = Queue.new(5)
q.enqueue(40)
q.enqueue(22)
q.enqueue(3)
Expand Down
26 changes: 15 additions & 11 deletions test/stack_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Choose a reason for hiding this comment

The 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)
Expand Down