Skip to content

Julia Kingrey #28

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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

In this exercise we will implement both a stack & a queue, and then use them in a variety of hands-on exercises.

Due:
**Due: Monday March 2nd**

## Learning Goals

Expand Down
26 changes: 23 additions & 3 deletions lib/problems.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
require_relative './stack.rb'

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(2n) or just plain O(n) where n is length of the array because we are looping over the array twice: once when we split from string to array and a second time when we create the hash map.
# Space Complexity: constant because the number of variables stays (more or less) the same no matter how long our string/array is
def balanced(string)
raise NotImplementedError, "Not implemented yet"
if string.length == 0
return true
else
array = string.split("")
end

hash = {}
array.each do |character|
if hash[character].nil?
hash[character] = 1
else
hash[character] += 1
end
end

if hash['('] == hash[')'] && hash['{'] == hash['}'] && hash['['] == hash[']']
return true
else
return false
end
Comment on lines +21 to +25

Choose a reason for hiding this comment

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

This doesn't quite work as it only counts the number of the characters, not if they're matched.


end

# Time Complexity: ?
Expand Down
63 changes: 56 additions & 7 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,28 +1,77 @@
class Queue

def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"

# # assume fixed array length is 10
@store = Array.new(10)

# initialize front and back both at the same place
@front = -1
@back = -1

# default status is empty
@is_empty = true
end

def enqueue(element)

Choose a reason for hiding this comment

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

👍

raise NotImplementedError, "Not yet implemented"
# implementing circular buffer to keep track of array's front and back indices (so insertions/deletions will be O of 1 -- would be O of n with if we used shovel/push)

# if queue has space, move @back to the next free position, i.e. next position clockwise

@back = (@back + 1) % size

@store[@back] = element

# resize queue if full: in this implementation, queue resizes at every multiple of 10 and reorders with front at index 0
if @front == @back && !@is_empty
# add the new spaces after the 'back' of the queue
new_queue_a = @store[@front+1..-1]
new_queue_b = @store[0..@back-1]
new_queue_c = Array.new(10)
@back = @store.length - 1
@store = new_queue_a + new_queue_b + new_queue_c
@store[@back] = element
@front = -1

end

# if queue is empty, change to not empty
if @is_empty
@is_empty = false
end

end

def dequeue

Choose a reason for hiding this comment

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

👍

raise NotImplementedError, "Not yet implemented"
# increase at the front
# check that the queue is empty when you reach the end

@front = (@front + 1) % size

# update is_empty for empty queue
if @front == @back
@is_empty = true
end

removed = @store[@front]

# initially used delete_at(@front) but reassigning value to nil preserves fixed array size
@store[@front] = nil

# return the element that was removed
return removed
end

def front
raise NotImplementedError, "Not yet implemented"
@front

Choose a reason for hiding this comment

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

Suggested change
@front
return @store[@front]

end

def size
raise NotImplementedError, "Not yet implemented"
return @store.length

Choose a reason for hiding this comment

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

This gets you the size of the internal array, not the amount of elements stored in the queue.

end

def empty?
raise NotImplementedError, "Not yet implemented"
return @is_empty
end

def to_s

Choose a reason for hiding this comment

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

Needs to be updated.

Expand Down
12 changes: 7 additions & 5 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
require "linked_list"

class Stack
def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = LinkedList.new
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"
return @store.empty?
end

def to_s
Expand Down
9 changes: 2 additions & 7 deletions test/problems_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,25 @@
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

it "also works if the string has opens and closes in the beginning and end" do
skip
expect(balanced('[]()')).must_equal true
end
end
Expand All @@ -40,7 +35,7 @@
skip
expect(evaluate_postfix("34+")).must_equal 7
expect(evaluate_postfix("34*")).must_equal 12
expect(evaluate_postfix("34-")).must_equal -1
expect(evaluate_postfix("34-")).must_equal (-1)
expect(evaluate_postfix("34/")).must_equal 0
end

Expand All @@ -49,7 +44,7 @@
expect(evaluate_postfix("34+2*")).must_equal 14
expect(evaluate_postfix("34*2/")).must_equal 6
expect(evaluate_postfix("34-1+")).must_equal 0
expect(evaluate_postfix("34/7-")).must_equal -7
expect(evaluate_postfix("34/7-")).must_equal (-7)
expect(evaluate_postfix("35+6*")).must_equal 48
expect(evaluate_postfix("62/5+")).must_equal 8
end
Expand Down
54 changes: 29 additions & 25 deletions test/queue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,76 +11,72 @@
end

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

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

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

