forked from kyclark/tiny_python_projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution1.py
More file actions
executable file
·66 lines (48 loc) · 1.76 KB
/
solution1.py
File metadata and controls
executable file
·66 lines (48 loc) · 1.76 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
64
65
66
#!/usr/bin/env python3
"""Telephone"""
import argparse
import os
import random
import string
# --------------------------------------------------
def get_args():
"""Get command-line arguments"""
parser = argparse.ArgumentParser(
description='Telephone',
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('text', metavar='text', help='Input text or file')
parser.add_argument('-s',
'--seed',
help='Random seed',
metavar='seed',
type=int,
default=None)
parser.add_argument('-m',
'--mutations',
help='Percent mutations',
metavar='mutations',
type=float,
default=0.1)
args = parser.parse_args()
if not 0 <= args.mutations <= 1:
parser.error(f'--mutations "{args.mutations}" must be between 0 and 1')
if os.path.isfile(args.text):
args.text = open(args.text).read().rstrip()
return args
# --------------------------------------------------
def main():
"""Make a jazz noise here"""
args = get_args()
text = args.text
random.seed(args.seed)
alpha = ''.join(sorted(string.ascii_letters + string.punctuation))
len_text = len(text)
num_mutations = round(args.mutations * len_text)
new_text = text
for i in random.sample(range(len_text), num_mutations):
new_char = random.choice(alpha.replace(new_text[i], ''))
new_text = new_text[:i] + new_char + new_text[i + 1:]
print(f'You said: "{text}"\nI heard : "{new_text}"')
# --------------------------------------------------
if __name__ == '__main__':
main()