-
Notifications
You must be signed in to change notification settings - Fork 44
Time - Haben #18
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?
Time - Haben #18
Changes from all commits
1897e45
32757f0
fa72828
40734f8
e917ad7
47ee784
4c824f6
743461d
e22e106
4bd9b45
8988d5c
bd02552
5c87ad6
d6aaf6f
9e3c58a
cc0873f
f6ecaa7
649ede3
4e6542a
bf072dc
716f842
77bb5cb
dc8a04d
d4a0b88
fcaee62
2c435b5
9492d2c
f887a77
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 |
|---|---|---|
| @@ -1,13 +1,74 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| require_relative './stack.rb' | ||
|
|
||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) | ||
| # # Space Complexity: O(n) | ||
| # def balanced(string) | ||
| # opening_brackets = Stack.new | ||
| # # string.each_char do |char| | ||
| # # # if an opening bracket, push it into the opening_brackets | ||
| # # if char == '{' || char == '(' || char == '[' | ||
| # # opening_brackets.push(char) | ||
| # # else | ||
|
|
||
| # # compare if the last opening bracket matches with the current closing bracket in char | ||
| # opening_bracket = opening_brackets.pop() | ||
| # if matching_braces == '}' && opening_bracket != '{' | ||
| # return false | ||
| # elsif matching_braces == ')' && opening_bracket != '(' | ||
| # return false | ||
| # elsif matching_braces == ']' && opening_bracket != '[' | ||
| # return false | ||
| # end | ||
|
|
||
| # # if everything matches/closes, then we have a balanced brackets | ||
| # return opening_brackets.empty? | ||
| # end | ||
|
|
||
| def balanced(string) | ||
| raise NotImplementedError, "Not implemented yet" | ||
| matching_hash = { | ||
| '{' => '}', | ||
| '(' => ')', | ||
| '[' => ']' | ||
| } | ||
| matching_stacks = Stack.new | ||
|
|
||
| string.each_char do |char| | ||
| if matching_hash.key? char | ||
| matching_stacks.push(char) | ||
| elsif matching_hash.value? char | ||
| closing_brace = matching_hash[matching_stacks.pop] | ||
| return false if closing_brace != char | ||
| end | ||
| end | ||
| matching_stacks.empty? | ||
| end | ||
|
|
||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # Time Complexity: O(n) | ||
| # Space Complexity: O(n) | ||
| def evaluate_postfix(postfix_expression) | ||
| raise NotImplementedError, "Not implemented yet" | ||
| operands = Stack.new | ||
| postfix_expression.each_char do |char| | ||
| if char != '+' && char != '*' && char != '-' && char != '/' | ||
| operands.push(char) | ||
| else # if not pop 2 values | ||
| y = operands.pop.to_i | ||
| x = operands.pop.to_i | ||
| result = 0 | ||
|
|
||
| if char == '+' | ||
| result = x + y | ||
| elsif char == '*' | ||
| result = x * y | ||
| elsif char == '-' | ||
| result = x - y | ||
| elsif char == '/' | ||
| result = x / y | ||
| end | ||
|
|
||
| operands.push(result) | ||
| end | ||
| end | ||
| operands.pop | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,45 @@ | ||
| class Queue | ||
| # frozen_string_literal: true | ||
|
|
||
| def initialize | ||
| # @store = ... | ||
| raise NotImplementedError, "Not yet implemented" | ||
| class Queue | ||
| def initialize(size = 30) | ||
| @store = [] | ||
| @size = size | ||
| @front = 0 | ||
| @back = 0 | ||
| end | ||
|
|
||
| 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" | ||
| end | ||
|
|
||
| def dequeue | ||
| raise NotImplementedError, "Not yet implemented" | ||
| if @front == (@back + 1) % @size | ||
| raise StandardError, 'Queue is full.' | ||
| else | ||
| @store[@back] = element | ||
| @back = (@back + 1) % @size | ||
| end | ||
| end | ||
|
|
||
| def front | ||
| raise NotImplementedError, "Not yet implemented" | ||
| def empty? | ||
| @front == @back # front & back are pointing to the same position, then we've read everything | ||
| end | ||
|
|
||
| def size | ||
| raise NotImplementedError, "Not yet implemented" | ||
| 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. 👍 |
||
| return nil if empty? | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| element = @store[@front] | ||
| @front = (@front + 1) % @size # advance the front pointer/index by 1 | ||
| element | ||
| end | ||
|
|
||
| def to_s | ||
|
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. 👍 |
||
| return @store.to_s | ||
| return [] if empty? | ||
|
|
||
| queue = [] | ||
| current = @front | ||
|
|
||
| while current != @back | ||
| queue << @store[current] | ||
| current = (current + 1) % @size | ||
| end | ||
|
|
||
| queue.to_s | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,19 +1,22 @@ | ||
| 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" | ||
| def pop() | ||
| return nil if self.empty? | ||
|
|
||
| element = @store.remove_first | ||
|
|
||
| return element | ||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| def empty?() | ||
| 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.
👍