Skip to content

Commit b96985a

Browse files
committed
complete play4 with two solutions
1 parent 4b29a93 commit b96985a

5 files changed

Lines changed: 695 additions & 0 deletions

File tree

Lines changed: 361 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,361 @@
1+
{
2+
"cells": [
3+
{
4+
"cell_type": "code",
5+
"execution_count": 4,
6+
"metadata": {
7+
"collapsed": false
8+
},
9+
"outputs": [
10+
{
11+
"name": "stdout",
12+
"output_type": "stream",
13+
"text": [
14+
"line is: \n",
15+
"You are watching a film in which two men are having a fight. They hit one another hard. At the start they only fight with their fists. But soon they begin hitting one another over the heads with chairs. And so it goes on until one of the men crashes through a window and falls thirty feet to the ground below. He is dead!Of course he isn't really dead. With any luck he isn't even hurt. Why? Because the men who fall out of high windows or jump from fast moving trains, who crash cars of even catch fire, are professionals. They do this for a living. These men are called stuntmen. That is to say, they perform tricks.There are two sides to their work. They actually do most of the things you see on the screen. For example, they fall from a high building. However, they do not fall on to hard ground but on to empty cardboard boxes covered with a mattress . Again, when they hit one another with chairs, the chairs are made of soft wood and when they crash through windows, the glass is made of sugar!But although their work depends on trick of this sort, it also requires a high degree of skill and training. Often a stuntman' s success depends on careful timing. For example, when he is \"blown up\" in a battle scene, he has to jump out of the way of the explosion just at the right moment.\n",
16+
"\n",
17+
"line.split() is: \n",
18+
"['You', 'are', 'watching', 'a', 'film', 'in', 'which', 'two', 'men', 'are', 'having', 'a', 'fight.', 'They', 'hit', 'one', 'another', 'hard.', 'At', 'the', 'start', 'they', 'only', 'fight', 'with', 'their', 'fists.', 'But', 'soon', 'they', 'begin', 'hitting', 'one', 'another', 'over', 'the', 'heads', 'with', 'chairs.', 'And', 'so', 'it', 'goes', 'on', 'until', 'one', 'of', 'the', 'men', 'crashes', 'through', 'a', 'window', 'and', 'falls', 'thirty', 'feet', 'to', 'the', 'ground', 'below.', 'He', 'is', 'dead\\xef\\xbc\\x81Of', 'course', 'he', \"isn't\", 'really', 'dead.', 'With', 'any', 'luck', 'he', \"isn't\", 'even', 'hurt.', 'Why?', 'Because', 'the', 'men', 'who', 'fall', 'out', 'of', 'high', 'windows', 'or', 'jump', 'from', 'fast', 'moving', 'trains,', 'who', 'crash', 'cars', 'of', 'even', 'catch', 'fire,', 'are', 'professionals.', 'They', 'do', 'this', 'for', 'a', 'living.', 'These', 'men', 'are', 'called', 'stuntmen.', 'That', 'is', 'to', 'say,', 'they', 'perform', 'tricks.There', 'are', 'two', 'sides', 'to', 'their', 'work.', 'They', 'actually', 'do', 'most', 'of', 'the', 'things', 'you', 'see', 'on', 'the', 'screen.', 'For', 'example,', 'they', 'fall', 'from', 'a', 'high', 'building.', 'However,', 'they', 'do', 'not', 'fall', 'on', 'to', 'hard', 'ground', 'but', 'on', 'to', 'empty', 'cardboard', 'boxes', 'covered', 'with', 'a', 'mattress', '.', 'Again,', 'when', 'they', 'hit', 'one', 'another', 'with', 'chairs,', 'the', 'chairs', 'are', 'made', 'of', 'soft', 'wood', 'and', 'when', 'they', 'crash', 'through', 'windows,', 'the', 'glass', 'is', 'made', 'of', 'sugar!But', 'although', 'their', 'work', 'depends', 'on', 'trick', 'of', 'this', 'sort,', 'it', 'also', 'requires', 'a', 'high', 'degree', 'of', 'skill', 'and', 'training.', 'Often', 'a', \"stuntman'\", 's', 'success', 'depends', 'on', 'careful', 'timing.', 'For', 'example,', 'when', 'he', 'is', '\"blown', 'up\"', 'in', 'a', 'battle', 'scene,', 'he', 'has', 'to', 'jump', 'out', 'of', 'the', 'way', 'of', 'the', 'explosion', 'just', 'at', 'the', 'right', 'moment.']\n",
19+
"line is: \n",
20+
"\n",
21+
"\n",
22+
"line.split() is: \n",
23+
"[]\n",
24+
"line is: \n",
25+
"Naturally stuntmen are well paid for their work, but they lead dangerous lives. They often get seriously injured, and sometimes killed. A Norwegian stuntman, for example, skied over the edge of a cliff a thousand feet high. His parachute failed to open, and he was killed. In spite of all the risks, this is no longer a profession for men only. Men no longer dress up as women when actresses have to perform some dangerous action. For nowadays there are stuntgirls tool.\n",
26+
"\n",
27+
"line.split() is: \n",
28+
"['Naturally', 'stuntmen', 'are', 'well', 'paid', 'for', 'their', 'work,', 'but', 'they', 'lead', 'dangerous', 'lives.', 'They', 'often', 'get', 'seriously', 'injured,', 'and', 'sometimes', 'killed.', 'A', 'Norwegian', 'stuntman,', 'for', 'example,', 'skied', 'over', 'the', 'edge', 'of', 'a', 'cliff', 'a', 'thousand', 'feet', 'high.', 'His', 'parachute', 'failed', 'to', 'open,', 'and', 'he', 'was', 'killed.', 'In', 'spite', 'of', 'all', 'the', 'risks,', 'this', 'is', 'no', 'longer', 'a', 'profession', 'for', 'men', 'only.', 'Men', 'no', 'longer', 'dress', 'up', 'as', 'women', 'when', 'actresses', 'have', 'to', 'perform', 'some', 'dangerous', 'action.', 'For', 'nowadays', 'there', 'are', 'stuntgirls', 'tool.']\n",
29+
"line is: \n",
30+
"In some ways, the United States has made some progress. Fires no longer destroy 18,000 buildings as they did in the Great Chicago Fire of 1871, or kill half a town of 2,400 people, as they did the same night in Peshtigo, Wisconsin. Other than the Beverly Hill Supper Club fire in Kentucky in 1977, it has been four decades since more than 100 Americans died in a fire.\n",
31+
"\n",
32+
"line.split() is: \n",
33+
"['In', 'some', 'ways,', 'the', 'United', 'States', 'has', 'made', 'some', 'progress.', 'Fires', 'no', 'longer', 'destroy', '18,000', 'buildings', 'as', 'they', 'did', 'in', 'the', 'Great', 'Chicago', 'Fire', 'of', '1871,', 'or', 'kill', 'half', 'a', 'town', 'of', '2,400', 'people,', 'as', 'they', 'did', 'the', 'same', 'night', 'in', 'Peshtigo,', 'Wisconsin.', 'Other', 'than', 'the', 'Beverly', 'Hill', 'Supper', 'Club', 'fire', 'in', 'Kentucky', 'in', '1977,', 'it', 'has', 'been', 'four', 'decades', 'since', 'more', 'than', '100', 'Americans', 'died', 'in', 'a', 'fire.']\n",
34+
"line is: \n",
35+
"\n",
36+
"\n",
37+
"line.split() is: \n",
38+
"[]\n",
39+
"line is: \n",
40+
"But even with such successes, the United States still has one of the worst fire death rates in the world. Safety experts say the problem is neither money nor technology, but the indifference of a country that just will not take fires seriously enough.\n",
41+
"\n",
42+
"line.split() is: \n",
43+
"['But', 'even', 'with', 'such', 'successes,', 'the', 'United', 'States', 'still', 'has', 'one', 'of', 'the', 'worst', 'fire', 'death', 'rates', 'in', 'the', 'world.', 'Safety', 'experts', 'say', 'the', 'problem', 'is', 'neither', 'money', 'nor', 'technology,', 'but', 'the', 'indifference', 'of', 'a', 'country', 'that', 'just', 'will', 'not', 'take', 'fires', 'seriously', 'enough.']\n"
44+
]
45+
},
46+
{
47+
"data": {
48+
"text/plain": [
49+
"Counter({'\"blown': 1,\n",
50+
" '.': 1,\n",
51+
" '100': 1,\n",
52+
" '18,000': 1,\n",
53+
" '1871,': 1,\n",
54+
" '1977,': 1,\n",
55+
" '2,400': 1,\n",
56+
" 'A': 1,\n",
57+
" 'Again,': 1,\n",
58+
" 'Americans': 1,\n",
59+
" 'And': 1,\n",
60+
" 'At': 1,\n",
61+
" 'Because': 1,\n",
62+
" 'Beverly': 1,\n",
63+
" 'But': 2,\n",
64+
" 'Chicago': 1,\n",
65+
" 'Club': 1,\n",
66+
" 'Fire': 1,\n",
67+
" 'Fires': 1,\n",
68+
" 'For': 3,\n",
69+
" 'Great': 1,\n",
70+
" 'He': 1,\n",
71+
" 'Hill': 1,\n",
72+
" 'His': 1,\n",
73+
" 'However,': 1,\n",
74+
" 'In': 2,\n",
75+
" 'Kentucky': 1,\n",
76+
" 'Men': 1,\n",
77+
" 'Naturally': 1,\n",
78+
" 'Norwegian': 1,\n",
79+
" 'Often': 1,\n",
80+
" 'Other': 1,\n",
81+
" 'Peshtigo,': 1,\n",
82+
" 'Safety': 1,\n",
83+
" 'States': 2,\n",
84+
" 'Supper': 1,\n",
85+
" 'That': 1,\n",
86+
" 'These': 1,\n",
87+
" 'They': 4,\n",
88+
" 'United': 2,\n",
89+
" 'Why?': 1,\n",
90+
" 'Wisconsin.': 1,\n",
91+
" 'With': 1,\n",
92+
" 'You': 1,\n",
93+
" 'a': 15,\n",
94+
" 'action.': 1,\n",
95+
" 'actresses': 1,\n",
96+
" 'actually': 1,\n",
97+
" 'all': 1,\n",
98+
" 'also': 1,\n",
99+
" 'although': 1,\n",
100+
" 'and': 5,\n",
101+
" 'another': 3,\n",
102+
" 'any': 1,\n",
103+
" 'are': 8,\n",
104+
" 'as': 3,\n",
105+
" 'at': 1,\n",
106+
" 'battle': 1,\n",
107+
" 'been': 1,\n",
108+
" 'begin': 1,\n",
109+
" 'below.': 1,\n",
110+
" 'boxes': 1,\n",
111+
" 'building.': 1,\n",
112+
" 'buildings': 1,\n",
113+
" 'but': 3,\n",
114+
" 'called': 1,\n",
115+
" 'cardboard': 1,\n",
116+
" 'careful': 1,\n",
117+
" 'cars': 1,\n",
118+
" 'catch': 1,\n",
119+
" 'chairs': 1,\n",
120+
" 'chairs,': 1,\n",
121+
" 'chairs.': 1,\n",
122+
" 'cliff': 1,\n",
123+
" 'country': 1,\n",
124+
" 'course': 1,\n",
125+
" 'covered': 1,\n",
126+
" 'crash': 2,\n",
127+
" 'crashes': 1,\n",
128+
" 'dangerous': 2,\n",
129+
" 'dead.': 1,\n",
130+
" 'dead\\xef\\xbc\\x81Of': 1,\n",
131+
" 'death': 1,\n",
132+
" 'decades': 1,\n",
133+
" 'degree': 1,\n",
134+
" 'depends': 2,\n",
135+
" 'destroy': 1,\n",
136+
" 'did': 2,\n",
137+
" 'died': 1,\n",
138+
" 'do': 3,\n",
139+
" 'dress': 1,\n",
140+
" 'edge': 1,\n",
141+
" 'empty': 1,\n",
142+
" 'enough.': 1,\n",
143+
" 'even': 3,\n",
144+
" 'example,': 3,\n",
145+
" 'experts': 1,\n",
146+
" 'explosion': 1,\n",
147+
" 'failed': 1,\n",
148+
" 'fall': 3,\n",
149+
" 'falls': 1,\n",
150+
" 'fast': 1,\n",
151+
" 'feet': 2,\n",
152+
" 'fight': 1,\n",
153+
" 'fight.': 1,\n",
154+
" 'film': 1,\n",
155+
" 'fire': 2,\n",
156+
" 'fire,': 1,\n",
157+
" 'fire.': 1,\n",
158+
" 'fires': 1,\n",
159+
" 'fists.': 1,\n",
160+
" 'for': 4,\n",
161+
" 'four': 1,\n",
162+
" 'from': 2,\n",
163+
" 'get': 1,\n",
164+
" 'glass': 1,\n",
165+
" 'goes': 1,\n",
166+
" 'ground': 2,\n",
167+
" 'half': 1,\n",
168+
" 'hard': 1,\n",
169+
" 'hard.': 1,\n",
170+
" 'has': 4,\n",
171+
" 'have': 1,\n",
172+
" 'having': 1,\n",
173+
" 'he': 5,\n",
174+
" 'heads': 1,\n",
175+
" 'high': 3,\n",
176+
" 'high.': 1,\n",
177+
" 'hit': 2,\n",
178+
" 'hitting': 1,\n",
179+
" 'hurt.': 1,\n",
180+
" 'in': 8,\n",
181+
" 'indifference': 1,\n",
182+
" 'injured,': 1,\n",
183+
" 'is': 6,\n",
184+
" \"isn't\": 2,\n",
185+
" 'it': 3,\n",
186+
" 'jump': 2,\n",
187+
" 'just': 2,\n",
188+
" 'kill': 1,\n",
189+
" 'killed.': 2,\n",
190+
" 'lead': 1,\n",
191+
" 'lives.': 1,\n",
192+
" 'living.': 1,\n",
193+
" 'longer': 3,\n",
194+
" 'luck': 1,\n",
195+
" 'made': 3,\n",
196+
" 'mattress': 1,\n",
197+
" 'men': 5,\n",
198+
" 'moment.': 1,\n",
199+
" 'money': 1,\n",
200+
" 'more': 1,\n",
201+
" 'most': 1,\n",
202+
" 'moving': 1,\n",
203+
" 'neither': 1,\n",
204+
" 'night': 1,\n",
205+
" 'no': 3,\n",
206+
" 'nor': 1,\n",
207+
" 'not': 2,\n",
208+
" 'nowadays': 1,\n",
209+
" 'of': 16,\n",
210+
" 'often': 1,\n",
211+
" 'on': 6,\n",
212+
" 'one': 5,\n",
213+
" 'only': 1,\n",
214+
" 'only.': 1,\n",
215+
" 'open,': 1,\n",
216+
" 'or': 2,\n",
217+
" 'out': 2,\n",
218+
" 'over': 2,\n",
219+
" 'paid': 1,\n",
220+
" 'parachute': 1,\n",
221+
" 'people,': 1,\n",
222+
" 'perform': 2,\n",
223+
" 'problem': 1,\n",
224+
" 'profession': 1,\n",
225+
" 'professionals.': 1,\n",
226+
" 'progress.': 1,\n",
227+
" 'rates': 1,\n",
228+
" 'really': 1,\n",
229+
" 'requires': 1,\n",
230+
" 'right': 1,\n",
231+
" 'risks,': 1,\n",
232+
" 's': 1,\n",
233+
" 'same': 1,\n",
234+
" 'say': 1,\n",
235+
" 'say,': 1,\n",
236+
" 'scene,': 1,\n",
237+
" 'screen.': 1,\n",
238+
" 'see': 1,\n",
239+
" 'seriously': 2,\n",
240+
" 'sides': 1,\n",
241+
" 'since': 1,\n",
242+
" 'skied': 1,\n",
243+
" 'skill': 1,\n",
244+
" 'so': 1,\n",
245+
" 'soft': 1,\n",
246+
" 'some': 3,\n",
247+
" 'sometimes': 1,\n",
248+
" 'soon': 1,\n",
249+
" 'sort,': 1,\n",
250+
" 'spite': 1,\n",
251+
" 'start': 1,\n",
252+
" 'still': 1,\n",
253+
" 'stuntgirls': 1,\n",
254+
" \"stuntman'\": 1,\n",
255+
" 'stuntman,': 1,\n",
256+
" 'stuntmen': 1,\n",
257+
" 'stuntmen.': 1,\n",
258+
" 'success': 1,\n",
259+
" 'successes,': 1,\n",
260+
" 'such': 1,\n",
261+
" 'sugar!But': 1,\n",
262+
" 'take': 1,\n",
263+
" 'technology,': 1,\n",
264+
" 'than': 2,\n",
265+
" 'that': 1,\n",
266+
" 'the': 23,\n",
267+
" 'their': 4,\n",
268+
" 'there': 1,\n",
269+
" 'they': 10,\n",
270+
" 'things': 1,\n",
271+
" 'thirty': 1,\n",
272+
" 'this': 3,\n",
273+
" 'thousand': 1,\n",
274+
" 'through': 2,\n",
275+
" 'timing.': 1,\n",
276+
" 'to': 8,\n",
277+
" 'tool.': 1,\n",
278+
" 'town': 1,\n",
279+
" 'training.': 1,\n",
280+
" 'trains,': 1,\n",
281+
" 'trick': 1,\n",
282+
" 'tricks.There': 1,\n",
283+
" 'two': 2,\n",
284+
" 'until': 1,\n",
285+
" 'up': 1,\n",
286+
" 'up\"': 1,\n",
287+
" 'was': 1,\n",
288+
" 'watching': 1,\n",
289+
" 'way': 1,\n",
290+
" 'ways,': 1,\n",
291+
" 'well': 1,\n",
292+
" 'when': 4,\n",
293+
" 'which': 1,\n",
294+
" 'who': 2,\n",
295+
" 'will': 1,\n",
296+
" 'window': 1,\n",
297+
" 'windows': 1,\n",
298+
" 'windows,': 1,\n",
299+
" 'with': 5,\n",
300+
" 'women': 1,\n",
301+
" 'wood': 1,\n",
302+
" 'work': 1,\n",
303+
" 'work,': 1,\n",
304+
" 'work.': 1,\n",
305+
" 'world.': 1,\n",
306+
" 'worst': 1,\n",
307+
" 'you': 1})"
308+
]
309+
},
310+
"execution_count": 4,
311+
"metadata": {},
312+
"output_type": "execute_result"
313+
}
314+
],
315+
"source": [
316+
"from collections import Counter\n",
317+
"#import re\n",
318+
"\n",
319+
"word_dict = Counter()\n",
320+
"# with will automaticlly close your file\n",
321+
"with open('text.txt', 'r') as file:\n",
322+
" for line in file:\n",
323+
" print \"line is: \\n\", line\n",
324+
" print \"line.split() is: \\n\", line.split()\n",
325+
" word_dict.update(line.split())\n",
326+
"word_dict"
327+
]
328+
},
329+
{
330+
"cell_type": "code",
331+
"execution_count": null,
332+
"metadata": {
333+
"collapsed": true
334+
},
335+
"outputs": [],
336+
"source": []
337+
}
338+
],
339+
"metadata": {
340+
"anaconda-cloud": {},
341+
"kernelspec": {
342+
"display_name": "Python [Root]",
343+
"language": "python",
344+
"name": "Python [Root]"
345+
},
346+
"language_info": {
347+
"codemirror_mode": {
348+
"name": "ipython",
349+
"version": 2
350+
},
351+
"file_extension": ".py",
352+
"mimetype": "text/x-python",
353+
"name": "python",
354+
"nbconvert_exporter": "python",
355+
"pygments_lexer": "ipython2",
356+
"version": "2.7.12"
357+
}
358+
},
359+
"nbformat": 4,
360+
"nbformat_minor": 0
361+
}

