Advertisement
Guest User

Untitled

a guest
May 19th, 2019
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.78 KB | None | 0 0
  1. import math
  2. import random
  3.  
  4. class Array:
  5. def __init__(self, n):
  6. """
  7. Parameters
  8. ----------
  9. n : int
  10. The size of the array
  11. """
  12. self.array = random.sample(range(n), n)
  13.  
  14. def reverse(self):
  15. """Rearranges an array backwards
  16.  
  17. Raises
  18. ------
  19. Exception
  20. If the input array is empty
  21. """
  22. if self.array:
  23. print('\n')
  24. print(self.array)
  25. print('\n')
  26. # Traverses half of the array
  27. for i in range(math.floor(len(self.array) / 2)):
  28. # Moves the last element to the first place and the first element to the last place
  29. self.array[i], self.array[len(self.array) - i - 1] = self.array[len(self.array) - i - 1], self.array[i]
  30. print(self.array)
  31. print('\n')
  32. else:
  33. raise Exception('Empty array!')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement