-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathshuffle.py
More file actions
63 lines (53 loc) · 1.59 KB
/
shuffle.py
File metadata and controls
63 lines (53 loc) · 1.59 KB
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class Solution(object):
def __init__(self, nums):
"""
:type nums: List[int]
"""
self.array = nums
self.mixup = self.array[:]
self.length = len(self.array)
"""
# Logic2
# combinations
import itertools
self.combinations = []
for comb in itertools.permutations(self.array[:], r=len(nums)):
self.combinations.append(comb)
self.length = len(self.combinations)
"""
def reset(self):
"""
Resets the array to its original configuration and return it.
:rtype: List[int]
"""
self.mixup = self.array[:]
return self.mixup
def shuffle(self):
"""
Returns a random shuffling of the array.
:rtype: List[int]
"""
# Logic 1
import random
if self.length > 0:
for i in range(self.length):
v = random.randint(0, self.length-1)
self.mixup[v], self.mixup[i] = self.mixup[i], self.mixup[v]
return self.mixup
#while self.mixup[:] == self.array[:]:
# self.mixup = self.mixup[v:]+self.mixup[:v] # Not enough randomness
"""
# Logic 2
if self.length > 0:
import random
v = random.randint(0, self.length-1)
self.mixup = self.combinations[v][:]
return self.mixup
"""
# Your Solution object will be instantiated and called as such:
#nums = [1,2,3]
#obj = Solution(nums)
#param_1 = obj.reset()
#param_2 = obj.shuffle()
#print param_1
#print param_2