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

Tiffany C. #34

Open
wants to merge 3 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
604 changes: 302 additions & 302 deletions lib/linked_list.rb

Large diffs are not rendered by default.

68 changes: 62 additions & 6 deletions lib/problems.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,69 @@
require_relative './stack.rb'

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n), push and pop should be O(1) operations since the linked list has both a head and tail pointer
# Space Complexity: O(n) if the string is not balanced and comprised wholly of open brackets, also O(n) if it actually closes
def balanced(string)

Choose a reason for hiding this comment

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

This works, but you can make it simpler by using a hash to do the mashing instead of using the case statement.

raise NotImplementedError, "Not implemented yet"
return false if string.length % 2 == 1
return true if string.length == 0

stack = Stack.new

string.each_char do |char|
case char
when "{"
stack.push("}")
when "["
stack.push("]")
when "("
stack.push(")")
when "}"
return false if stack.pop != "}"
when "]"
return false if stack.pop != "]"
when ")"
return false if stack.pop != ")"
else
return false
end
end

return stack.empty?
end

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n)
# Space Complexity: O(n)
def evaluate_postfix(postfix_expression)

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 nil if postfix_expression.length == 0

stack = Stack.new

postfix_expression.each_char do |char|
int_conversion = char.to_i
if int_conversion > 0
stack.push(int_conversion)
elsif int_conversion == 0 && char == "0"
stack.push(int_conversion)
else
postfix_operation(char, stack)
end
end

return stack.pop
end

def postfix_operation(operator, stack)
operand_2 = stack.pop
operand_1 = stack.pop

case operator
when "+"
stack.push(operand_1 + operand_2)
when "-"
stack.push(operand_1 - operand_2)
when "*"
stack.push(operand_1 * operand_2)
when "/"
raise ArgumentError, 'Cannot divide by 0' if (operand_1 == 0 || operand_2 == 0)
stack.push(operand_1 / operand_2)
end
end
43 changes: 30 additions & 13 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,30 +1,47 @@
class Queue
require_relative './linked_list'

class Queue

Choose a reason for hiding this comment

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

⚠️ This isn't using a circular buffer like requested!


def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
# @store = Array.new(10)
# -1 is a flag to just say that this queue is empty
# @front = @back = -1

@store = LinkedList.new
end

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

# if @front == @back
# # decide
# end

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

@store.add_last(element)
end

def dequeue
raise NotImplementedError, "Not yet implemented"
def dequeue
@store.remove_first
end

def front
raise NotImplementedError, "Not yet implemented"
end

def size
raise NotImplementedError, "Not yet implemented"
end

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

def to_s
return @store.to_s
end
Expand Down
50 changes: 50 additions & 0 deletions lib/queue2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class Queue

def initialize
@store = Array.new(30)
@front = @back = 0
# full when front == back

Choose a reason for hiding this comment

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

But when this object is created they're both equal!

end

def enqueue(element)

Choose a reason for hiding this comment

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

👍

# don't care when queue is empty
# only care if it's full

if @front == (@back + 1) % @store.length
raise ArgumentError, 'queue is currently full'
end

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

def dequeue

Choose a reason for hiding this comment

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

👍

# only care if queue is empty
if @front == @back
raise ArgumentError, 'the queue is empty'
end

removed = @store[@front]

@store[@front] = nil
@front = (@front + 1) % @store.length

return removed
end

def front
raise NotImplementedError, "Not yet implemented"
end

def size
raise NotImplementedError, "Not yet implemented"
end
Comment on lines +35 to +41

Choose a reason for hiding this comment

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

So not all done


def empty?
return @front == @back
end

def to_s
return @store.compact.to_s
end
Comment on lines +47 to +49

Choose a reason for hiding this comment

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

This works but it doesn't preserve element order if the back has wrapped around the buffer.

end
19 changes: 10 additions & 9 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
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"
@store.remove_last
end

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

def to_s
return @store.to_s
end
Expand Down
21 changes: 7 additions & 14 deletions test/problems_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,45 +7,38 @@
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

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
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
expect(evaluate_postfix("34*2/")).must_equal 6
expect(evaluate_postfix("34-1+")).must_equal 0
Expand All @@ -54,4 +47,4 @@
expect(evaluate_postfix("62/5+")).must_equal 8
end
end
end
end
101 changes: 101 additions & 0 deletions test/queue2_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
require 'minitest/autorun'
require 'minitest/reporters'
require_relative '../lib/queue2'

Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

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

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

it "adds multiple somethings to a Queue" do
q = Queue.new
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
q = Queue.new
q.empty?.must_equal true
end

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

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

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

it "returns the front element in the Queue" do
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)
q.enqueue(20)
q.enqueue(30)
expect(q.dequeue).must_equal 10
expect(q.dequeue).must_equal 20
q.enqueue(40)
q.enqueue(50)
q.enqueue(60)
q.enqueue(70)
q.enqueue(80)
q.enqueue(90)
q.enqueue(100)
q.enqueue(110)
q.enqueue(120)
q.enqueue(130)
q.enqueue(140)
q.enqueue(150)
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('[40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 150, 160, 170, 180, 190, 200, 210]')
end
end
Loading