-
Notifications
You must be signed in to change notification settings - Fork 42
Alex #11
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
base: master
Are you sure you want to change the base?
Alex #11
Changes from all commits
fec7fd9
837a5db
c9e678a
0c5b443
60c70a8
6ab1844
fcb9137
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,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 = { | ||
| "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 | ||
| 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)] | ||
|
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. An array of arrays?
Author
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. 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 } | ||
|
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 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 } | ||
|
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. If the rear wraps around the array, the output here wouldn't be quite right. |
||
|
|
||
| return adjusted_store.to_s | ||
| 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.
Nice use of a hash!