+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Solution 10\n",
+ "Keep two lists: a list of pending duplicates, and a list of actual duplicates. The first time we see a letter, add it to the pending list. If we see the letter again, it's already in the pending list, so that prompts us to add it to the duplicates list."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def duplicates_via_pending_list(word):\n",
+ " pending = []\n",
+ " duplicates = []\n",
+ " for letter in word:\n",
+ " if letter in pending:\n",
+ " if letter not in duplicates:\n",
+ " duplicates.append(letter)\n",
+ " else:\n",
+ " pending.append(letter)\n",
+ " return duplicates"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 27
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "duplicates_via_pending_list('occurrences')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 28,
+ "text": [
+ "['c', 'r', 'e']"
+ ]
+ }
+ ],
+ "prompt_number": 28
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Solution 11\n",
+ "Exactly the same as above, but using sets instead of lists. This means we don't have to worry about adding more than one copy of the letter to either `pending` or `duplicates`."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "def duplicates_via_pending_set(word):\n",
+ " pending = set()\n",
+ " duplicates = set()\n",
+ " for letter in word:\n",
+ " if letter in pending:\n",
+ " duplicates.add(letter)\n",
+ " pending.add(letter)\n",
+ " return list(duplicates)"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [],
+ "prompt_number": 31
+ },
+ {
+ "cell_type": "code",
+ "collapsed": false,
+ "input": [
+ "duplicates_via_pending_set('occurrences')"
+ ],
+ "language": "python",
+ "metadata": {},
+ "outputs": [
+ {
+ "metadata": {},
+ "output_type": "pyout",
+ "prompt_number": 32,
+ "text": [
+ "['c', 'r', 'e']"
+ ]
+ }
+ ],
+ "prompt_number": 32
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "##Solution 12\n",
+ "Creating a set removes all the duplicates. Make a set from the word, then remove each letter in the set from the word. Whatever letters are left are the duplicates. (Convert the duplicates to a set then to a list to eliminate letters that occur more than three times.)"
+ ]
+ },