fork download
  1. class CharCountFilter
  2. TARGET_COUNT = 2
  3.  
  4. def self.filter(lines)
  5. lines.select { |line| char_appears_exactly_twice?(line) }
  6. end
  7.  
  8. def self.char_appears_exactly_twice?(line)
  9. count_characters(line).value?(TARGET_COUNT)
  10. end
  11.  
  12. def self.count_characters(line)
  13. line.chars.each_with_object(Hash.new(0)) { |char, counts| counts[char] += 1 }
  14. end
  15. end
  16.  
  17. lines = ARGF.readlines.map(&:chomp)
  18.  
  19. puts CharCountFilter.filter(lines)
Success #stdin #stdout 0.01s 8032KB
stdin
asdf
fdas
asds
d fm
dfaa
aaaa
aabb
aaabb
stdout
asds
dfaa
aabb
aaabb