Skip to content
This repository was archived by the owner on Jun 25, 2018. It is now read-only.

Commit b970818

Browse files
committed
Add recap exercise at the beginning of day2 solve issue #14
1 parent 09d47c1 commit b970818

File tree

2 files changed

+143
-11
lines changed

2 files changed

+143
-11
lines changed

Diff for: Introduction_to_python_day_2_introduction.ipynb

+77-11
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
},
8484
{
8585
"cell_type": "code",
86-
"execution_count": 1,
86+
"execution_count": 43,
8787
"metadata": {
8888
"collapsed": false,
8989
"slideshow": {
@@ -109,7 +109,7 @@
109109
},
110110
{
111111
"cell_type": "code",
112-
"execution_count": 2,
112+
"execution_count": 44,
113113
"metadata": {
114114
"collapsed": false,
115115
"slideshow": {
@@ -141,7 +141,7 @@
141141
},
142142
{
143143
"cell_type": "code",
144-
"execution_count": 3,
144+
"execution_count": 45,
145145
"metadata": {
146146
"collapsed": false,
147147
"slideshow": {
@@ -169,7 +169,7 @@
169169
},
170170
{
171171
"cell_type": "code",
172-
"execution_count": 4,
172+
"execution_count": 46,
173173
"metadata": {
174174
"collapsed": false,
175175
"slideshow": {
@@ -193,7 +193,7 @@
193193
},
194194
{
195195
"cell_type": "code",
196-
"execution_count": 5,
196+
"execution_count": 47,
197197
"metadata": {
198198
"collapsed": false,
199199
"slideshow": {
@@ -205,7 +205,7 @@
205205
"name": "stdout",
206206
"output_type": "stream",
207207
"text": [
208-
"A dictionary: {'C': 'Cytosine', 'T': 'Thymine', 'G': 'Guanine', 'A': 'Adenine'}\n",
208+
"A dictionary: {'G': 'Guanine', 'T': 'Thymine', 'A': 'Adenine', 'C': 'Cytosine'}\n",
209209
"Value associated to key C: Cytosine\n"
210210
]
211211
}
@@ -233,7 +233,7 @@
233233
},
234234
{
235235
"cell_type": "code",
236-
"execution_count": 6,
236+
"execution_count": 48,
237237
"metadata": {
238238
"collapsed": false
239239
},
@@ -272,7 +272,7 @@
272272
},
273273
{
274274
"cell_type": "code",
275-
"execution_count": 7,
275+
"execution_count": 49,
276276
"metadata": {
277277
"collapsed": false,
278278
"slideshow": {
@@ -302,7 +302,7 @@
302302
},
303303
{
304304
"cell_type": "code",
305-
"execution_count": 8,
305+
"execution_count": 50,
306306
"metadata": {
307307
"collapsed": false,
308308
"slideshow": {
@@ -332,7 +332,7 @@
332332
},
333333
{
334334
"cell_type": "code",
335-
"execution_count": 9,
335+
"execution_count": 51,
336336
"metadata": {
337337
"collapsed": false,
338338
"slideshow": {
@@ -379,7 +379,7 @@
379379
},
380380
{
381381
"cell_type": "code",
382-
"execution_count": 10,
382+
"execution_count": 52,
383383
"metadata": {
384384
"collapsed": false
385385
},
@@ -399,6 +399,72 @@
399399
"print(\"ATG TCA CCG GGC\".split())"
400400
]
401401
},
402+
{
403+
"cell_type": "markdown",
404+
"metadata": {
405+
"slideshow": {
406+
"slide_type": "slide"
407+
}
408+
},
409+
"source": [
410+
"## Exercise 2.0.1\n",
411+
"\n",
412+
"- Create a string variable with the lyrics of Imagine by John Lennon, 1971. Split into words. Print the total number of words, and the number of unique words. Calculate the frequency of each word and store the result into a dictionary. Print each unique word along with its frequency. Find the most frequent word in the song, print it with its frequency.\n",
413+
"\n",
414+
"<center><img src=\"https://upload.wikimedia.org/wikipedia/en/1/1d/John_Lennon_-_Imagine_John_Lennon.jpg\"/></center>"
415+
]
416+
},
417+
{
418+
"cell_type": "code",
419+
"execution_count": 53,
420+
"metadata": {
421+
"collapsed": false,
422+
"slideshow": {
423+
"slide_type": "subslide"
424+
}
425+
},
426+
"outputs": [],
427+
"source": [
428+
"lyrics = \"\"\"\n",
429+
"Imagine there's no Heaven\n",
430+
"It's easy if you try\n",
431+
"No Hell below us\n",
432+
"Above us only sky\n",
433+
"\n",
434+
"Imagine all the people\n",
435+
"Living for today\n",
436+
"Aaa haa\n",
437+
"\n",
438+
"Imagine there's no countries\n",
439+
"It isn't hard to do\n",
440+
"Nothing to kill or die for\n",
441+
"And no religion too\n",
442+
"\n",
443+
"Imagine all the people\n",
444+
"Living life in peace\n",
445+
"Yoo hoo\n",
446+
"\n",
447+
"You may say I'm a dreamer\n",
448+
"But I'm not the only one\n",
449+
"I hope someday you'll join us\n",
450+
"And the world will be as one\n",
451+
"\n",
452+
"Imagine no possessions\n",
453+
"I wonder if you can\n",
454+
"No need for greed or hunger\n",
455+
"A brotherhood of man\n",
456+
"\n",
457+
"Imagine all the people\n",
458+
"Sharing all the world\n",
459+
"Yoo hoo\n",
460+
"\n",
461+
"You may say I'm a dreamer\n",
462+
"But I'm not the only one\n",
463+
"I hope someday you'll join us\n",
464+
"And the world will live as one\n",
465+
"\"\"\""
466+
]
467+
},
402468
{
403469
"cell_type": "markdown",
404470
"metadata": {

Diff for: solutions/ex2_0_1.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
lyrics = """
2+
Imagine there's no Heaven
3+
It's easy if you try
4+
No Hell below us
5+
Above us only sky
6+
7+
Imagine all the people
8+
Living for today
9+
Aaa haa
10+
11+
Imagine there's no countries
12+
It isn't hard to do
13+
Nothing to kill or die for
14+
And no religion too
15+
16+
Imagine all the people
17+
Living life in peace
18+
Yoo hoo
19+
20+
You may say I'm a dreamer
21+
But I'm not the only one
22+
I hope someday you'll join us
23+
And the world will be as one
24+
25+
Imagine no possessions
26+
I wonder if you can
27+
No need for greed or hunger
28+
A brotherhood of man
29+
30+
Imagine all the people
31+
Sharing all the world
32+
Yoo hoo
33+
34+
You may say I'm a dreamer
35+
But I'm not the only one
36+
I hope someday you'll join us
37+
And the world will live as one
38+
"""
39+
40+
# Split into words
41+
words = lyrics.split()
42+
# Print the total number of words
43+
print('There are', len(words), 'words in this song.')
44+
45+
# Print the number of unique words
46+
unique_words = set(words)
47+
print('There are', len(unique_words), 'unique ones.')
48+
49+
# Calculate the frequency of each word and store the result into a dictionary
50+
results = {}
51+
for w in unique_words:
52+
results[w.lower()] = lyrics.count(w)
53+
54+
# Print each unique word along with its frequency
55+
for r in results:
56+
print(results[r], '\ttimes for word:', r)
57+
58+
# Find the most frequent word in the song
59+
most_frequent = 0
60+
for r in results:
61+
if results[r] > most_frequent:
62+
most_frequent = results[r]
63+
most_frequent_word = r
64+
65+
# Print the most frequent word with its frequency
66+
print(most_frequent_word, 'is the most frequent word being used', most_frequent, 'times.')

0 commit comments

Comments
 (0)