Guest User

Untitled

a guest
Jul 20th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.58 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. class Anagram
  4. attr_accessor :file
  5.  
  6. def initialize
  7. @data = Hash.new {|hash, key| hash[key] = []}
  8. self
  9. end
  10.  
  11. def process
  12. @file.each_line do |line|
  13. word = line.strip!
  14. key = word.split("").sort!.join
  15. @data[key] << word
  16. end
  17. self
  18. end
  19.  
  20. def output
  21. return @output if @output
  22. @output = ""
  23. @data.each do |key, words|
  24. next unless words.length > 1
  25. @output << words.join(" ") << "\n"
  26. end
  27. @output
  28. end
  29. end
  30.  
  31. if __FILE__ == $0
  32. a = Anagram.new
  33. a.file = File.open(ARGV.first)
  34. puts a.process.output
  35. end
Add Comment
Please, Sign In to add comment