-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise38.py
More file actions
51 lines (29 loc) · 980 Bytes
/
Copy pathExercise38.py
File metadata and controls
51 lines (29 loc) · 980 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
45
46
47
48
49
50
51
import random
def shuffle(values: list) -> None:
"""
Shuffle the list randomly
:param values: values in list
:type values: list
:raise TypeError: If values is not a list
:return: None
:rtype: None
"""
for i in range(len(values)):
swapIndex = random.randint(0, len(values) - 1)
values[i], values[swapIndex] = values[swapIndex], values[i]
...
random.seed(42)
# Perform this test ten times:
for i in range(10):
testData1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
shuffle(testData1)
# Make sure the number of values hasn't changed:
assert len(testData1) == 10
# Make sure the order has changed:
assert testData1 != [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Make sure that when re-sorted, all the original values are there:
assert sorted(testData1) == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Make sure an empty list shuffled remains empty:
testData2 = []
shuffle(testData2)
assert testData2 == []