Skip to content

Commit

Permalink
Reject/Accepts to short circuit differences
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonathan Lozinski committed Dec 3, 2012
1 parent 68e7be9 commit b23020c
Showing 1 changed file with 32 additions and 9 deletions.
41 changes: 32 additions & 9 deletions Arigeom/lib/arigeom.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,27 @@ def arithmetic_change(set)
end
end

def geometric_acceptor(set)
acceptor(set) do |cur, last|
cur.to_f / last.to_f
end
end

def arithmetic_acceptor(set)
acceptor(set) do |cur, last|
cur - last
end
end

def find_geometric(set)
combination_detect(set) do |combi|
geometric_change(combi)
geometric_acceptor(combi)
end
end

def find_arithmetic(set)
combination_detect(set) do |combi|
arithmetic_change(combi)
arithmetic_acceptor(combi)
end
end

Expand All @@ -31,20 +43,31 @@ def combination_detect(set, &block)
selected = []
set.length.downto(2) do |size|
set.combination(size).each do |s|
changes = yield s
if changes.uniq.size == 1
return s
end
accepted = yield s
return s if accepted
end
end
nil
end

def acceptor(set, &block)
first = set[0]
prev = set[1]
last_change = yield(prev, first)
set[2..-1].each do |s|
change = yield(s, prev)
return unless last_change == change
prev = s
last_change = change
end
true
end

def differ(set, &block)
last = set.first
prev = set.first
set[1..-1].map do |s|
change = yield(s, last)
last = s
change = yield(s, prev)
prev = s
change
end
end
Expand Down

0 comments on commit b23020c

Please sign in to comment.