diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 00000000..dbfd5530 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,16 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "ruby-debug", + "request": "launch", + "name": "Launch File", + "program": "${workspaceFolder}/${command:AskForProgramName}", + "programArgs": [], + "useBundler": false + } + ] +} \ No newline at end of file diff --git a/lib/linked_list.rb b/lib/linked_list.rb index edd32b4d..a62f49ba 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -339,4 +339,4 @@ def to_s return list.to_s end -end +end \ No newline at end of file diff --git a/lib/problems.rb b/lib/problems.rb index 5085953d..7bcf1cc2 100644 --- a/lib/problems.rb +++ b/lib/problems.rb @@ -1,13 +1,59 @@ require_relative './stack.rb' -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) +# Space Complexity: O(n) def balanced(string) - raise NotImplementedError, "Not implemented yet" + open = { + "(" => true, + "{" => true, + "[" => true + } + + close = { + ")" => "(", + "}" => "{", + "]" => "[" + } + + results = Stack.new + + string.each_char do |char| + if open[char] + results.push(char) + elsif close[char] + return false if results.pop != close[char] + end + end + + results.empty? end -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) +# Space Complexity: O(n) def evaluate_postfix(postfix_expression) - raise NotImplementedError, "Not implemented yet" + stack = [] + math = '*+/-' + + postfix_expression.each_char do |char| + if math.include?char + num2 = stack.pop + num1 = stack.pop + stack << calculator(num1, num2, char) + else + stack << char.to_i + end + end + stack.pop end + +def calculator(num1,num2,operator) + if operator == '+' + num1 + num2 + elsif operator == '-' + num1 - num2 + elsif operator == '*' + num1 * num2 + elsif operator == '/' + num1/num2 + end +end \ No newline at end of file diff --git a/lib/queue.rb b/lib/queue.rb index 828217c6..e86708ef 100644 --- a/lib/queue.rb +++ b/lib/queue.rb @@ -1,31 +1,64 @@ class Queue + Array_Length = 20 def initialize - # @store = ... - raise NotImplementedError, "Not yet implemented" + @store = Array.new(Array_Length) + @front = @back = 0 # back is the LAST EMPTY Space at the end end def enqueue(element) - raise NotImplementedError, "Not yet implemented" + if @front.nil? + @front = @back = 0 + elsif @front - @back == 1 || (@back - @front == Array_Length - 1) # edge case were @back and @front are in different sides of the circle + raise ArgumentError, 'Queue is full' + end + + @store[@back] = element + @back = (@back + 1) % Array_Length # to update the back + end def dequeue - raise NotImplementedError, "Not yet implemented" + if @front == @back || @front.nil? + raise ArgumentError, 'Queue is empty' + end + + temp = @store[@front] + @front = (@front + 1) % Array_Length + return temp end def front - raise NotImplementedError, "Not yet implemented" + return @store[@front] end def size - raise NotImplementedError, "Not yet implemented" + return 0 if @front.nil? + if @front < @back + @back - @front + else + Array_Length - (@front + @back) + end end def empty? - raise NotImplementedError, "Not yet implemented" + if @front.nil? || @front == @back + return true + else + return false + end end def to_s - return @store.to_s + results = [] + if @back >= @front + results = @store[@front...@back] + else + # from beginig to end of the list + begining to @back + results = @store[@front...Array_Length] + (@store[0...@back]) + end + return results.to_s end + + end diff --git a/lib/stack.rb b/lib/stack.rb index cfc6ef0f..d8b3b9ed 100644 --- a/lib/stack.rb +++ b/lib/stack.rb @@ -1,22 +1,24 @@ class Stack def initialize - # @store = ... - raise NotImplementedError, "Not yet implemented" + @store = LinkedList.new end def push(element) - raise NotImplementedError, "Not yet implemented" + @store.add_first(element) end def pop - raise NotImplementedError, "Not yet implemented" + return nil if @store.empty? + @store.remove_first end def empty? - raise NotImplementedError, "Not yet implemented" + return true if @store.empty? + return false end def to_s return @store.to_s end -end + +end \ No newline at end of file diff --git a/test/problems_test.rb b/test/problems_test.rb index 046f059e..f28aff57 100644 --- a/test/problems_test.rb +++ b/test/problems_test.rb @@ -1,8 +1,9 @@ require_relative 'test_helper' +require_relative '../lib/problems' Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -xdescribe "Test wave 3 problems" do +describe "Test wave 3 problems" do describe "balanced" do it "Given balanced strings it should return true" do diff --git a/test/queue_test.rb b/test/queue_test.rb index 92fc11fb..a6a0b74f 100644 --- a/test/queue_test.rb +++ b/test/queue_test.rb @@ -25,6 +25,15 @@ expect(q.to_s).must_equal "[10, 20, 30]" end + it "returns the correct size of the queue" do + + q = Queue.new + q.enqueue(1) + q.enqueue(2) + q.enqueue(3) + expect(q.size).must_equal 3 + end + it "starts the Queue empty" do q = Queue.new