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

Branches - Diana Han #26

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
21 changes: 20 additions & 1 deletion lib/problems.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,26 @@
# Time Complexity: ?
# Space Complexity: ?
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"
return true if string.length == 0

return false if string.length % 2 == 1

open_parens = ["[","{","("]
closed_parens = ["]","}",")"]

stack = Stack.new

string.each_char do |element|
if open_parens.include?(element)
stack.push(element)
elsif closed_parens.include?(element)
data = stack.pop
if open_parens.find_index(data) != closed_parens.find_index(element)
return false
end
end
end
return true
end

# Time Complexity: ?
Expand Down
48 changes: 39 additions & 9 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,61 @@
class Queue

def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = Array.new(50)
@front = @back = -1
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"
if @front == -1
@front = 0
@back = 1
@store[@front] = element
elsif @front == @back
raise ArgumentError
else
@store[@back] = element
@back = (@back + 1 ) % @store.length
end
end

def dequeue
raise NotImplementedError, "Not yet implemented"
def dequeue #remove and return from front

Choose a reason for hiding this comment

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

👍

if @front == -1
raise ArgumentError
else
element = @store[@front]
@front = (@front + 1) % @store.length
end

if @front == @back
@front = @back = -1
end

return element
end

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

def size
raise NotImplementedError, "Not yet implemented"
@back - @front

Choose a reason for hiding this comment

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

This works when front < back. What about when back has wrapped around the buffer.

end

def empty?
raise NotImplementedError, "Not yet implemented"
if @front == @back
return true
end
Comment on lines +45 to +47

Choose a reason for hiding this comment

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

Suggested change
if @front == @back
return true
end
return @front == @back

end

def to_s

Choose a reason for hiding this comment

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

👍 Well done

return @store.to_s
# @store.delete(nil)
# return @store.to_s
list = []
current = @front
while current != @back
list << @store[current]
current = (current + 1) % @store.length
end
return list.to_s
end
end
45 changes: 40 additions & 5 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,57 @@
class Stack
def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = Array.new
end

def push(element)
raise NotImplementedError, "Not yet implemented"
@store << element
end

def pop
raise NotImplementedError, "Not yet implemented"
top_element = @store[-1]
@store.delete_at(-1)

return top_element
end

def empty?
raise NotImplementedError, "Not yet implemented"
if @store.length == 0
return true
else
return false
end
end

def to_s
return @store.to_s
end
def length
return @store.length
end
end

# USING LINKED LIST
# class Stack
# def initialize
# @store = LinkedList.new
# end

# def push(element)
# @store.add_front(element)
# end

# def pop
# return nil if self.empty?

# element = store.remove_front
# return element
# end

# def empty?
# return @store.empty?
# end

# def to_s
# return @store.to_s
# end
# end
8 changes: 2 additions & 6 deletions test/problems_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,30 +7,26 @@
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
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 Down
9 changes: 0 additions & 9 deletions test/queue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,12 @@
end

it "adds something to an empty Queue" do
skip
q = Queue.new
q.enqueue(10)
expect(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)
Expand All @@ -27,13 +25,11 @@
end

it "starts the size of a Queue at 0" do
skip
q = Queue.new
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)
Expand All @@ -43,7 +39,6 @@
end

it "removes something from the Queue" do
skip
q = Queue.new
q.enqueue(5)
removed = q.dequeue
Expand All @@ -52,7 +47,6 @@
end

it "removes the right something (LIFO)" do
skip
q = Queue.new
q.enqueue(5)
q.enqueue(3)
Expand All @@ -63,7 +57,6 @@
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 @@ -75,7 +68,6 @@
end

it "returns the front element in the Queue" do
skip
q = Queue.new
q.enqueue(40)
q.enqueue(22)
Expand All @@ -102,7 +94,6 @@
q.enqueue(130)
q.enqueue(140)
q.enqueue(150)
q.enqueue(150)
q.enqueue(160)
q.enqueue(170)
q.enqueue(180)
Expand Down
5 changes: 0 additions & 5 deletions test/stack_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
end

it "pushes something onto a empty Stack" do
skip
s = Stack.new
s.push(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)
Expand All @@ -26,13 +24,11 @@
end

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

it "removes something from the stack" do
skip
s = Stack.new
s.push(5)
removed = s.pop
Expand All @@ -41,7 +37,6 @@
end

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