Skip to content

Instantly share code, notes, and snippets.

@pikesley
Created July 13, 2012 16:44
Show Gist options
  • Save pikesley/3105905 to your computer and use it in GitHub Desktop.
Save pikesley/3105905 to your computer and use it in GitHub Desktop.
With A=1, B=2, C=3, ... and adding letters to give values to words so APPLE=50 CHERRY=77 etc. can you find two fruits with the same value ?
#!/usr/bin/env ruby
@@scores = {
"a" => 1, "b" => 2, "c" => 3, "d" => 4, "e" => 5, "f" => 6, "g" => 7, "h" => 8, "i" => 9, "j" => 10, "k" => 11, "l" => 12, "m" => 13, "n" => 14,
"o" => 15, "p" => 16, "q" => 17, "r" => 18, "s" => 19, "t" => 20, "u" => 21,
"v" => 22, "w" => 23, "x" => 24, "y" => 25, "z" => 26
}
class Fruit
attr_reader :score
def initialize f
@f = f.strip
@score = self.get_score f
end
def get_score f
score = 0
f.split("").each do |l|
begin
score += @@scores[l.downcase]
rescue TypeError
end
end
score
end
def to_s
@f
end
end
results = {}
f = File.open ARGV[0]
f.each do |l|
fruit = Fruit.new l.strip
if results[fruit.score]
results[fruit.score] << fruit
else
results[fruit.score] = [fruit]
end
end
results.keys.sort.each do |k|
if results[k].size > 1
puts "Match! %3d: %s" % [
k,
results[k].join(", ")
]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment