-
Notifications
You must be signed in to change notification settings - Fork 41
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
base: master
Are you sure you want to change the base?
Fire - Noor #23
Changes from all commits
d651453
8bcf829
244dade
e2fdea3
89f09c7
1b2c3d0
b904c45
2abaa95
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 |
---|---|---|
@@ -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 | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -339,4 +339,4 @@ def to_s | |
|
||
return list.to_s | ||
end | ||
end | ||
end |
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 |
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) | ||
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 | ||
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. 👍 , 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 | ||
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" | ||
return 0 if @front.nil? | ||
if @front < @back | ||
@back - @front | ||
else | ||
Array_Length - (@front + @back) | ||
end | ||
end | ||
|
||
def empty? | ||
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" | ||
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,24 @@ | ||
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" | ||
@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 |
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.
👍