Skip to content

Instantly share code, notes, and snippets.

@mudge
Created March 13, 2009 10:40
Show Gist options
  • Save mudge/78519 to your computer and use it in GitHub Desktop.
Save mudge/78519 to your computer and use it in GitHub Desktop.
A challenge set by sross. (See his Lisp solution: http://gist.github.com/79799)
# Ensure that all the arrays in a hash are ordered the same (even if
# they have missing elements).
def ordered?(hash_of_arrays)
hash_of_arrays.all? do |k,v|
hash_of_arrays.all? { |k2,v2| v - (v - v2) == v2 - (v2 - v) }
end
end
ordered?({ 1 => [1, 2, 3, 4], 2 => [1, 3, 4], 3 => [2, 4, 5], 4 => [1, 2, 4, 6]})
#=> true
ordered?({ 1 => [1, 2, 3, 4], 2 => [1, 3, 4], 3 => [2, 4, 5], 4 => [1, 4, 2, 6]})
#=> false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment