Skip to content

Commit

Permalink
Arigeom beats kata
Browse files Browse the repository at this point in the history
  • Loading branch information
loz committed Dec 2, 2012
1 parent 3d4671c commit 223bfb7
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 0 deletions.
2 changes: 2 additions & 0 deletions Arigeom/.rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
--color
--format progress
19 changes: 19 additions & 0 deletions Arigeom/Guardfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# A sample Guardfile
# More info at https://github.com/guard/guard#readme

guard 'rspec', :version => 2 do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }

# Rails example
watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
watch('config/routes.rb') { "spec/routing" }
watch('app/controllers/application_controller.rb') { "spec/controllers" }
# Capybara request specs
watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
end

50 changes: 50 additions & 0 deletions Arigeom/lib/arigeom.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class Arigeom
def permutations(set)
options = []
(2..set.length).each do |size|
o = set.combination(size).to_a
options += o
end
options
end

def geometric_change(set)
differ(set) do |cur, last|
cur.to_f / last.to_f
end
end

def arithmetic_change(set)
differ(set) do |cur, last|
cur - last
end
end

def find_geometric(set)
options = permutations(set)
changes = options.map { |o| {set: o, change: geometric_change(o)} }
sets = changes.select {|o| o[:change].uniq.length == 1 }
sets = sets.map { |o| o[:set] }
sets.last
end

def find_arithmetic(set)
options = permutations(set)
changes = options.map { |o| {set: o, change: arithmetic_change(o)} }
sets = changes.select {|o| o[:change].uniq.length == 1 }
sets = sets.map { |o| o[:set] }
sets.last
end

private

def differ(set, &block)
options = set.dup
last = options.shift
options.map do |s|
change = yield(s, last)
last = s
change
end
end
end
56 changes: 56 additions & 0 deletions Arigeom/spec/arigeom_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
require 'spec_helper'

describe Arigeom do
subject { described_class.new }
describe :permutations do
it "returns arrays of possibe sets for current length" do
result = subject.permutations [1,2,3,4]
result.to_a.should == [
[1,2],
[1,3],
[1,4],
[2,3],
[2,4],
[3,4],
[1,2,3],
[1,2,4],
[1,3,4],
[2,3,4],
[1,2,3,4]
]
end
end

describe :geometric_change do
it "returns difference ratio between numbers" do
subject.geometric_change([2,4,8,16]).should == [2,2,2]
subject.geometric_change([2,4,12,66]).should == [2,3,5.5]
end
end

describe :arithmetic_change do
it "returns the differences between numbers" do
subject.arithmetic_change([2,4,6,8]).should == [2,2,2]
subject.arithmetic_change([2,6,12,20]).should == [4,6,8]
end
end

describe :find_geometric do
it "returns largest combination with equal geometric change" do
subject.find_geometric([2,4,5,8,11,16]).should == [2, 4, 8, 16]
subject.find_geometric([1,2,3,4,5]).should == [1,2,4]
subject.find_geometric([1,3,9,10,19,27,28,81]).should == [1,3,9,27,81]
subject.find_geometric([1,4,7,10,13,25]).should == [4,10,25]
end
end

describe :find_arithmetic do
it "returns largest combination with equal arithmetic change" do
subject.find_arithmetic([2,4,5,8,11,16]).should == [2,5,8,11]
subject.find_arithmetic([1,2,3,4,5]).should == [1,2,3,4,5]
subject.find_arithmetic([1,3,9,10,19,27,28,81]).should == [1,10,19,28]
subject.find_arithmetic([1,4,7,10,13,25]).should == [1,4,7,10,13]
end
end
end

14 changes: 14 additions & 0 deletions Arigeom/spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# This file was generated by the `rspec --init` command. Conventionally, all
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
# Require this file using `require "spec_helper.rb"` to ensure that it is only
# loaded once.
#
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration

require 'arigeom'

RSpec.configure do |config|
config.treat_symbols_as_metadata_keys_with_true_values = true
config.run_all_when_everything_filtered = true
config.filter_run :focus
end

0 comments on commit 223bfb7

Please sign in to comment.