Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import math
- import random
- class Array:
- def __init__(self, n):
- """
- Parameters
- ----------
- n : int
- The size of the array
- """
- self.array = random.sample(range(n), n)
- def reverse(self):
- """Rearranges an array backwards
- Raises
- ------
- Exception
- If the input array is empty
- """
- if self.array:
- print('\n')
- print(self.array)
- print('\n')
- # Traverses half of the array
- for i in range(math.floor(len(self.array) / 2)):
- # Moves the last element to the first place and the first element to the last place
- self.array[i], self.array[len(self.array) - i - 1] = self.array[len(self.array) - i - 1], self.array[i]
- print(self.array)
- print('\n')
- else:
- raise Exception('Empty array!')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement