Guest User

Untitled

a guest
Mar 24th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. #Abdulwahed Wahedi
  2. #flatten code for CitrusByte
  3.  
  4. def test_flatten(arrays):
  5. #tests the flatten function with an input array of arrays
  6. for array in arrays:
  7. result = flatten(array)
  8. print 'testing ' + str(array)
  9. print 'result ' + str(result) + '\n'
  10.  
  11. def flatten(bumpy):
  12. #recursive function to flatten an arbitrarily nested array into a single dimensional array
  13. flat_array = []
  14. if len(bumpy) > 0:
  15. for x in bumpy:
  16. if isinstance(x, list):
  17. flat_array += flatten(x)
  18. else:
  19. flat_array += [x]
  20. return flat_array
  21.  
  22.  
  23. def main():
  24. #to test this more extensively I would add some kind of random array generator
  25. test_arrays = [
  26. [],
  27. [1],
  28. [1,[2,3]],
  29. [1,[2,[3,4,[5,6]]]],
  30. [[[[[[[[[[1]]]]]]]]]]
  31. ]
  32. test_flatten(test_arrays)
  33. return 0
  34.  
  35. if __name__ == "__main__":
  36. main()
Add Comment
Please, Sign In to add comment