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

Branches - Brianna #36

Open
wants to merge 5 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
47 changes: 43 additions & 4 deletions lib/problems.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,52 @@
require_relative './stack.rb'

# Time Complexity: ?
# Space Complexity: ?
# Time Complexity: O(n) - n is chars in string
# Space Complexity: O(1/2n) --> O(n) - n is chars in string
def balanced(string)

Choose a reason for hiding this comment

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

👍

raise NotImplementedError, "Not implemented yet"
return true if string.empty?
return false if string.length % 2 != 0

s = Stack.new
open_bracs = ["(", "[", "{"]
close_bracs = [")", "]", "}"]

string.each_char do |char|
if open_bracs.include?(char)
s.push(char)
elsif close_bracs.include?(char)
compare = s.pop
if open_bracs.find_index(compare) != close_bracs.find_index(char)
return false
end
end
end

return true
end

# Time Complexity: ?
# Space Complexity: ?
def evaluate_postfix(postfix_expression)
Comment on lines 27 to 29

Choose a reason for hiding this comment

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

👍

raise NotImplementedError, "Not implemented yet"
operands = ["*", "**", "/", "%", "+", "-"]
s= Stack.new

postfix_expression.each_char do |char|
if operands.include?(char)
first = s.pop
second = s.pop
result = eval("#{second}#{char}#{first}")
s.push(result)
else
s.push(char)
end
end

return s.pop
end

# p balanced("{[{}{}]}")
# p balanced("{[{}{}]")
# p balanced("{()}[)")

# p evaluate_postfix("35+6*") #48
# p evaluate_postfix("53+62/*35*+") #39
71 changes: 56 additions & 15 deletions lib/queue.rb
Original file line number Diff line number Diff line change
@@ -1,31 +1,72 @@
class Queue

def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = Array.new(50)
@front = -1
@back = -1
end

def enqueue(element)
raise NotImplementedError, "Not yet implemented"
if((@front == 0 && @back == @store.length - 1) || (@front == @back + 1))

Choose a reason for hiding this comment

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

It might be better to use `@front == (@back + 1) % @store.length

raise ArgumentError
elsif @front == -1 && @back == -1
@front = 0
@back = 0
end

@store[@back] = element
@back = (@back + 1) % @store.length
end

def dequeue

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 == -1
raise ArgumentError
end

data = @store[@front]
@store[@front] = nil
@front = (@front + 1) % @store.length

if @front == @back
@front = -1
@back = -1
end

return data
end

def front
raise NotImplementedError, "Not yet implemented"
return @store[@front]
end

def size
raise NotImplementedError, "Not yet implemented"
if self.empty?
raise ArgumentError
end

return @back - @front

Choose a reason for hiding this comment

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

This only works if @front < @back,

Suggested change
return @back - @front
return @back - @front if @front < @back
return @back + @store.length - @front

end

def empty?
raise NotImplementedError, "Not yet implemented"
if @front == -1 && @back == -1
return true
else
return false
end
Comment on lines +51 to +55

Choose a reason for hiding this comment

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

Suggested change
if @front == -1 && @back == -1
return true
else
return false
end
return @front == -1 && @back == -1

end

def to_s
return @store.to_s
return @store[@front...@back].to_s

Choose a reason for hiding this comment

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

Again this only works if front < back.

end
end

# q = Queue.new
# q.enqueue(0)
# q.enqueue(1)
# q.enqueue(2)
# q.enqueue(3)
# p q
# p q.size
# p q.dequeue
# p q.size
# p q.front
Comment on lines +63 to +72

Choose a reason for hiding this comment

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

This should probably be removed before submission

Suggested change
# q = Queue.new
# q.enqueue(0)
# q.enqueue(1)
# q.enqueue(2)
# q.enqueue(3)
# p q
# p q.size
# p q.dequeue
# p q.size
# p q.front

26 changes: 17 additions & 9 deletions lib/stack.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
require_relative 'linked_list'

class Stack
def initialize
# @store = ...
raise NotImplementedError, "Not yet implemented"
@store = LinkedList.new
end

def push(element)
raise NotImplementedError, "Not yet implemented"
@store.add_last(element)
end

def pop
raise NotImplementedError, "Not yet implemented"
return nil if self.empty?
element = @store.remove_last()
return element
end

def empty?
raise NotImplementedError, "Not yet implemented"
return @store.empty?
end

def to_s
return @store.to_s
end
end

# stack = Stack.new
# stack.push(5)
# stack.push(10)
# p stack