-
Notifications
You must be signed in to change notification settings - Fork 44
space katem #31
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?
space katem #31
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 |
|---|---|---|
|
|
@@ -3,11 +3,114 @@ | |
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def balanced(string) | ||
| raise NotImplementedError, "Not implemented yet" | ||
| end | ||
| # raise NotImplementedError, "Not implemented yet" | ||
| #create new stack array | ||
| stack_1 = Stack.new | ||
| #loop thorugh each character in the string | ||
| string.each_char do |char| | ||
| #if char is a {, [, or (, then push to stack array | ||
| if char == "{" || char == "[" || char == "(" | ||
| stack_1.push(char) | ||
| end | ||
| #check if stack is not empty | ||
| #then check if the char is an {} | ||
| #then check use peek to see what's at the end of the array | ||
| #if it's a {, then pop it | ||
| if !stack_1.empty? | ||
| #check for {} | ||
| if char == "{" | ||
| if stack_1.peek == "}" | ||
| stack_1.pop | ||
| else | ||
| return false | ||
| end | ||
| end | ||
|
|
||
| #same check for [] | ||
| if char == "[" | ||
| if stack_1.peek == "]" | ||
| stack_1.pop | ||
| else | ||
| return false | ||
| end | ||
| end | ||
| #same check for () | ||
| if char == "(" | ||
| if stack_1.peek == ")" | ||
| stack_1.pop | ||
| else | ||
| return false | ||
| end | ||
| end | ||
| end | ||
| end #end class | ||
|
|
||
| #check if stack array is empty, if it is,then return true | ||
| if stack_1.empty? | ||
| return true | ||
| end | ||
| return false | ||
| end | ||
|
|
||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def evaluate_postfix(postfix_expression) | ||
| raise NotImplementedError, "Not implemented yet" | ||
| #got help from the principal eng at hs | ||
| #kris said: you have to set your operands | ||
| #then pretend you have the postfix_expression like 13-10 +8/15 * 3 | ||
| #when operands are found, push to stack (also create the stack first) | ||
| #when operator is found. two items are popped from stack | ||
| #should probably create a helper function for the two items being popped | ||
| #then something like pemdas happens | ||
| #push the result back into stack for future use | ||
| #final result lives at the top of the stack | ||
| #think of it at jenga pieces moving to the top ..or something... | ||
|
|
||
| #operands here | ||
| operands = { "0" => 0, "1" => 1, "2" => 2, "3" => 3, "4" => 4, "5" => 5, "6" => 6, "7" => 7, "8" => 8, "9" => 9 } | ||
| #helper function here | ||
| #when you pop, make sure to set it to int | ||
| def pop_two_jenga_pieces | ||
|
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. 👍 |
||
| value1 = jenga_stack.pop.to_i | ||
|
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. jenga? |
||
| value2 = jenga_stack.pop.to_i | ||
| return value1, value2 | ||
| end | ||
|
|
||
| def evaluate_postfix(postfix_expression) | ||
| # raise NotImplementedError, "Not implemented yet" | ||
| #create the stack first | ||
| jenga_stack = Stack.new | ||
|
|
||
| #loop through the expression | ||
| postfix_expression.each_char do |char| | ||
| #check if operands are included | ||
| if operands.include?(char) | ||
|
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. You should make |
||
| jenga_stack.push(char) | ||
| end | ||
|
|
||
| #check if the operand is a + | ||
| if char == "+" | ||
| value1, value2 = pop_two_jenga_pieces(jenga_stack) | ||
| jenga_stack.push(value2 + value1) | ||
| end | ||
|
|
||
| #check if the operand is a - | ||
| if char == "+" | ||
| value1, value2 = pop_two_jenga_pieces(jenga_stack) | ||
| jenga_stack.push(value2 - value1) | ||
| end | ||
|
|
||
| #check if the operand is a * | ||
| if char == "+" | ||
| value1, value2 = pop_two_jenga_pieces(jenga_stack) | ||
| jenga_stack.push(value2 * value1) | ||
| end | ||
|
|
||
| #check if the operand is a / | ||
| if char == "+" | ||
| value1, value2 = pop_two_jenga_pieces(jenga_stack) | ||
| jenga_stack.push(value2 / value1) | ||
| end | ||
| #return the top jenga piece | ||
| return jenga_stack.pop | ||
| end #end of do loop | ||
| end #end of function | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,102 @@ | ||
| class Queue | ||
| # set queue_size here to something like 20 or whatever | ||
| queue_size = 20 | ||
|
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 should be a constant, the fact that it's not gives you scope problems and failing tests |
||
|
|
||
| def initialize | ||
| # @store = ... | ||
| raise NotImplementedError, "Not yet implemented" | ||
| # raise NotImplementedError, "Not yet implemented" | ||
| # create a new array | ||
| @store = Array.new(queue_size) | ||
| #set the front and rear to position -1 bc it's empty still | ||
| @front = -1 | ||
| @rear = -1 | ||
| end | ||
|
|
||
| #example: i want to add myself to a line of 20 people for coffee | ||
| 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" | ||
| # raise NotImplementedError, "Not yet implemented" | ||
| #check if front is empty | ||
| if @front == -1 | ||
| #then rear is 1 and front is now 0 | ||
| @rear = 1 | ||
| @front = 0 | ||
| #access the element in the store array | ||
| @store[@front] = element | ||
| #otherwise if front IS the rear, then the queue is full | ||
| elsif @front == @rear | ||
| raise Error,"Tis Full" | ||
| #not empty | ||
| #calculate new rear then set it as the element | ||
| #set original rear to new rear | ||
| else | ||
| new_rear = (@rear + 1) % queue_size | ||
| @store[@rear] = element | ||
| @rear = new_rear | ||
| end | ||
| end | ||
|
|
||
| #example: wth, this coffee line too long, i want to remove myself from this | ||
| def dequeue | ||
| raise NotImplementedError, "Not yet implemented" | ||
| # raise NotImplementedError, "Not yet implemented" | ||
| #check if front is empty, if yes, return nil | ||
| if @front == -1 | ||
| return nil | ||
| #otherwise if front IS the rear, then the queue is full | ||
| elsif @front == @rear | ||
| raise Error, "Full up here" | ||
| #not empty | ||
| else | ||
| #do same thing as the enqueue | ||
| new_front = (@front + 1) % queue_size | ||
| temp = @store[@front] | ||
| @store[@front] = nil | ||
| @front = new_front | ||
| end | ||
| return temp | ||
| end | ||
|
|
||
| #the front is the front, so return the front | ||
| def front | ||
| raise NotImplementedError, "Not yet implemented" | ||
| # raise NotImplementedError, "Not yet implemented" | ||
| return @front | ||
| end | ||
|
|
||
| def 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. You can use the positions of front and rear to calculate the size of the queue. |
||
| raise NotImplementedError, "Not yet implemented" | ||
| # raise NotImplementedError, "Not yet implemented" | ||
| #start at 0 | ||
| count = 0 | ||
| #loop through each element in the array | ||
| @store.each do |element| | ||
| #check if it's not empty, then increment the count | ||
| if !element.nil? | ||
| count += 1 | ||
| end | ||
| return count | ||
| end | ||
|
|
||
| #if the size is less than 0, then pretty sure it's empty | ||
| #check if it's bigger than 0 first, if it is, then it's NOT empty == false | ||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| # raise NotImplementedError, "Not yet implemented" | ||
| if size > 0 | ||
| return false | ||
| end | ||
| return true | ||
| end | ||
|
|
||
| #return values as a string | ||
| def to_s | ||
| return @store.to_s | ||
| #create an array to hold the values | ||
| array_values = [] | ||
| #loop through array | ||
| @store.each do |element| | ||
| #check if it's not empty, then add element to the array that was created | ||
| if !element.nil? | ||
| array_values.append(element) | ||
| end | ||
| end | ||
| #return the array and make sure to do to_s to ensure string in case it mutated somewhere | ||
| return array_values.to_s | ||
| end | ||
| end | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,27 @@ | ||
| class Stack | ||
|
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. 👍 |
||
| def initialize | ||
| # @store = ... | ||
| raise NotImplementedError, "Not yet implemented" | ||
| # raise NotImplementedError, "Not yet implemented" | ||
| #create a new LinkedList | ||
| @store = LinkedList.new | ||
| end | ||
|
|
||
| def push(element) | ||
| raise NotImplementedError, "Not yet implemented" | ||
| # raise NotImplementedError, "Not yet implemented" | ||
| @store.add_last(element) | ||
| end | ||
|
|
||
| def pop | ||
| raise NotImplementedError, "Not yet implemented" | ||
| # raise NotImplementedError, "Not yet implemented" | ||
| temp = @store.get_last | ||
| @store.remove_last | ||
| return temp | ||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| # raise NotImplementedError, "Not yet implemented" | ||
| # just check if empty..is this a trick..a little too easy? lol | ||
| return @store.empty? | ||
| end | ||
|
|
||
| def to_s | ||
|
|
||
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.
I think you have the right idea here, but not quite there.
If the character is an open brace, parens etc. Push it into the Stack.
Otherwise pop the last item off the stack and see if it matches the current character, if it doesn't return false.
Continue until the end of the string.