Skip to content
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
2 changes: 2 additions & 0 deletions .rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--require spec_helper
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true
# A sample Gemfile
source "https://rubygems.org"

gem 'rspec'
gem 'guard-rspec'
65 changes: 65 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
GEM
remote: https://rubygems.org/
specs:
coderay (1.1.1)
diff-lcs (1.2.5)
ffi (1.9.14)
formatador (0.2.5)
guard (2.14.0)
formatador (>= 0.2.4)
listen (>= 2.7, < 4.0)
lumberjack (~> 1.0)
nenv (~> 0.1)
notiffany (~> 0.0)
pry (>= 0.9.12)
shellany (~> 0.0)
thor (>= 0.18.1)
guard-compat (1.2.1)
guard-rspec (4.7.3)
guard (~> 2.1)
guard-compat (~> 1.1)
rspec (>= 2.99.0, < 4.0)
listen (3.1.5)
rb-fsevent (~> 0.9, >= 0.9.4)
rb-inotify (~> 0.9, >= 0.9.7)
ruby_dep (~> 1.2)
lumberjack (1.0.10)
method_source (0.8.2)
nenv (0.3.0)
notiffany (0.1.1)
nenv (~> 0.1)
shellany (~> 0.0)
pry (0.10.4)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
rb-fsevent (0.9.8)
rb-inotify (0.9.7)
ffi (>= 0.5.0)
rspec (3.5.0)
rspec-core (~> 3.5.0)
rspec-expectations (~> 3.5.0)
rspec-mocks (~> 3.5.0)
rspec-core (3.5.4)
rspec-support (~> 3.5.0)
rspec-expectations (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-mocks (3.5.0)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (~> 3.5.0)
rspec-support (3.5.0)
ruby_dep (1.5.0)
shellany (0.0.1)
slop (3.6.0)
thor (0.19.1)

PLATFORMS
ruby

DEPENDENCIES
guard-rspec
rspec

BUNDLED WITH
1.12.5
70 changes: 70 additions & 0 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

## Uncomment and set this to only include directories you want to watch
# directories %w(app lib config test spec features) \
# .select{|d| Dir.exists?(d) ? d : UI.warning("Directory #{d} does not exist")}

## Note: if you are using the `directories` clause above and you are not
## watching the project directory ('.'), then you will want to move
## the Guardfile to a watched dir and symlink it back, e.g.
#
# $ mkdir config
# $ mv Guardfile config/
# $ ln -s config/Guardfile .
#
# and, you'll have to watch "config/Guardfile" instead of "Guardfile"

# Note: The cmd option is now required due to the increasing number of ways
# rspec may be run, below are examples of the most common uses.
# * bundler: 'bundle exec rspec'
# * bundler binstubs: 'bin/rspec'
# * spring: 'bin/rspec' (This will use spring if running and you have
# installed the spring binstubs per the docs)
# * zeus: 'zeus rspec' (requires the server to be started separately)
# * 'just' rspec: 'rspec'

guard :rspec, cmd: "bundle exec rspec" do
require "guard/rspec/dsl"
dsl = Guard::RSpec::Dsl.new(self)

# Feel free to open issues for suggestions and improvements

# RSpec files
rspec = dsl.rspec
watch(rspec.spec_helper) { rspec.spec_dir }
watch(rspec.spec_support) { rspec.spec_dir }
watch(rspec.spec_files)

# Ruby files
ruby = dsl.ruby
dsl.watch_spec_files_for(ruby.lib_files)

# Rails files
rails = dsl.rails(view_extensions: %w(erb haml slim))
dsl.watch_spec_files_for(rails.app_files)
dsl.watch_spec_files_for(rails.views)

watch(rails.controllers) do |m|
[
rspec.spec.call("routing/#{m[1]}_routing"),
rspec.spec.call("controllers/#{m[1]}_controller"),
rspec.spec.call("acceptance/#{m[1]}")
]
end

# Rails config changes
watch(rails.spec_helper) { rspec.spec_dir }
watch(rails.routes) { "#{rspec.spec_dir}/routing" }
watch(rails.app_controller) { "#{rspec.spec_dir}/controllers" }

# Capybara features specs
watch(rails.view_dirs) { |m| rspec.spec.call("features/#{m[1]}") }
watch(rails.layouts) { |m| rspec.spec.call("features/#{m[1]}") }

# Turnip features and steps
watch(%r{^spec/acceptance/(.+)\.feature$})
watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) do |m|
Dir[File.join("**/#{m[1]}.feature")][0] || "spec/acceptance"
end
end
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,19 @@
Marco? Polo!

