Advertisement
Guest User

stackoverflow.com/q/31194315

a guest
Jul 2nd, 2015
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.67 KB | None | 0 0
  1. require 'benchmark'
  2.  
  3. def get_array number, recurring
  4.     (1..number-recurring).map {|i| [i, i]} + (number-recurring..number).map {|i| [i, i-recurring]}
  5. end
  6.  
  7. def simons array
  8.     hash = array.reduce({}) {|hash, pair| (hash[pair.last] ||= []).push pair.first; hash}
  9.     hash.keys.combination(16).map do |array|
  10.       array.map {|parent| hash[parent]}
  11.     end.map {|array| array.first.product *array.drop(1)}.flatten(1).size
  12. end
  13.  
  14. def original array
  15.     array.combination(16).select {|c| c.map(&:last).uniq.size == c.size }.size
  16. end
  17.  
  18. puts "Original: #{Benchmark.measure { puts original get_array 33, 1}}"
  19. puts "Simons:   #{Benchmark.measure { puts simons   get_array 33, 1}}"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement