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
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true
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.13.6
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
44 changes: 44 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,47 @@
Marco? Polo!

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

Alexa and Jake!

1. DFS uses stack
2. BFS uses queue
3. DFS can be recursive
4. BFS to print a given depth at at time
5. Tree has heirarchy (parents and children). Graphs are any data set with linked nodes

Using DFS - simple tree of nodes

Start at root
push it on the stack and pop it off
run .children on it
Push its children onto the stack
pop them off
stack.pop

Using BFS - simple tree of nodes

Start at root
push it into the queue
pop it off
run .children on it
put children in the queue
pops from the front of the queue

Searching a graph using DFS

Start at given node
Go to first element of that array
Push all immediate nodes on to the stack
Pop off first node that shows up in array
Go to its immediate nodes and add all of its children to the stack
Continue in this manner: we get all nodes onto the stack and look at one directly
Until we get to a node with no children
If the node isn't the anwer it pops off and we move to its sibling node(if exists)
and we continue in this manner

Searching a graph using BFS

Start at given node and go down its array
@ each node that node has to get pushed into a queue
those nodes immediate nodes then get pushed into the queue
4 changes: 4 additions & 0 deletions lib/knight_searcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class KnightSearcher do


end
75 changes: 75 additions & 0 deletions lib/knights_travails.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
Move = Struct.new(:x, :y, :depth, :children, :parent)

class MoveTree

MOVEMENT_VECTORS = [
[1,2], [1,-2], [-1,2], [-1,-2],
[2,1], [2,-1], [-2,1], [-2,-1]
]

attr_reader :max_depth

def initialize(starting_position, max_depth = 1)
@max_depth = max_depth
@starting_position = [0,0]
@queue = []
end

def inspect
# Your tree has 9 move nodes and a max depth of 1
end

def move_nodes(node, target_value)
return print_statistics if [node.x, node.y] == target_value
return "Your target not found" if node.depth > max_depth

# if root, make new node
# node = Move.new(node.x, node.y, 0, [], nil) if depth_count == 0

possible_move_count += child_count
possible_move_count
end

def record_children(node)
# Setting and setting children
child_count = 0
MOVEMENT_VECTORS.each do |vector|
possible_move = add_vectors([node.x, node.y], vector)

# make new move struct at cood, parent current node
move_struct = Move.new(possible_move[0], possible_move[1], depth_count, nil, node)

# set parent nodes children as these add() results
node.children << move_struct

queue << move_struct
child_count += 1 unless off_board?(possible_move)
end
child_count
end

def print_statistics
puts "Whoopee!"
end

def branch_out(target_value)
# max depth logic goes here
4.times do
while !@queue.empty?
move_nodes(queue.unshift, target_value)
end
end
end

def add_vectors(position, vector)
[position[0] + vector[0], position[1] + vector[1]]
end

def off_board?(position)
x = position[0]
y = position[1]

x > 4 || x < 0 || y > 4 || y < 0
end

end
29 changes: 29 additions & 0 deletions spec/knights_travails_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'knights_travails'
require 'rspec'

describe MoveTree do
let(:basic_tree) { MoveTree.new([0, 0])}
describe '#new' do
it 'sets max_depth' do
expect(basic_tree.max_depth).to eq(1)
end

it 'starts with 0 move nodes' do
expect(basic_tree.move_nodes([0,0])).to eq(0)
end
end

describe '#move_nodes' do
it 'returns an integer' do
expect(basic_tree.move_nodes([1,1])).to be_an(Integer)
end
end

describe '#off board' do
it 'returns whether position is off board' do
expect(basic_tree.off_board?([5,3])).to be true
expect(basic_tree.off_board?([3,7])).to be true
expect(basic_tree.off_board?([3,3])).to be false
end
end
end
Loading