-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrearrange.py
More file actions
30 lines (24 loc) · 812 Bytes
/
rearrange.py
File metadata and controls
30 lines (24 loc) · 812 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
# in-place shuffle algo
# import sys, random
# words = sys.argv[1:]
# random.shuffle(words)
# print words
import sys
from random import randint
# THIS EDITS THE ORIGINAL ARRAY
def shuffle(array):
# count elements in array
numberOfElements = len(array)
# number of indexes in array from number of elements in array
possibleIndexes = numberOfElements - 1
# iterate over all indexes
for i in range(0, numberOfElements):
# generate a random int that is one of the possible indexes
randomIndex = randint(0, possibleIndexes)
# swap the iterative index and the random index
array[i], array[randomIndex] = array[randomIndex], array[i]
# if run from terminal, use the command line args and print the result to the console
if __name__ == '__main__':
words = sys.argv[1:]
shuffle(words)
print words