Skip to content
Open

Alex #11

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
70 changes: 66 additions & 4 deletions lib/problems.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,71 @@
require_relative './stack.rb'
require_relative "./stack.rb"

def balanced(string)
raise NotImplementedError, "Not implemented yet"
return true if string.length == 0

stack = Stack.new

pairs = {
"(" => ")",
"{" => "}",
"[" => "]",
}

string.each_char do |char|
if pairs.include?(char) # it's an opening parens
stack.push(char)
else # it's a closing parens
last_value = stack.pop
if char != pairs[last_value]
return false
end
end
end

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

def evaluate_postfix(postfix_expression)
raise NotImplementedError, "Not implemented yet"
end
numbers = {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nice use of a hash!

"0" => true,
"1" => true,
"2" => true,
"3" => true,
"4" => true,
"5" => true,
"6" => true,
"7" => true,
"8" => true,
"9" => true,
}

stack = Stack.new

postfix_expression.each_char do |val|
if numbers.include?(val)
stack.push(val.to_i)
else
operand_one = stack.pop
operand_two = stack.pop

if val == "+"
result = operand_one + operand_two
elsif val == "*"
result = operand_one * operand_two
elsif val == "-"
result = operand_two - operand_one
else
result = operand_two / operand_one
end

stack.push(result)
end
end

final_result = stack.pop
return final_result
end
44 changes: 36 additions & 8 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,59 @@
class Queue
QUEUE_SIZE = 20

def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = [Array.new(QUEUE_SIZE)]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

An array of arrays?

Copy link
Copy Markdown
Author

@alexandria7 alexandria7 Sep 10, 2019

Choose a reason for hiding this comment

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

WHOOPS! totally a mistake

@front = @rear = -1
end

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

@store[@front] = element
elsif @front == @rear
raise Error, "queue full!"
else
new_rear = (@rear + 1) % QUEUE_SIZE
@store[@rear] = element
@rear = new_rear
end
end

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

if self.size == 0
@front = @rear = -1
else
@front = (@front + 1) % QUEUE_SIZE
end

return removed
end

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

def size
raise NotImplementedError, "Not yet implemented"
filled_spots = @store.select { |val| val != nil }
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

This is a little bit less efficient than if you started at the front and counted through until you found the rear, or better yet calculated the size.

return filled_spots.length
end

def empty?
raise NotImplementedError, "Not yet implemented"
if @front == -1
return true
else
return false
end
end

def to_s
return @store.to_s
adjusted_store = @store.select { |val| val != nil }
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If the rear wraps around the array, the output here wouldn't be quite right.


return adjusted_store.to_s
end
end
16 changes: 11 additions & 5 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@
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)
return
end

def pop
raise NotImplementedError, "Not yet implemented"
return nil if @store.empty?

item = @store.remove_last

return item
end

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

def to_s
Expand Down
28 changes: 11 additions & 17 deletions test/problems_test.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,40 @@
require 'minitest/autorun'
require 'minitest/reporters'
require_relative '../lib/problems'
require "minitest/autorun"
require "minitest/reporters"
require_relative "../lib/problems"

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

describe "Test wave 3 problems" do
describe "balanced" do
it "Given balanced strings it should return true" do
skip
expect(balanced('(({}))')).must_equal true
expect(balanced("(({}))")).must_equal true
end

it "regards an empty string as balanced" do
skip
expect(balanced('')).must_equal true
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
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
expect(balanced("[]")).must_equal true
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 @@ -49,4 +43,4 @@
expect(evaluate_postfix("62/5+")).must_equal 8
end
end
end
end
15 changes: 4 additions & 11 deletions test/queue_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'minitest/autorun'
require 'minitest/reporters'
require_relative '../lib/queue'
require "minitest/autorun"
require "minitest/reporters"
require_relative "../lib/queue"

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

Expand All @@ -11,14 +11,12 @@
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)
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 "removes something from the Queue" do
skip
q = Queue.new
q.enqueue(5)
removed = q.dequeue
Expand All @@ -42,7 +38,6 @@
end

it "removes the right something (LIFO)" do
skip
q = Queue.new
q.enqueue(5)
q.enqueue(3)
Expand All @@ -53,7 +48,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 @@ -65,12 +59,11 @@
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
end
end
13 changes: 4 additions & 9 deletions test/stack_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require 'minitest/autorun'
require 'minitest/reporters'
require_relative '../lib/stack'
require "minitest/autorun"
require "minitest/reporters"
require_relative "../lib/stack"
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

describe "Test Stack Implementation" do
Expand All @@ -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 All @@ -50,4 +45,4 @@
removed.must_equal 7
s.to_s.must_equal "[5, 3]"
end
end
end