Skip to content

Instantly share code, notes, and snippets.

@jin
Created October 30, 2014 05:53
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 jin/1d0157eb8d825b7493ab to your computer and use it in GitHub Desktop.
Save jin/1d0157eb8d825b7493ab to your computer and use it in GitHub Desktop.
#!/usr/bin/env ruby
def sum_of_differences(arr)
total = 0
arr.each_cons(2) do |x, y|
total += (x - y).abs
end
total
end
puts sum_of_differences [143, 86, 1470, 913, 1774, 948, 1509, 1022, 1750, 130]
puts sum_of_differences [143, 130, 86, 913, 948, 1022, 1470, 1509, 1750, 1774]
puts sum_of_differences [143, 913, 948, 1022, 1470, 1509, 1750, 1774, 130, 86]
puts sum_of_differences [143, 913, 948, 1022, 1470, 1509, 1750, 1774, 86, 130]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment