fork download
  1. module Enumerable
  2. def tally(h = {})
  3. each_with_object(h) {|x, h| h[x] = (h[x] || 0) + 1}
  4. end
  5. end
  6. f = ->a {
  7. factrial = ->n {(1..n).inject(:*) || 1}
  8. n_permutations = ->a {factrial.(a.size) / a.tally.values.map(&factrial).inject(:*)}
  9. (0...a.size).map {|i| a[i..-1]}.map {|b|
  10. b.count(&b[0].method(:>)) * n_permutations.(b) / b.size
  11. }.sum + 1
  12. }
  13. p f.([1, 2, 3, 4, 5, 6, 7, 8, 9])
  14. p f.([1, 2, 3, 4, 5, 6, 7, 9, 8])
  15. p f.([1, 2, 3, 4, 5, 6, 8, 7, 9])
  16. p f.([4, 1, 6, 5, 8, 9, 7, 3, 2])
  17. p f.([6, 8, 4, 7, 5, 3, 2, 1, 9])
  18. p f.([9, 8, 7, 6, 5, 4, 3, 2, 1])
  19. p f.([1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4])
  20. p f.([1, 1, 1, 2, 2, 2, 3, 3, 4, 3, 4, 4])
  21. p f.([1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 3, 4])
  22. p f.([2, 2, 2, 3, 3, 1, 4, 3, 4, 1, 1, 4])
  23. p f.([3, 2, 4, 4, 2, 4, 3, 3, 1, 1, 1, 2])
  24. p f.([4, 4, 4, 3, 3, 3, 2, 2, 2, 1, 1, 1])
  25. p f.([3, 1, 4, 1, 5, 9])
  26.  
Success #stdin #stdout 0.01s 8040KB
stdin
Standard input is empty
stdout
1
2
3
123456
234567
362880
1
2
3
123456
234567
369600
127