it "a Queue is empty after removing all the elements" do
skip
q = Queue.new
q.enqueue(5)
q.enqueue(6)
expect( expect(q.dequeue) ).must_equal 5
expect( expect(q.dequeue) ).must_equal 6
# changed text syntax so I could better understand why code wasn't passing
expect( (q.dequeue) ).must_equal 5
expect( (q.dequeue) ).must_equal 6
expect(q.empty?).must_equal true
end

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

it "removes the right something (LIFO)" do
skip
q = Queue.new
q.enqueue(5)
q.enqueue(3)
q.enqueue(7)
expect(q.to_s).must_equal "[5, 3, 7, nil, nil, nil, nil, nil, nil, nil]"
removed = q.dequeue
removed.must_equal 5
q.to_s.must_equal "[3, 7]"
_(removed).must_equal 5
_(q.to_s).must_equal "[nil, 3, 7, nil, nil, nil, nil, nil, nil, nil]"
end

it "properly adjusts the size with enqueueing and dequeueing" do
skip
q = Queue.new
q.empty?.must_equal true
_(q.empty?).must_equal true
q.enqueue(-1)
q.enqueue(-60)
q.empty?.must_equal false
_(q.empty?).must_equal false
q.dequeue
q.dequeue
q.empty?.must_equal true
_(q.empty?).must_equal true
end

it "returns the front element in the Queue" do
skip
q = Queue.new
q.enqueue(40)
q.enqueue(22)
q.enqueue(3)
q.dequeue
#
expect(q.to_s).must_equal "[40, 22, 3, nil, nil, nil, nil, nil, nil, nil]"
expect(q.dequeue).must_equal 40
expect(q.dequeue).must_equal 22
end
it "works for a large Queue" do
Expand All @@ -90,27 +86,35 @@
q.enqueue(30)
expect(q.dequeue).must_equal 10
expect(q.dequeue).must_equal 20
expect(q.to_s).must_equal("[nil, nil, 30, nil, nil, nil, nil, nil, nil, nil]")
q.enqueue(40)
expect(q.to_s).must_equal("[nil, nil, 30, 40, nil, nil, nil, nil, nil, nil]")
q.enqueue(50)
q.enqueue(60)
q.enqueue(70)
q.enqueue(80)
q.enqueue(90)
q.enqueue(100)
expect(q.to_s).must_equal("[nil, nil, 30, 40, 50, 60, 70, 80, 90, 100]")
q.enqueue(110)
# circular buffer in action: front index points to 30, back index points to 110
expect(q.to_s).must_equal("[110, nil, 30, 40, 50, 60, 70, 80, 90, 100]")
q.enqueue(120)
# resize in action: queue length has hit 10, so another 10 spots are added at back of the array and queue is resorted with @front at index 0
expect(q.to_s).must_equal("[30, 40, 50, 60, 70, 80, 90, 100, 110, 120, nil, nil, nil, nil, nil, nil, nil, nil, nil]")
q.enqueue(130)
# after resizing, next element is added at the back
expect(q.to_s).must_equal("[30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, nil, nil, nil, nil, nil, nil, nil, nil]")
q.enqueue(140)
q.enqueue(150)
expect(q.to_s).must_equal("[30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, nil, nil, nil, nil, nil, nil, nil]")
q.enqueue(150)
q.enqueue(160)
q.enqueue(170)
q.enqueue(180)
q.enqueue(190)
q.enqueue(200)
q.enqueue(210)
q.dequeue

expect(q.to_s).must_equal('[30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200]')
expect(q.dequeue).must_equal 30
expect(q.to_s).must_equal("[nil, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210]")
end
end
21 changes: 8 additions & 13 deletions test/stack_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,48 +6,43 @@
describe "Test Stack Implementation" do
it "creates a Stack" do
s = Stack.new
s.class.must_equal Stack
_(s.class).must_equal Stack
end

it "pushes something onto a empty Stack" do
skip
s = Stack.new
s.push(10)
s.to_s.must_equal "[10]"
_(s.to_s).must_equal "[10]"
end

it "pushes multiple somethings onto a Stack" do
skip
s = Stack.new
s.push(10)
s.push(20)
s.push(30)
s.to_s.must_equal "[10, 20, 30]"
_(s.to_s).must_equal "[10, 20, 30]"
end

it "starts the stack empty" do
skip
s = Stack.new
s.empty?.must_equal true
_(s.empty?).must_equal true
end

it "removes something from the stack" do
skip
s = Stack.new
s.push(5)
removed = s.pop
removed.must_equal 5
s.empty?.must_equal true
_(removed).must_equal 5
_(s.empty?).must_equal true
end

it "removes the right something (LIFO)" do
skip
s = Stack.new
s.push(5)
s.push(3)
s.push(7)
removed = s.pop
removed.must_equal 7
s.to_s.must_equal "[5, 3]"
_(removed).must_equal 7
_(s.to_s).must_equal "[5, 3]"
end
end