top_rank_per_group.sf 838 B

12345678910111213141516171819202122232425262728293031323334
  1. #!/usr/bin/ruby
  2. #
  3. ## https://rosettacode.org/wiki/Top_rank_per_group
  4. #
  5. var data = <<'EOF'.lines.map{ <name id salary dept> ~Z .split(',') -> flat.to_h }
  6. Tyler Bennett,E10297,32000,D101
  7. John Rappl,E21437,47000,D050
  8. George Woltman,E00127,53500,D101
  9. Adam Smith,E63535,18000,D202
  10. Claire Buckman,E39876,27800,D202
  11. David McClellan,E04242,41500,D101
  12. Rich Holcomb,E01234,49500,D202
  13. Nathan Adams,E41298,21900,D050
  14. Richard Potter,E43128,15900,D101
  15. David Motsinger,E27002,19250,D202
  16. Tim Sampair,E03033,27000,D101
  17. Kim Arlich,E10001,57000,D190
  18. Timothy Grove,E16398,29900,D190
  19. EOF
  20. var n = 5
  21. for d in (data.map {|h| h{:dept} }.uniq.sort) {
  22. var es = data.grep { _{:dept} == d }.sort_by { -Num(_{:salary}) }
  23. say d
  24. n.times {
  25. es || break
  26. printf("%-15s | %-6s | %5d\n", es.shift(){<name id salary>...})
  27. }
  28. print "\n"
  29. }