-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscramble.py
More file actions
44 lines (34 loc) · 1.23 KB
/
scramble.py
File metadata and controls
44 lines (34 loc) · 1.23 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
'''
The script takes input from file "input.txt" and scrambles all the words in it.
scrambling is done in a way that all the apostropes are preserved and only the letters are scrambled.
The output is stored into file named "output.txt"
'''
import random
import string
import re
def scramble(word): #if word length is 1, return as it is.
if(len(word)==1 or len(word)==2):
return word
if(re.search(r"'",word)): #if word has apostrope, preserve the order.
foo = list(word[1:-2])
random.shuffle(foo)
return word[0] + ''.join(foo) + word[-2] + word[-1]
if(word[-1]!=',' and word[-1]!='.' and word[-1]!='?' and word[-1]!=';' and word[-1]!='!'):
foo = list(word[1:-1])
random.shuffle(foo)
return word[0] + ''.join(foo) + word[-1]
else: #if word has punctuation, preserve order.
foo = list(word[1:-1])
random.shuffle(foo)
return word[0] + ''.join(foo) + word[-2] + word[-1]
f = open("input.txt", "r")
fo = open("output.txt", "w")
scentence = f.read()
words = scentence.split()
msgstr=""
for i in words:
msgstr+=scramble(i) #call scramble() for all the words
msgstr+=' '
fo.write(msgstr)
f.close()
fo.close()