nonsequitur - 2007-10-31 19:25:06
states = [“alabama”, ..., “wyoming”]
module Enumerable def unique_combinations a = dup result = [] length.times { el1 = a.shift; a.each { |el2| result << [el1, el2] } } result end def normalize_and_cluster dict = {} each { |el| (dict[yield(el)] ||= []) << el } dict end end
states.unique_combinations.normalize_and_cluster { |state1, state2| (state1 + state2).split(//).sort }.each { |k, v| puts ”#{v0.join(’ + ’)} = #{v1.join(’ + ’)}” if v.length > 1 } # >> northcarolina + southdakota = northdakota + southcarolina
formatting is with Textile syntax. Comments are not displayed until they are approved by a moderator. Moderators will not approve unless the comment contributes value to the discussion.
nonsequitur - 2007-10-31 19:25:06
states = [“alabama”, ..., “wyoming”]
module Enumerable
def unique_combinations
a = dup
result = []
length.times { el1 = a.shift; a.each { |el2| result << [el1, el2] } } result
end
def normalize_and_cluster
dict = {}
each { |el| (dict[yield(el)] ||= []) << el } dict
end
end
states.unique_combinations.normalize_and_cluster { |state1, state2| (state1 + state2).split(//).sort }.each { |k, v| puts ”#{v0.join(’ + ’)} = #{v1.join(’ + ’)}” if v.length > 1 }
# >> northcarolina + southdakota = northdakota + southcarolina