Skip to content

Instantly share code, notes, and snippets.

@riffraff
Created March 31, 2010 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save riffraff/350453 to your computer and use it in GitHub Desktop.
Save riffraff/350453 to your computer and use it in GitHub Desktop.
require 'enumerator'
require 'mu'
def f (ary, size)
needed = ary.size - size
while needed > 0
result = Array.new
ary.each_slice(2) do |a,b|
if needed > 0
result << a+b
needed-=1
else
result << a << b
end
end
ary = result
end
ary
end
a = [1, 15, 5, 11, 8, 8, 3, 13, 7, 9, 10, 6, 4, 12, 14, 2]
t do
assert_equal f(a,15) , [16, 5, 11, 8, 8, 3, 13, 7, 9, 10, 6, 4, 12, 14,2]
end
t do
assert_equal f(a,14) , [16, 16, 8, 8, 3, 13, 7, 9, 10, 6, 4, 12, 14,2]
end
t do
assert_equal f(a,1) , [128]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment