Created
September 8, 2015 06:33
-
-
Save logicrime/0f5d43127c68ead826cb to your computer and use it in GitHub Desktop.
addwords.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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