fork download
  1. data = <<~TEXT
  2. outlook temperature humidity windy play
  3. sunny hot high FALSE no
  4. sunny hot high TRUE no
  5. overcast hot high FALSE yes
  6. rainy mild high FALSE yes
  7. rainy cool normal FALSE yes
  8. rainy cool normal TRUE no
  9. overcast cool normal TRUE yes
  10. sunny mild high FALSE no
  11. sunny cool normal FALSE yes
  12. rainy mild normal FALSE yes
  13. sunny mild normal TRUE yes
  14. overcast mild high TRUE yes
  15. overcast hot normal FALSE yes
  16. rainy mild high TRUE no
  17. TEXT
  18.  
  19. lines = data.lines.map(&:chomp)
  20. headers = lines.first.split("\t")
  21. counts = Hash.new { |h, k| h[k] = Hash.new(0) }
  22.  
  23. lines[1..-1].each do |line|
  24. values = line.split("\t")
  25. headers.each_with_index do |header, i|
  26. counts[header][values[i]] += 1
  27. end
  28. end
  29.  
  30. counts.each do |header, values|
  31. puts "#{header} counts:"
  32. values.each do |val, count|
  33. puts " #{val}: #{count}"
  34. end
  35. end
  36.  
Success #stdin #stdout 0.01s 8096KB
stdin
Standard input is empty
stdout
outlook counts:
  sunny: 5
  overcast: 4
  rainy: 5
temperature counts:
  hot: 4
  mild: 6
  cool: 4
humidity counts:
  high: 7
  normal: 7
windy counts:
  FALSE: 8
  TRUE: 6
play counts:
  no: 5
  yes: 9