Skip to content
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

Fire - Noor #23

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions .vscode/launch.json
Original file line number Diff line number Diff line change
@@ -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
}
]
}
2 changes: 1 addition & 1 deletion lib/linked_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -339,4 +339,4 @@ def to_s

return list.to_s
end
end
end
58 changes: 52 additions & 6 deletions lib/problems.rb
Original file line number Diff line number Diff line change
@@ -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
49 changes: 41 additions & 8 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -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)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 , very compact, nicely done!

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

raise NotImplementedError, "Not yet implemented"
return 0 if @front.nil?
if @front < @back
@back - @front
else
Array_Length - (@front + @back)
end
end

def empty?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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
14 changes: 8 additions & 6 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
class Stack

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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
3 changes: 2 additions & 1 deletion test/problems_test.rb
Original file line number Diff line number Diff line change
@@ -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

Expand Down
9 changes: 9 additions & 0 deletions test/queue_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down