Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Abdulwahed Wahedi
- #flatten code for CitrusByte
- def test_flatten(arrays):
- #tests the flatten function with an input array of arrays
- for array in arrays:
- result = flatten(array)
- print 'testing ' + str(array)
- print 'result ' + str(result) + '\n'
- def flatten(bumpy):
- #recursive function to flatten an arbitrarily nested array into a single dimensional array
- flat_array = []
- if len(bumpy) > 0:
- for x in bumpy:
- if isinstance(x, list):
- flat_array += flatten(x)
- else:
- flat_array += [x]
- return flat_array
- def main():
- #to test this more extensively I would add some kind of random array generator
- test_arrays = [
- [],
- [1],
- [1,[2,3]],
- [1,[2,[3,4,[5,6]]]],
- [[[[[[[[[[1]]]]]]]]]]
- ]
- test_flatten(test_arrays)
- return 0
- if __name__ == "__main__":
- main()
Add Comment
Please, Sign In to add comment