From 9173393ff773ef75cfe51054d663aa38127b66ef Mon Sep 17 00:00:00 2001 From: Folgert Karsdorp Date: Fri, 18 Jul 2014 09:32:52 +0200 Subject: [PATCH] clean up, more consistent questioning --- Chapter 2 - First steps.ipynb | 55 +++++++++++++++-------------------- 1 file changed, 23 insertions(+), 32 deletions(-) diff --git a/Chapter 2 - First steps.ipynb b/Chapter 2 - First steps.ipynb index c9187c0..97d330d 100644 --- a/Chapter 2 - First steps.ipynb +++ b/Chapter 2 - First steps.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:61dd41f72d99884905e5394954e7e29cc07c4cb761156899ccb69a4ccd4146bd" + "signature": "sha256:e373d55e4ef72c3bc815b25ea9282fdbb5e8ccfcb570fa386b1b62394e68496b" }, "nbformat": 3, "nbformat_minor": 0, @@ -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`." ] }, { @@ -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": {}, @@ -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": {}, @@ -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.*" ] }, { @@ -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", @@ -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)" @@ -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": {},