This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fizzbuzz(number) | |
if number % 15 == 0 | |
"Fizzbuzz" | |
elsif number % 3 == 0 | |
"Fizz" | |
elsif number % 5 == 0 | |
"Buzz" | |
else | |
number | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SimpleStack | |
def initialize | |
@stack = Array.new | |
end | |
#removes the first item from the stack and returns the item popped | |
def pop | |
@stack.pop | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class SimpleQueue | |
def initialize | |
@queue = Array.new | |
end | |
#adds item to back of queue and returns the queue | |
def enqueue(item) | |
@queue.unshift(item) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require '../lib/Queue' | |
frontier = Queue.new | |
# set up the adjacency list that defines the graph | |
neighbors = {} | |
neighbors = {:a => [:b,:f], :b => [:c,:d], :c => [:e], :d =>[:e], :e =>[:c,:d], | |
:f => [:a,:g,:h,:i], :g => [:f], :h => [:f], :i => [:f] } | |
parent = {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#defines DFS Object. | |
#currently only implements dfs_visit!, which only traverses the graph from one node | |
#to fully implement DFS, need to loop through ALL starting nodes | |
class DFS | |
attr_reader :adj_list, :parent | |
def initialize(adj_list={}) | |
@adj_list = adj_list |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Queue | |
include Enumerable | |
attr_reader :queue | |
def initialize | |
@queue = Array.new | |
end | |
#enqueue(item) adds item to the end of the queue |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@fib_hash = {} | |
@fib_hash = {0 => 0, 1 => 1 } | |
def fibo_finder(n) | |
if @fib_hash.has_key?(n) | |
return @fib_hash[n] | |
else | |
@fib_hash[n] = fibo_finder(n-1) + fibo_finder(n-2) | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def fibo(n) | |
if n < 2 | |
n | |
else | |
fibo(n-1) + fibo(n-2) | |
end | |
end | |
puts fibo(8) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require '../lib/Queue' | |
frontier = Queue.new | |
neighbors = {} | |
neighbors = {:a => [:b,:f], | |
:b => [:a,:c,:d], | |
:c => [:b,:e], | |
:d => [:b,:e], | |
:e => [:c,:d], | |
:f => [:a,:g,:h,:i], | |
:g => [:f], |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DFS | |
attr_reader :adj_list, :parent | |
def initialize(adj_list={}) | |
@adj_list = adj_list | |
@parent = {} | |
end | |
def dfs_run!(starting_node) |
OlderNewer