forked from larissalages/code_problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshuffle_array.py
More file actions
32 lines (25 loc) · 863 Bytes
/
shuffle_array.py
File metadata and controls
32 lines (25 loc) · 863 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import random
class Solution:
def __init__(self, nums: List[int]):
self.nums = nums
self.org = nums.copy()
def reset(self) -> List[int]:
"""
Resets the array to its original configuration and return it.
"""
return self.org
def shuffle(self) -> List[int]:
"""
Returns a random shuffling of the array.
"""
# Fisher Yates Algorithm
for i in range(0, len(self.nums)):
idx_2 = random.randint(i, len(self.nums) - 1)
self.nums[i], self.nums[idx_2] = self.nums[idx_2], self.nums[i]
return self.nums
# Or use random.shuffle, it has a better perfomance
# random.shuffle(self.nums)
# Your Solution object will be instantiated and called as such:
# obj = Solution(nums)
# param_1 = obj.reset()
# param_2 = obj.shuffle()