BrambleXu/play4/play4_counter.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# -*- coding:utf-8 -*-
2+
#**第 0004 题:**任一个英文的纯文本文件,统计其中的单词出现的个数。
3+
'''
4+
In this file, I want to count how many time each word showed in a text
5+
function:
6+
clean_file: input is the file path, output is a list contain every words in the file.
7+
A word may show many time.
8+
like this -> ['This', 'module', 'implements', 'specialized', 'container', 'datatypes', 'providing', 'alternatives', 'to', 'Python\xe2\x80\x99s', 'general', 'purpose', 'builtin', 'containers', 'dict', 'list', 'set', 'and', 'tuple', 'In', 'addition', 'to', 'the', 'concrete', 'container', 'classes', 'the', 'collections', 'module', 'provides', 'abstract', 'base', 'classes', 'that', 'can', 'be', 'used', 'to', 'test', 'whether', 'a', 'class', 'provides', 'a', 'particular', 'interface', 'for', 'example', 'whether', 'it', 'is', 'hashable', 'or', 'a', 'mapping', 'In', 'some', 'ways', 'the']
9+
10+
count_words: use Counter class to get a dict telling us how many time a word showed
11+
12+
'''
13+
14+
import string # used to clean the file
15+
from collections import Counter
16+
#import re
17+
18+
def count_words(file_path):
19+
processed_file = clean_file(file_path)
20+
#print processed_file
21+
word_dict = Counter(processed_file)
22+
print word_dict
23+
24+
def clean_file(file_path):
25+
with open(file_path, 'r') as f:
26+
#i = 0
27+
file_as_list = []
28+
for line in f.readlines():
29+
if line != "\n":
30+
#print i
31+
file_as_list.extend(line.strip().translate(None, string.punctuation).split().lower())
32+
return file_as_list
33+
34+
if __name__ == '__main__':
35+
counts = count_words('text.txt')
36+
#print "the number of words in the file is: %s" % counts
37+
#print "the number of words in the file is: %s" % counts.most_common(10)

0 commit comments

Comments
 (0)