Skip to content

Instantly share code, notes, and snippets.

@logicrime
Created September 8, 2015 06:33
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 logicrime/0f5d43127c68ead826cb to your computer and use it in GitHub Desktop.
Save logicrime/0f5d43127c68ead826cb to your computer and use it in GitHub Desktop.
addwords.rb
#!/usr/bin/env ruby
# addwords.rb
# you know those posts that are like
# A+T+T+I+T+U+D+E= 100, or whatever?
# this program does that
# you need a file to pass to initialize
# that has one word per line
# most distros have a dict somewhere, just use that
class WordAdder
def initialize(f)
@letters = ('a'..'z').to_a
@words = File.open(f)
end
def add_up(word)
res = 0
word.each_char do |c|
res += @letters.index(c) + 1 if @letters.index(c)
end
res
end
def eat_file(thresh = 1)
@words.each_line do |line|
puts "#{line.chomp} = #{add_up(line)}" if add_up(line) >= thresh
end
end
end
w = WordAdder.new('words.txt')
w.eat_file(100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment