pastebin light

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

pastebin light //misacek

Posted by alg_group_members.rb on Sun 27 Nov 2011 18:31:59 CET
download | new post

  1.  
  2. #!/usr/bin/env ruby
  3.  
  4. def group_members(size, operation)
  5.  
  6. a = [ ]
  7.  
  8. case operation
  9.  
  10. when '+'
  11. 0.upto(size-1){|i| a << i }
  12.  
  13. when '*'
  14. dropped = [ ]
  15. 2.upto(size-1){|i|
  16. res = size/i.to_f
  17. if res != res.to_i then
  18. no_other = true
  19. dropped.each{|j|
  20. no_other = false if i%j == 0
  21. }
  22. a << i if no_other == true
  23. else
  24. dropped << i
  25. end
  26. }
  27. a.insert(0, 1) # 0 is position
  28. end
  29.  
  30. return a
  31. end
  32.  

Syntax highlighting: