-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path384_shuffle_an_array.py
More file actions
44 lines (32 loc) · 955 Bytes
/
384_shuffle_an_array.py
File metadata and controls
44 lines (32 loc) · 955 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
33
34
35
36
37
38
39
40
41
42
43
44
import random
class Solution(object):
def __init__(self, nums):
"""
:type nums: List[int]
"""
self.nums = nums
self.N = len(nums)
def reset(self):
"""
Resets the array to its original configuration and return it.
:rtype: List[int]
"""
return(self.nums)
def shuffle(self):
"""
Returns a random shuffling of the array.
:rtype: List[int]
"""
randShuffling = self.N*[0]
usableVals = self.nums[:]
for i in range(self.N):
pointer = random.randint(0,self.N-i-1)
randShuffling[i] = usableVals[pointer]
del usableVals[pointer]
return(randShuffling)
# Your Solution object will be instantiated and called as such:
# obj = Solution(nums)
# param_1 = obj.reset()
# param_2 = obj.shuffle()
if __name__ == '__main__':
sol = Solution([1,2,3,4,5])