-
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
Leaves - Dominique #43
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -1,31 +1,59 @@ | ||||||||||
class Queue | ||||||||||
|
||||||||||
def initialize | ||||||||||
# @store = ... | ||||||||||
raise NotImplementedError, "Not yet implemented" | ||||||||||
@store = Array.new(100) | ||||||||||
@front = @back = -1 | ||||||||||
@data = '' | ||||||||||
# - 1 gets put into @back and the value of @back is put into @front | ||||||||||
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. 👍 |
||||||||||
raise NotImplementedError, "Not yet implemented" | ||||||||||
end | ||||||||||
# ## if the queue is empty | ||||||||||
if @front == -1 && @back == -1 | ||||||||||
@front = 0 | ||||||||||
@back = 1 | ||||||||||
@store[@front] = element | ||||||||||
elsif @front == @back | ||||||||||
# if the queue if full | ||||||||||
raise ArgumentError.new('The queue is full') | ||||||||||
else | ||||||||||
@store[@back] = element | ||||||||||
@back = (@back + 1) % @store.length | ||||||||||
end | ||||||||||
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. 👍 |
||||||||||
raise NotImplementedError, "Not yet implemented" | ||||||||||
if @front == -1 | ||||||||||
raise ArgumentError, "Queue Empty" | ||||||||||
else | ||||||||||
value = @store[@front] | ||||||||||
@store[@front] = nil | ||||||||||
@front = (@front + 1) % @store.length | ||||||||||
if @front == @back | ||||||||||
@front = @back = -1 | ||||||||||
end | ||||||||||
return value | ||||||||||
end | ||||||||||
|
||||||||||
end | ||||||||||
|
||||||||||
def front | ||||||||||
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 yet implemented" | ||||||||||
return @store[@front] | ||||||||||
end | ||||||||||
|
||||||||||
def size | ||||||||||
raise NotImplementedError, "Not yet implemented" | ||||||||||
return @store.length | ||||||||||
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 only gives the size of the internal array size, not the number of elements in the Queue. |
||||||||||
end | ||||||||||
|
||||||||||
def empty? | ||||||||||
raise NotImplementedError, "Not yet implemented" | ||||||||||
return true if @front == @back | ||||||||||
|
||||||||||
return false | ||||||||||
Comment on lines
+51
to
+53
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.
Suggested change
|
||||||||||
end | ||||||||||
|
||||||||||
def to_s | ||||||||||
return @store.to_s | ||||||||||
return @store.compact.to_s | ||||||||||
end | ||||||||||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,57 +1,57 @@ | ||
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 | ||
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 | ||
expect(evaluate_postfix("34/7-")).must_equal -7 | ||
expect(evaluate_postfix("35+6*")).must_equal 48 | ||
expect(evaluate_postfix("62/5+")).must_equal 8 | ||
end | ||
end | ||
end | ||
# 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 | ||
# 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 | ||
# expect(evaluate_postfix("34/7-")).must_equal -7 | ||
# expect(evaluate_postfix("35+6*")).must_equal 48 | ||
# expect(evaluate_postfix("62/5+")).must_equal 8 | ||
# end | ||
# 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.
Data?