Advertisement
Guest User

Untitled

a guest
Sep 26th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.45 KB | None | 0 0
  1. # Flat array
  2. #input = [[1,2,[3]],4, 5, [6,7]]
  3. # How to use it:
  4. # 1. ruby flat.rb
  5. # 2. Type the input e.g: [[1,2,[3]],4, 5, [6,7]]
  6. # 3. Type Enter and Ctrl-D
  7. # it will print the oputput
  8.  
  9. def flat_array (input_a)
  10. flatten = [];
  11. for i in input_a
  12. if i.instance_of? Array
  13. flatten.concat(flat_array(i))
  14. else
  15. flatten.push(i)
  16. end
  17. end
  18. flatten
  19. end
  20.  
  21. input = $stdin.read
  22. input = eval(input.strip! || input);
  23. print flat_array(input), "\n"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement