-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmimic.py
80 lines (63 loc) · 2.17 KB
/
mimic.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Copyright 2010 Google Inc.
# Licensed under the Apache License, Version 2.0
# http://www.apache.org/licenses/LICENSE-2.0
"""
Mimic exercise
Read in the file specified on the command line.
Do a simple split() on whitespace to obtain all the words in the file.
Rather than read the file line by line, it's easier to read it into
one giant string and split it once.
Note: the standard python module 'random' includes a random.choice(list)
method which picks a random element from a non-empty list.
You can try adding in line breaks around 70 columns so the output looks
better.
"""
__author__ = "???"
import random
import sys
def create_mimic_dict(filename):
"""Returns a dict mapping each word to a list of words which follow it.
For example:
Input: "I am a software developer, and I don't care who knows"
Output:
{
"" : ["I"],
"I" : ["am", "don't"],
"am": ["a"],
"a": ["software"],
"software" : ["developer,"],
"developer," : ["and"],
"and" : ["I"],
"don't" : ["care"],
"care" : ["who"],
"who" : ["knows"]
}
"""
# +++your code here+++
pass
def print_mimic_random(mimic_dict, num_words):
"""Given a previously created mimic_dict and num_words,
prints random words from mimic_dict as follows:
- Use a start_word of '' (empty string)
- Print the start_word
- Look up the start_word in your mimic_dict and get its next-list
- Randomly select a new word from the next-list
- Repeat this process num_words times
"""
# +++your code here+++
pass
def main(args):
# Get input filename from command line args
filename = args[0]
# Create and print the jumbled (mimic) version of the input file
print(f'Using {filename} as input:\n')
d = create_mimic_dict(filename)
print_mimic_random(d, 200)
if __name__ == '__main__':
if len(sys.argv) != 2:
print('usage: python mimic.py file-to-read')
else:
main(sys.argv[1:])
print('\n\nCompleted.')