[A data structures and algorithms Ruby challenge from the Viking Code School](http://www.vikingcodeschool.com)

What data structure is used to implement DFS?
stack

What data structure is typically used to implement BFS?
queue

Which one can be done recursively? (the clue should be the data structure)
DFS

Which one would you use to print a list of all the nodes in a tree or graph, starting with depth 1, then depth 2, then depth 3 etc.?
BFS

What is the difference between a tree and a graph?
A tree has only one parent and spreads out from a root node, a graph can have
many connections in all directions
5 changes: 5 additions & 0 deletions lib/launch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require './tree_searcher'
require './node'
require './path_finder'

PathFinder.new.find([0,0], [1,2])
30 changes: 30 additions & 0 deletions lib/node.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Node
attr_accessor :x, :y, :depth, :parent, :children

MOVES = [ [-2,-1], [-2,1],
[-1,2] , [-1,-2],
[1,2] , [1,-2],
[2,1] , [2,-1]
]

RANGE = (0..7)

def initialize(x,y, depth, parent = nil, children = [])
@x = x
@y = y
@parent = parent
@children = children
@depth = depth
end

def expand
MOVES.each do |move|
x = @x + move[0]
y = @y + move[1]

if RANGE.include?(x) && RANGE.include?(y)
@children << Node.new(x, y, @depth + 1, self)
end
end
end
end
27 changes: 27 additions & 0 deletions lib/path_finder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class PathFinder
def find(start_coords, target_coords)
# build first node
starting_node = Node.new(start_coords[0], start_coords[1], 0)

# serch those children to see if any are target
result = TreeBFS.search(starting_node, target_coords)

print_path(result)
end

def print_path(end_node)
path = []
parent_node = end_node

while parent_node
path << [parent_node.x, parent_node.y]
parent_node = parent_node.parent
end

puts "It took #{end_node.depth} steps!"
path.reverse.each do |step|
p step
puts
end
end
end
24 changes: 24 additions & 0 deletions lib/tree_searcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class TreeBFS

def self.search(root, target_coords)

queue = [root]

until queue.empty?
current_node = queue.shift

if current_node.x == target_coords[0] &&
current_node.y == target_coords[1]
return current_node
else
current_node.expand

current_node.children.each do |child|
queue << child
end
end
end
end
end


78 changes: 78 additions & 0 deletions psuedocode.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
Searching a simple tree of nodes where each Node has an array of child nodes (some_node.children) using DFS.

start at root node
check if it's value == target value
children.each do
check(child, target value), base case: root.left == nil or find value
end



Searching the same tree using BFS.

Using a queue.
Start at the root.
Check the root.
Add it's children to the queue.
Until queue is empty
dequeue and check node
enqueue node'children
Foreverrrr


Searching a graph (represented however you feel most comfortable -- Edge List, Adjacency List or Adjacency Matrix) using DFS.

Go to first connection in list, and check it's first connect, and....
Go to second connection, and check it's first connection.....


Searching the same graph using BFS.

Build Adjacenty Matrix from graph.
Iterate over array of lists. For each list, check all possibilities.

_______________________________________________________________________________________________



MOVES = [ [-2,-1], [-2,1],
[-1,2] , [-1,-2],
[1,2] , [1,-2],
[2,1] , [2,-1], ]

Cell Object ( * 64 )
x value
y value

Graph builder
generate moves graph (x, y, board_size=8)
@board_size = board_size
one set of branches at a time
check if out of bounds
set parents and depth
search those branches
when correct move is found
return cell.
cell.parent.parent......reverse with stack


Graph Searcher
find correct move (graph correct move)


















Loading