{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Q 1 (function practice)\n", "\n", "Let's practice functions. Here's a simple function that takes a string and returns a list of all the 4 letter words:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def four_letter_words(message):\n", " words = message.split()\n", " four_letters = [w for w in words if len(w) == 4]\n", " return four_letters" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "message = \"The quick brown fox jumps over the lazy dog\"\n", "print(four_letter_words(message))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write a version of this function that takes a second argument, n, that is the word length we want to search for" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q 2 (primes)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "A prime number is divisible only by 1 and itself. We want to write a function that takes a positive integer, n, and finds all of the primes up to that number.\n", "\n", "A simple (although not very fast) way to find the primes is to start at 1, and build a list of primes by checking if the current number is divisible by any of the previously found primes. If it is not divisible by any earlier primes, then it is a prime.\n", "\n", "The modulus operator, `%` could be helpful here." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q 3 (exceptions for error handling)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We want to safely convert a string into a float, int, or leave it as a string, depending on its contents. As we've already seen, python provides `float()` and `int()` functions for this:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = \"2.0\"\n", "b = float(a)\n", "print(b, type(b))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "But these throw exceptions if the conversion is not possible" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = \"this is a string\"\n", "b = float(a)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = \"1.2345\"\n", "b = int(a)\n", "print(b, type(b))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "b = float(a)\n", "print(b, type(b))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Notice that an int can be converted to a float, but if you convert a float to an int, you rise losing significant digits. A string cannot be converted to either.\n", "\n", "### your task\n", "\n", "Write a function, `convert_type(a)` that takes a string `a`, and converts it to a float if it is a number with a decimal point, an int if it is an integer, or leaves it as a string otherwise, and returns the result. You'll want to use exceptions to prevent the code from aborting." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Q 4 (tic-tac-toe)\n", "\n", "Here we'll write a simple tic-tac-toe game that 2 players can play. First we'll create a string that represents our game board:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "board = \"\"\"\n", " {s1:^3} | {s2:^3} | {s3:^3}\n", "-----+-----+-----\n", " {s4:^3} | {s5:^3} | {s6:^3}\n", "-----+-----+----- 123\n", " {s7:^3} | {s8:^3} | {s9:^3} 456\n", " 789 \n", "\"\"\"" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "This board will look a little funny if we just print it—the spacing is set to look right when we replace the `{}` with `x` or `o`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(board)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "and well use a dictionary to denote the status of each square, \"x\", \"o\", or empty, \"\"" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "play = {}\n", "\n", "def initialize_board(play):\n", " for n in range(9):\n", " play[\"s{}\".format(n+1)] = \"\"\n", "\n", "initialize_board(play)\n", "play" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Note that our `{}` placeholders in the `board` string have identifiers (the numbers in the `{}`). We can use these to match the variables we want to print to the placeholder in the string, regardless of the order in the `format()`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "a = \"{s1:} {s2:}\".format(s2=1, s1=2)\n", "a" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here's an easy way to add the values of our dictionary to the appropriate squares in our game board. First note that each of the {} is labeled with a number that matches the keys in our dictionary. Python provides a way to unpack a dictionary into labeled arguments, using **\n", "\n", "This lets us to write a function to show the tic-tac-toe board." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def show_board(play):\n", " \"\"\" display the playing board. We take a dictionary with the current state of the board\n", " We rely on the board string to be a global variable\"\"\"\n", " print(board.format(**play))\n", " \n", "show_board(play)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Now we need a function that asks a player for a move:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def get_move(n, xo, play):\n", " \"\"\" ask the current player, n, to make a move -- make sure the square was not \n", " already played. xo is a string of the character (x or o) we will place in\n", " the desired square \"\"\"\n", " valid_move = False\n", " while not valid_move:\n", " idx = input(\"player {}, enter your move (1-9)\".format(n))\n", " if play[\"s{}\".format(idx)] == \"\":\n", " valid_move = True\n", " else:\n", " print(\"invalid: {}\".format(play[\"s{}\".format(idx)]))\n", " \n", " play[\"s{}\".format(idx)] = xo" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "help(get_move)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### your task\n", "\n", "Using the functions defined above,\n", " * `initialize_board()`\n", " * `show_board()`\n", " * `get_move()`\n", "\n", "fill in the function `play_game()` below to complete the game, asking for the moves one at a time, alternating between player 1 and 2" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def play_game():\n", " \"\"\" play a game of tic-tac-toe \"\"\"\n", " " ] } ], "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.4.2" } }, "nbformat": 4, "nbformat_minor": 2 }