-
Notifications
You must be signed in to change notification settings - Fork 49
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
base: master
Are you sure you want to change the base?
Tiffany C. #34
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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) | ||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,47 @@ | ||
class Queue | ||
require_relative './linked_list' | ||
|
||
class Queue | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
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 | ||
|
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
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 |
There was a problem hiding this comment.
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.