Skip to content

Instantly share code, notes, and snippets.

@mach1
Created March 22, 2014 17:34
Show Gist options
  • Save mach1/9711189 to your computer and use it in GitHub Desktop.
Save mach1/9711189 to your computer and use it in GitHub Desktop.
row = [ 5, 6, 3, 7, 4, 6, 3, 8 ]
def max_plus(row)
return row unless row.length > 1
row = get_maximums_and_differences row
split_row = get_halves row
# transform first half
split_row[0] = max_plus(split_row[0])
power = Math::log(row.length, 2) - 1
# multiply second half with sqrt(2) in power
split_row[1] = multiply_with_sqrt_of_two(split_row[1], power)
return split_row.flatten
end
def get_halves(row)
return row.each_slice( (row.size/2.0).round ).to_a
end
def get_maximums_and_differences(row)
maximums = []
differences = []
row.each_slice 2 do |pair|
maximums.push(pair.max)
differences.push(pair.first - pair.last)
end
return maximums + differences
end
def multiply_with_sqrt_of_two(row, power)
sqrt_two_in_power = Math::sqrt(2) ** power
return row.map { |number| (number * sqrt_two_in_power).round }
end
row = max_plus(row)
puts row.to_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment