Skip to content

Branches - Kelsey #25

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 5 commits 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
42 changes: 39 additions & 3 deletions lib/problems.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,45 @@
require_relative './stack.rb'

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(n)
def balanced(string)
Comment on lines +3 to 5

Choose a reason for hiding this comment

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

👍

raise NotImplementedError, "Not implemented yet"


# paren pairs are at the same indexes
open_parens = [ '(', '[', '{' ]
close_parens = [')', ']', '}' ]

# check if string length isn't even
if string.length % 2 != 0
return false
# check if string is empty
elsif string.length == 0
return true
else
# check if string starts with a close paren or ends with an open paren (unbalanced)
if (close_parens.include? string[0]) || (open_parens.include? string[-1])
return false
else
stack = Stack.new
end
end

string.each_char do |x|
if open_parens.include? x
stack.push(x)
# if x is a close paren, remove last open paren in stack and check to ensure it's a match for the close paren x.
elsif close_parens.include? x
last_in_stack = stack.pop()
if last_in_stack != open_parens[close_parens.index(x)]
return false
end
end
end
if stack.empty?
return true
else
return false
end
Comment on lines +38 to +42

Choose a reason for hiding this comment

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

Suggested change
if stack.empty?
return true
else
return false
end
return stack.empty?

end

# Time Complexity: ?
Expand Down
56 changes: 42 additions & 14 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,59 @@
class Queue

def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = Array.new(30)
@front = @back = -1
end

def enqueue(element)
raise NotImplementedError, "Not yet implemented"
if @front == -1 && @back == -1
@front = 0
@back = 0
end

if !@store[@back].nil? && @front == @back
raise StandardError.new('queue is full.')
end

@store[@back] = element
@back = (@back+1) % @store.length

end

def dequeue
raise NotImplementedError, "Not yet implemented"
dequeued = @store[@front]

# remove dequeued item from queue
@store[@front] = nil
# move front to next item in queue
@front = @front+1

Choose a reason for hiding this comment

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

You need to make sure the front will wrap around the queue

Suggested change
@front = @front+1
@front = (@front+1) % @store.length

You also need to check to see if the queue is empty!


return dequeued
end

def front
raise NotImplementedError, "Not yet implemented"
return @store[@front]
end

def size
raise NotImplementedError, "Not yet implemented"
return @store.compact.length
end
Comment on lines 38 to 40

Choose a reason for hiding this comment

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

This works, but it's not very efficient.


def empty?
raise NotImplementedError, "Not yet implemented"
if self.size == 0
return true
else
return false
end
end


# helper method for tests
def length
return @store.length
end

def to_s
@store.compact!

Choose a reason for hiding this comment

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

This compacts the buffer which removes any extra space you have in the buffer, which you might need in the Queue!

Better to do something like this:

    return "[]" if @front == -1
    current = @front
    output = "["
    next = (@front + 1) % @store.length
    while next != @back
      output += "#{@store[current]},"
      current = (current + 1) % @store.length
      next = (next + 1) % @store.length
    end

    output += "#{@store[current]}]"
    return output

return @store.to_s
end
end
20 changes: 11 additions & 9 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
require_relative './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"
# last in, first out
@store.remove_last
end

def empty?
raise NotImplementedError, "Not yet implemented"
@store.empty?
end

def to_s
return @store.to_s
end
Expand Down
25 changes: 14 additions & 11 deletions test/problems_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,34 +7,37 @@
describe "Test wave 3 problems" do
describe "balanced" do
it "Given balanced strings it should return true" do
skip
balanced('({[()]})')
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

# added test
it "also works if the string has unbalanced opens and closes in the beginning and end" do
expect(balanced('()((')).must_equal false
expect(balanced(']][]')).must_equal false
expect(balanced(')(][')).must_equal false
end
end

describe "postfix" do
it "can add a 2 numbers together" do
skip
Expand All @@ -43,7 +46,7 @@
expect(evaluate_postfix("34-")).must_equal -1
expect(evaluate_postfix("34/")).must_equal 0
end

it "can add a evaluate a more complicated expression" do
skip
expect(evaluate_postfix("34+2*")).must_equal 14
Expand Down
47 changes: 31 additions & 16 deletions test/queue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,40 +9,51 @@
q = Queue.new
q.class.must_equal Queue
end

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

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


# added test
it "returns an error if queue is full" do
q = Queue.new
q.length.times do
q.enqueue(10)
end
expect{q.enqueue(20)}.must_raise StandardError
end


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

# added test to ensure .empty? method works
q.enqueue(10)
q.empty?.must_equal false
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
end


# isn't this FIFO?
it "removes the right something (LIFO)" do
skip
q = Queue.new
q.enqueue(5)
q.enqueue(3)
Expand All @@ -51,9 +62,8 @@
removed.must_equal 5
q.to_s.must_equal "[3, 7]"
end

it "properly adjusts the size with enqueueing and dequeueing" do
skip
q = Queue.new
q.empty?.must_equal true
q.enqueue(-1)
Expand All @@ -63,16 +73,16 @@
q.dequeue
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.dequeue).must_equal 22
end

it "works for a large Queue" do
q = Queue.new
q.enqueue(10)
Expand All @@ -92,15 +102,20 @@
q.enqueue(130)
q.enqueue(140)
q.enqueue(150)
q.enqueue(150)
# removed duplicate to pass equal assertion
# q.enqueue(150)
q.enqueue(160)
q.enqueue(170)
q.enqueue(180)
q.enqueue(190)
q.enqueue(200)
q.enqueue(210)
# added 3 elements to pass equal assertion
q.enqueue(220)
q.enqueue(230)
q.enqueue(240)
q.dequeue

expect(q.to_s).must_equal('[40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240]')
end
end
20 changes: 10 additions & 10 deletions test/stack_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,40 +8,40 @@
s = Stack.new
s.class.must_equal Stack
end

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

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

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

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

it "removes the right something (LIFO)" do
skip
# skip
s = Stack.new
s.push(5)
s.push(3)
Expand Down