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

Elle #13

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open

Elle #13

Show file tree
Hide file tree
Changes from 4 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
63 changes: 61 additions & 2 deletions lib/problems.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,68 @@
require_relative './stack.rb'

def balanced(string)
raise NotImplementedError, "Not implemented yet"

Choose a reason for hiding this comment

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

Nice!

brackets = {
'{'=> '}',
'('=> ')',
'['=> ']'
};

return true if string.length == 0
return false if string.length % 2 != 0

stack = Stack.new

string.each_char do |char|
if brackets[char]
stack.push(char)
else
return false if char != brackets[stack.pop]
end
end

return stack.empty?

end


# def balanced(string)?

# return !stack.empty?

# stack_left = Stack.new
# stack_right = Stack.new
# sub_string_length = string.length / 2
# left_counter = 0
# right_counter = string.length - 1
#
# until left_counter == sub_string_length
# stack_left.push(string[left_counter])
# left_counter += 1
# end
#
# until right_counter == sub_string_length - 1
# stack_right.push(string[right_counter])
# right_counter -= 1
# end
#
# until stack_left.empty? || stack_right.empty?
# left_value = stack_left.pop
# right_value = stack_right.pop
# return false if left_value != right_value
# end
#
# return true if stack_left.empty? && stack_right.empty?

# end

# def is_even_length?(string)
# return (string.length % 2 == 0) ? true : false
# end



# OPTIONAL WAVE 4
def evaluate_postfix(postfix_expression)
raise NotImplementedError, "Not implemented yet"
end
end
61 changes: 49 additions & 12 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,68 @@
QUEUE_SIZE = 16

class Queue

def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = Array.new(QUEUE_SIZE)
@front = nil
@rear = nil
# setting these to nil because in Ruby -1 also represents the end of the array
end

def enqueue(element)
raise NotImplementedError, "Not yet implemented"
if @front == nil
@front = 0
@rear = 1
@store[@front] = element
elsif @front == @rear
raise Error, "The queue is full."
else
@store[@rear] = element
@rear = (@rear + 1) % QUEUE_SIZE
end
end

def dequeue

Choose a reason for hiding this comment

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

Great work here!

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

def front
raise NotImplementedError, "Not yet implemented"
end
raise Error, "The queue is empty." if empty?

def size
raise NotImplementedError, "Not yet implemented"
@store[@front] = nil
@front = (@front + 1) % QUEUE_SIZE

return remove_item
end

# def front
# raise NotImplementedError, "Not yet implemented"
# end

# def size
# raise NotImplementedError, "Not yet implemented"
# end

def empty?
raise NotImplementedError, "Not yet implemented"
return (@front == nil && @rear == nil) || (@front == @rear)
end


# def to_s
# @store.to_s
# end

def to_s
return @store.to_s
array_as_string = "["
if @store[@front]
array_as_string = array_as_string + "#{@store[@front]}"
end

i = @front + 1
until i == @rear
array_as_string = array_as_string + ", #{@store[i]}"
i += 1

Choose a reason for hiding this comment

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

Since the array could wrap around, you should do i = (i + 1) % QUEUE_SIZE

end

array_as_string = array_as_string + "]"
return array_as_string
end
end
11 changes: 6 additions & 5 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
require_relative 'linked_list.rb'

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
Expand Down
14 changes: 5 additions & 9 deletions test/problems_test.rb
Original file line number Diff line number Diff line change
@@ -1,46 +1,42 @@
require 'minitest/autorun'
require 'minitest/reporters'
require_relative '../lib/problems'

require 'minitest/skip_dsl'
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
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
end

describe "postfix" do
xdescribe "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 +45,4 @@
expect(evaluate_postfix("62/5+")).must_equal 8
end
end
end
end
13 changes: 4 additions & 9 deletions test/queue_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require 'minitest/autorun'
require 'minitest/reporters'
require_relative '../lib/queue'

require 'minitest/skip_dsl'
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

describe "Test Queue Implementation" do
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,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 @@ -65,12 +60,12 @@
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
8 changes: 2 additions & 6 deletions test/stack_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require 'minitest/autorun'
require 'minitest/reporters'
require_relative '../lib/stack'
require 'minitest/skip_dsl'
Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

describe "Test Stack Implementation" do
Expand All @@ -10,14 +11,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 +25,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 +38,6 @@
end

it "removes the right something (LIFO)" do
skip
s = Stack.new
s.push(5)
s.push(3)
Expand All @@ -50,4 +46,4 @@
removed.must_equal 7
s.to_s.must_equal "[5, 3]"
end
end
end