forked from AdaGold/stacks-queues
-
Notifications
You must be signed in to change notification settings - Fork 42
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
dev-elle-up
wants to merge
5
commits into
Ada-C11:master
Choose a base branch
from
dev-elle-up:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Elle #13
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
18d582b
Implemented the queue methods initialize, enqueue, and to_s. Adjusted…
dev-elle-up 8da1d33
Implemented remaining methods to pass tests. All green.
dev-elle-up 7f4b1ba
Wrote method calls for stacks.
dev-elle-up 128c1b5
Completed balanced method.
dev-elle-up da43f0c
Accounted for the circular buffer.
dev-elle-up File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
|
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
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. 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 | ||
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. Since the array could wrap around, you should do |
||
end | ||
|
||
array_as_string = array_as_string + "]" | ||
return array_as_string | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Nice!