Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- class Anagram
- attr_accessor :file
- def initialize
- @data = Hash.new {|hash, key| hash[key] = []}
- self
- end
- def process
- @file.each_line do |line|
- word = line.strip!
- key = word.split("").sort!.join
- @data[key] << word
- end
- self
- end
- def output
- return @output if @output
- @output = ""
- @data.each do |key, words|
- next unless words.length > 1
- @output << words.join(" ") << "\n"
- end
- @output
- end
- end
- if __FILE__ == $0
- a = Anagram.new
- a.file = File.open(ARGV.first)
- puts a.process.output
- end
Add Comment
Please, Sign In to add comment