{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Copyright 2014 Brett Slatkin, Pearson Education Inc.\n", "#\n", "# Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# http://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License.\n", "\n", "# Preamble to mimick book environment\n", "import logging\n", "from pprint import pprint\n", "from sys import stdout as STDOUT" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 1\n", "def palindrome(word):\n", " \"\"\"Return True if the given word is a palindrome.\"\"\"\n", " return word == word[::-1]\n", "\n", "assert palindrome('tacocat')\n", "assert not palindrome('banana')" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "'Return True if the given word is a palindrome.'\n" ] } ], "source": [ "# Example 2\n", "print(repr(palindrome.__doc__))" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/plain": [ "\"Library for testing words for various linguistic patterns.\\n\\nTesting how words relate to each other can be tricky sometimes!\\nThis module provides easy ways to determine when words you've\\nfound have special properties.\\n\\nAvailable functions:\\n- palindrome: Determine if a word is a palindrome.\\n- check_anagram: Determine if two words are anagrams.\\n...\\n\"" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Example 3\n", "\"\"\"Library for testing words for various linguistic patterns.\n", "\n", "Testing how words relate to each other can be tricky sometimes!\n", "This module provides easy ways to determine when words you've\n", "found have special properties.\n", "\n", "Available functions:\n", "- palindrome: Determine if a word is a palindrome.\n", "- check_anagram: Determine if two words are anagrams.\n", "...\n", "\"\"\"" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 4\n", "class Player(object):\n", " \"\"\"Represents a player of the game.\n", "\n", " Subclasses may override the 'tick' method to provide\n", " custom animations for the player's movement depending\n", " on their power level, etc.\n", "\n", " Public attributes:\n", " - power: Unused power-ups (float between 0 and 1).\n", " - coins: Coins found during the level (integer).\n", " \"\"\"" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Example 5\n", "import itertools\n", "def find_anagrams(word, dictionary):\n", " \"\"\"Find all anagrams for a word.\n", "\n", " This function only runs as fast as the test for\n", " membership in the 'dictionary' container. It will\n", " be slow if the dictionary is a list and fast if\n", " it's a set.\n", "\n", " Args:\n", " word: String of the target word.\n", " dictionary: Container with all strings that\n", " are known to be actual words.\n", "\n", " Returns:\n", " List of anagrams that were found. Empty if\n", " none were found.\n", " \"\"\"\n", " permutations = itertools.permutations(word, len(word))\n", " possible = (''.join(x) for x in permutations)\n", " found = {word for word in possible if word in dictionary}\n", " return list(found)\n", "\n", "assert find_anagrams('pancakes', ['scanpeak']) == ['scanpeak']" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.5.1" } }, "nbformat": 4, "nbformat_minor": 0 }