Skip to content

Branches - Erika #33

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

Open
wants to merge 9 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
9 changes: 6 additions & 3 deletions lib/linked_list.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

# Defines a node in the singly linked list
class Node
attr_reader :data # allow external entities to read value but not write
Expand Down Expand Up @@ -37,7 +38,8 @@ def remove_first()

value = @head.data
@head = @head.next
@head.previous = nil
@head.previous = nil unless @head.nil?

return value
end

Expand Down Expand Up @@ -265,7 +267,8 @@ def add_last(value)
end

def remove_last()
value = @tail.data
value = @tail.data unless @tail.nil?

if @head == @tail
@head = @tail = nil
else
Expand Down Expand Up @@ -339,4 +342,4 @@ def to_s

return list.to_s
end
end
end
36 changes: 34 additions & 2 deletions lib/problems.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,45 @@

require_relative './stack.rb'
require 'pry'

# Time Complexity: ?
# Space Complexity: ?
def balanced(string)
Comment on lines 5 to 7

Choose a reason for hiding this comment

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

👍 , Time & space complexity?

raise NotImplementedError, "Not implemented yet"

stack = Stack.new
index = 0
sets = {
")" => "(",
"}" => "{",
"]" => "["
}

while index <= string.length - 1

char = string[index]

if sets.values.include?(char)
stack.push(char)
elsif sets.keys.include?(char)
return false if stack.empty?
last_in = stack.pop
return false if last_in != sets[char]
end
index += 1

end

return stack.empty?

end






# Time Complexity: ?
# Space Complexity: ?
def evaluate_postfix(postfix_expression)
raise NotImplementedError, "Not implemented yet"
end
end
74 changes: 64 additions & 10 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,85 @@
class Queue

def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = Array.new(50)
@front = @rear = -1
end

def enqueue(element)
raise NotImplementedError, "Not yet implemented"

if (@rear + 1) % @store.length == @front
return ArgumentError.new("Queue is full")
end

if @front == -1 && @rear == -1
@front = @rear = 0
@store[@front] = element
else
@rear = (@rear + 1) % @store.length
@store[@rear] = element
end

end

def dequeue
raise NotImplementedError, "Not yet implemented"
def dequeue

Choose a reason for hiding this comment

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

👍


if @front == -1 && @rear == -1
return ArgumentError.new("Queue is empty")
end

element = @store[@front]

if @front == @rear
@store[@front] = nil
@front = @rear = -1
else
@store[@front] = nil
@front = (@front + 1) % @store.length
end

return element

end

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

def size

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 < @back
return @back - @front
else
return @back - 1 + @store.length - (@front + 1)
end

end

def empty?
raise NotImplementedError, "Not yet implemented"
return @front == @rear
end
Comment on lines 58 to 60

Choose a reason for hiding this comment

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

I think this should actually be:

return @front == -1 && @rear == -1



def to_s
return @store.to_s

store_arr = []
temp = @front

while temp <= @store.length - 1 && !@store[temp].nil?
store_arr << @store[temp]
temp += 1
end

if @store[temp].nil? && @rear < @front
temp = 0
while temp <= @rear
store_arr << @store[temp]
temp += 1
end
end

return store_arr.to_s
end
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_first(element)
end

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

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

def to_s
Expand Down
13 changes: 5 additions & 8 deletions test/problems_test.rb
Original file line number Diff line number Diff line change
@@ -1,51 +1,48 @@
require 'minitest/autorun'
require 'minitest/reporters'
require_relative '../lib/problems'
require 'minitest/skip_dsl'
require 'pry'

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

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
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 Down
15 changes: 5 additions & 10 deletions test/queue_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
require 'minitest/autorun'
require 'minitest/reporters'
require_relative '../lib/queue'
require 'minitest/skip_dsl'
require 'pry'

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

Expand All @@ -11,14 +13,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 +27,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 +40,6 @@
end

it "removes the right something (LIFO)" do
skip
q = Queue.new
q.enqueue(5)
q.enqueue(3)
Expand All @@ -53,7 +50,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,21 +61,22 @@
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

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
expect(q.dequeue).must_equal 30
q.enqueue(40)
q.enqueue(50)
q.enqueue(60)
Expand All @@ -92,15 +89,13 @@
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, 160, 170, 180, 190, 200, 210, 220, 230, 240]')
expect(q.to_s).must_equal('[40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210]')
end
end
Loading