-
Notifications
You must be signed in to change notification settings - Fork 44
Charlotte:Space #35
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?
Charlotte:Space #35
Changes from all commits
65fd1c1
a037895
0bd2d41
7625224
58f17e4
df1b792
b23f24e
363a058
d011fff
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,61 @@ | ||
| require_relative './stack.rb' | ||
|
|
||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| # time complexity: 0(n) where n is the legth | ||
| # space complexity: 0(n) - creates a new stack | ||
| def balanced(string) | ||
| raise NotImplementedError, "Not implemented yet" | ||
| return false if string.length.odd? | ||
|
|
||
| # create hash for quick look up | ||
| brackets = { | ||
| "(" => ")", "{" => "}", "[" => "]" | ||
| } | ||
| stack = Stack.new | ||
|
|
||
| string.chars.each do |char| | ||
| if brackets[char] | ||
| element = brackets[char] | ||
| stack.push(element) | ||
| else | ||
| if stack.pop != char | ||
| return false | ||
| end | ||
| end | ||
| end | ||
| return stack.empty? | ||
| end | ||
|
|
||
| # Time Complexity: 0(n) where n is the length | ||
| # Space Complexity: 0(n) | ||
| # def balanced(string) | ||
| # # string length must be even | ||
| # return false if string.length.odd? | ||
|
|
||
| # stack = Stack.new | ||
|
|
||
| # # loop through each char in the string | ||
| # string.each_char do |char| | ||
| # if char == "[" | ||
| # stack.push("]") | ||
| # elsif char == "{" | ||
| # stack.push("}") | ||
| # elsif char == "(" | ||
| # stack.push(")") | ||
| # # if current char is the same as the last inserted char on stack, remove last inserted char | ||
| # elsif !stack.empty? && stack.top == char | ||
| # stack.pop | ||
| # # if stack is empty or last char is not 'opening' pair | ||
| # else | ||
| # return false | ||
| # end | ||
| # end | ||
| # # finally if you get here and stack is empty - string is balanced | ||
| # return stack.empty? | ||
| # end | ||
|
|
||
|
|
||
| # Time Complexity: ? | ||
| # Space Complexity: ? | ||
| def evaluate_postfix(postfix_expression) | ||
| raise NotImplementedError, "Not implemented yet" | ||
| # optional wave 4 | ||
| def evaluate_postfix(postfix_expression) | ||
| raise NotImplementedError, "Not implemented yet" | ||
|
|
||
| end |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,31 +1,94 @@ | ||
| class Queue | ||
|
|
||
| def initialize | ||
| # @store = ... | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @store = Array.new(20) | ||
| @size = 20 | ||
| @front = -1 | ||
| @back = -1 | ||
| end | ||
|
|
||
| def enqueue(element) | ||
| raise NotImplementedError, "Not yet implemented" | ||
|
|
||
| # add element to rear | ||
| def enqueue(value) | ||
| if @front == -1 | ||
| @front = 0 | ||
| @back = 1 | ||
| @store[@front] = value | ||
| elsif @store.empty? | ||
| raise ArgumentError, "Full" | ||
|
Comment on lines
+16
to
+17
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. If the Queue is empty raise an argument error that it's full? I think you want to check to see if it's full by doing |
||
| else | ||
| @store[@back] = value | ||
| @back = (@back + 1) % @size | ||
| end | ||
| end | ||
|
|
||
| # remove element from front and return it | ||
| def dequeue | ||
| raise NotImplementedError, "Not yet implemented" | ||
| end | ||
|
|
||
| def front | ||
| raise NotImplementedError, "Not yet implemented" | ||
| if @front == -1 | ||
| raise ArgumentError, "Empty" | ||
| else | ||
| data = @store[@front] | ||
| @front = (@front + 1) % @size | ||
| if @store.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. You already checked to see if it's empty above with |
||
| @front = back = -1 | ||
| end | ||
| return data | ||
| end | ||
| end | ||
|
|
||
| def size | ||
| raise NotImplementedError, "Not yet implemented" | ||
| end | ||
|
|
||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| return @front == @back | ||
| end | ||
|
|
||
| def to_s | ||
| return @store.to_s | ||
| list = [] | ||
| current = @front | ||
|
|
||
| while current != @back | ||
| list << @store[current] | ||
| current = (current + 1) % @size | ||
| end | ||
| return list.to_s | ||
| end | ||
| end | ||
|
|
||
| # class Queue | ||
| # passes all tests but not made with a circular buffer. | ||
| # attr_accessor :front, :rear, :store | ||
| # attr_reader :size | ||
|
|
||
| # def initialize(size=20) | ||
| # @size = size | ||
| # @store = Array.new() | ||
| # @front = 0 | ||
| # @rear = -1 | ||
| # end | ||
|
|
||
| # # add element to rear | ||
| # def enqueue(element) | ||
| # return @store.push(element) | ||
| # end | ||
|
|
||
| # # remove element from front | ||
| # def dequeue | ||
| # return nil if @store.empty? | ||
| # return @store.shift() | ||
| # end | ||
|
|
||
| # def front(queue) | ||
| # return @front == -1 | ||
| # end | ||
|
|
||
| # def size | ||
| # return @store.size | ||
| # end | ||
|
|
||
| # def empty? | ||
| # if @front == 0 && @rear == -1 | ||
| # return @store.empty? | ||
| # end | ||
| # end | ||
|
|
||
| # def to_s | ||
| # return @store.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" | ||
| @store.remove_first | ||
| end | ||
|
|
||
| def empty? | ||
| raise NotImplementedError, "Not yet implemented" | ||
| @store.empty? | ||
| end | ||
|
|
||
| def top | ||
| @store.get_first | ||
| 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.
This works, but it's not what was asked. The assignment calls you to create a Queue using a circular buffer. So... yes it's a queue and passes the tests, but it wasn't implemented as requested.