diff --git a/.github/PULL_REQUEST_TEMPLATE b/.github/PULL_REQUEST_TEMPLATE index ed27d164..0ef68adb 100644 --- a/.github/PULL_REQUEST_TEMPLATE +++ b/.github/PULL_REQUEST_TEMPLATE @@ -4,14 +4,14 @@ Thanks for doing some brain yoga. You are now submitting this assignment! ## Comprehension Questions | Question | Answer | |------------ | --------- | -| What is an ADT? | | -| Describe a Stack | | -| What are the 5 methods in Stack and what does each do? | | -| Describe a Queue | | -| What are the 5 methods in Queue and what does each do? | | -| What is the difference between implementing something and using something? | | +| What is an ADT? | We aren't talking about security but rather Abstact Data Types that allows all data operations to happen at one end only. | +| Describe a Stack | An ordered collection of items where the addition of new items and the removal of existing items always takes place at the same end. For example, shoving towels into the cupboard from the top, always. | +| What are the 5 methods in Stack and what does each do? | push() stores an element in the stack, pop() removes element from the stack, peep() gets the top towel from the linen closet without removing it, isFull() checks if the linen closet is full of towels, and lastly, isEmpty() check is if the linen closet is empty and "someone" needs to do the laundry. | +| Describe a Queue | Think of a line where you add a person to the end of the line (no cutting!) and take someone's order by popping them out of line at the front. | +| What are the 5 methods in Queue and what does each do? | add() someone to the end of the line, offer() inserts a specific person in the line (kind of like allowed cutting), element() returns the person at the front of the line, peek() also returns the person at the front of the line, remove () takes the front of the line out of the line, and poll() tells me who's at the front of the line and also takes them out of the line. | +| What is the difference between implementing something and using something? | Implementing is like carrying a bunch of stuff when you are going to garden: a shovel, a weed wacker, a rake. To use something is to actually use the shovel to dig the dirt to plant your bulbs way late in the season and hope they show up but probably not because you're not that lucky when it comes to gardening outdoors. | ## OPTIONAL JobSimulation | Question | Answer | |------------ | --------- | -| Did you include a sample run of your code as a comment? | | +| Did you include a sample run of your code as a comment? | um... | diff --git a/lib/problems.rb b/lib/problems.rb index 5085953d..0247f7aa 100644 --- a/lib/problems.rb +++ b/lib/problems.rb @@ -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 + value1 = jenga_stack.pop.to_i + 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) + 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 diff --git a/lib/queue.rb b/lib/queue.rb index 828217c6..e1d1b9c9 100644 --- a/lib/queue.rb +++ b/lib/queue.rb @@ -1,31 +1,102 @@ class Queue +# set queue_size here to something like 20 or whatever +queue_size = 20 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) - 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 - 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 \ No newline at end of file diff --git a/lib/stack.rb b/lib/stack.rb index cfc6ef0f..5dc9aadc 100644 --- a/lib/stack.rb +++ b/lib/stack.rb @@ -1,19 +1,27 @@ class Stack 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