Skip to content

Commit

Permalink
clean up, more consistent questioning
Browse files Browse the repository at this point in the history
  • Loading branch information
fbkarsdorp committed Jul 18, 2014
1 parent 3b6f76a commit 9173393
Showing 1 changed file with 23 additions and 32 deletions.
55 changes: 23 additions & 32 deletions Chapter 2 - First steps.ipynb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"metadata": {
"name": "",
"signature": "sha256:61dd41f72d99884905e5394954e7e29cc07c4cb761156899ccb69a4ccd4146bd"
"signature": "sha256:e373d55e4ef72c3bc815b25ea9282fdbb5e8ccfcb570fa386b1b62394e68496b"
},
"nbformat": 3,
"nbformat_minor": 0,
Expand Down Expand Up @@ -274,7 +274,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"Lists in Python do not have a function `count` with which we can count how often a particular object occurs in a list. All the things you have learnt so far should enable you to write code that counts how often a certain items occurs in a list. Write some code that defines the variable `number_of_hits` and counts how often the word *in* (assigned to `item_to_count`) occurs in the the list of words called `words`."
"All the things you have learnt so far should enable you to write code that counts how often a certain items occurs in a list. Write some code that defines the variable `number_of_hits` and counts how often the word *in* (assigned to `item_to_count`) occurs in the the list of words called `words`."
]
},
{
Expand Down Expand Up @@ -380,12 +380,12 @@
"cell_type": "code",
"collapsed": false,
"input": [
"def count_in_list(item_to_count, list_to_search): # 1\n",
" number_of_hits = 0 # 2\n",
" for item in list_to_search: # 3\n",
" if item == item_to_count: # 4\n",
" number_of_hits += 1 # 5\n",
" return number_of_hits # 6"
"def count_in_list(item_to_count, list_to_search): \n",
" number_of_hits = 0 \n",
" for item in list_to_search: \n",
" if item == item_to_count: \n",
" number_of_hits += 1 \n",
" return number_of_hits "
],
"language": "python",
"metadata": {},
Expand Down Expand Up @@ -533,14 +533,14 @@
"cell_type": "code",
"collapsed": false,
"input": [
"def counter(list_to_search): # 1\n",
" counts = {} # 2\n",
" for word in list_to_search: # 3\n",
" if word in counts: # 4\n",
" counts[word] = counts[word] + 1 # 5\n",
" else: # 6\n",
" counts[word] = 1 # 7\n",
" return counts # 8"
"def counter(list_to_search): \n",
" counts = {} \n",
" for word in list_to_search: \n",
" if word in counts: \n",
" counts[word] = counts[word] + 1 \n",
" else: \n",
" counts[word] = 1 \n",
" return counts "
],
"language": "python",
"metadata": {},
Expand Down Expand Up @@ -748,7 +748,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"*Write your answer here* (double click me)"
"*Double click this cell and write down your answer.*"
]
},
{
Expand Down Expand Up @@ -968,7 +968,6 @@
"input": [
"def clean_text(text):\n",
" # insert your code here\n",
" return remove_punc(text.lower())\n",
" \n",
"# The following test should print True if your code is correct \n",
"short_text = \"Commas, as it turns out, are overestimated. Dots, however, even more so!\"\n",
Expand All @@ -992,13 +991,6 @@
"input": [
"woodhouse_counts = 0\n",
"# insert your code here\n",
"infile = open(\"data/austen-emma.txt\")\n",
"text = infile.read()\n",
"infile.close()\n",
"text = clean_text(text)\n",
"text = text.split()\n",
"word_counts = counter(text)['woodhouse']\n",
"woodhouse_counts = word_counts\n",
"\n",
"# The following test should print True if your code is correct \n",
"print(woodhouse_counts == 263)"
Expand Down Expand Up @@ -1076,21 +1068,20 @@
"input": [
"# first open and read data/austen-emma.txt. Don't forget to close the infile\n",
"infile = open(\"data/austen-emma.txt\")\n",
"text = infile.read()\n",
"infile.close()\n",
"text = clean_text(text)\n",
"text = # read the contents of the infile\n",
"# close the file handler\n",
"# clean the text\n",
"\n",
"# next compute the frequency distribution using the function counter\n",
"frequency_distribution = counter(text.split())\n",
"frequency_distribution = \n",
"\n",
"# now open the file data/austen-frequency-distribution.txt for writing\n",
"outfile = open(\"data/austen-frequency-distribution.txt\", mode = 'w')\n",
"outfile = \n",
"\n",
"for word, frequency in frequency_distribution.items():\n",
" outfile.write(word + \";\" + str(frequency) + '\\n')\n",
" \n",
"# close the outfile\n",
"outfile.close()"
"# close the outfile"
],
"language": "python",
"metadata": {},
Expand Down

0 comments on commit 9173393

Please sign in to comment.