Question | Click to View Answer |
Create a |
A stack is an ordered collection of items. New items are added to the top of the stack and items are removed from the stack starting at the top. This page has an awesome description of stacks. class Stack
attr_reader :items
def initialize(items)
@items = items
end
def push(item)
items.push(item)
end
def pop
items.pop
end
end
s = Stack.new(["a", "b", "c"])
s.push("dog")
p s.items
p s.pop # => "dog"
p s.pop # => "c"
|
Create a |
A Ruby array has def reverse(str)
stack = str.split("")
result = ""
while !stack.empty?
result += stack.pop
end
result
end
|
The
|
The def balanced_parenthesis?(str)
stack = []
str.split("").each do |char|
if char == "("
stack.push(char)
elsif char == ")"
return false if stack.empty?
stack.pop
end
end
stack.empty?
end
p balanced_parenthesis?('((()))') # true
p balanced_parenthesis?('(()') # false
p balanced_parenthesis?('((((((())') # false
p balanced_parenthesis?('()))') # false
p balanced_parenthesis?('(5+6)*(7+8)/(4+3)') # true
|
Use the Divide by 2 algorithm to write a |
The def divide_by_2(num)
stack = []
while num > 0
num.even? ? stack.push(0) : stack.push(1)
num = num / 2
end
stack.map(&:to_s).reverse.join
end
This link has an awesome description and Python solution to the problem. |
A queue is an ordered collection of items with a front and a rear. Items are added to the rear of the queue and taken from the front of the queue. Define a Hint: The string |
The class MyQueue
attr_reader :items
def initialize(items: [])
@items = items
end
def enqueue(item)
items.push(item)
end
def dequeue
items.shift
end
end
queue = MyQueue.new
queue.enqueue("boo")
queue.enqueue("42")
queue.enqueue(true)
queue.dequeue # => "boo"
queue.dequeue # => 42
|
In the previous question, we defined a |
The queue = MyQueue.new(items: ["hal", "matt", "bob", "sam", "joe"])
2.times do
name = queue.dequeue
queue.enqueue(name)
end
queue.items # => ["bob", "sam", "joe", "hal", "matt"]
|
What is a deque? |
A deque or double ended queue allows items to be added or removed from the front or the rear of the queue. A deque can behave like a stack, a queue, or both. This link has a great description of deques. The word "deque" refers to the abstract data type and is pronounced "deck". The word "dequeue" refers to removing items from a queue data structure. |
Create a |
The class Deque
attr_reader :items
def initialize(items: [])
@items = items
end
def add_rear(item)
items.push(item)
end
def remove_rear
items.pop
end
def add_front(item)
items.unshift(item)
end
def remove_front
items.shift
end
end
d = Deque.new
d.add_rear("cool") # items => ["cool"]
d.add_rear("nice") # items => ["cool", "nice"]
d.add_front(44) # items => [44, "cool", "nice"]
d.remove_front # items => ["cool", "nice"]
d.remove_rear # items => ["cool"]
|
In the previous question, we defined a |
def palindrome?(deque)
while deque.items.length > 1
if deque.remove_front != deque.remove_rear
return false
end
end
true
end
radar = Deque.new(items: "radar".split(""))
palindrome?(radar) # => true
cat = Deque.new(items: "cat".split(""))
palindrome?(cat) # => false
|