-
Notifications
You must be signed in to change notification settings - Fork 138
Expand file tree
/
Copy pathRandomWords.py
More file actions
executable file
·50 lines (40 loc) · 1.8 KB
/
RandomWords.py
File metadata and controls
executable file
·50 lines (40 loc) · 1.8 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
"""Write a function that randomises letters in words after the first N letters.
It will be called randomWords(words, n) where words is a list of words and n
the number of leading letters of each word to keep in order the remainder are
to be randomly jumbled up.
The code in main will call your function with different values of n to see
how much unscrambled text in each word you need to understand it.
Example:
Words please, <cr> to exit: Numeric is a hedge fund operating out of offices in Boston
0: imeNucr si a deghe dfnu rntpigeao out of ifosfce in otnBso
1: Nmuerci is a hedeg fudn oantrgiep out of ofsfcie in Bontos
2: Nucermi is a heedg fudn opertaign out of ofiscfe in Botson
3: Numrcei is a hedge fund operantgi out of offseic in Boston
4: Numeicr is a hedge fund operingat out of offisec in Bostno
5: Numeric is a hedge fund operaigtn out of offices in Boston
6: Numeric is a hedge fund operatngi out of offices in Boston
7: Numeric is a hedge fund operating out of offices in Boston
8: Numeric is a hedge fund operating out of offices in Boston
So you need about 4 or so letters to understand the sentence.
HINT: The random module is useful, it has a function shuffle(sequence) that
return the sequence randomly shuffled.
Created on 26 Feb 2015
@author: paulross
"""
import random
def randomWords(words, n):
pass
def main():
# Loop round until the user just gives a <cr> i.e. enter response.
while True:
line = raw_input('Words please, <cr> to exit: ')
words = line.split()
if len(words) == 0:
# User wants to quit
break
max_len = max([len(w) for w in words])
for n in range(max_len):
print('%4d: %s' % (n, ' '.join(randomWords(words, n))))
if __name__ == '__main__':
main()
print('Bye, bye.')