{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Class practice: strings, lists & numbers\n", "\n", "These were copied and slightly modified from [Paul Waddell's Urban Informatics and Visualization](https://github.com/waddell/urban-informatics-and-visualization). All credit goes to him, all mistakes are mine." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Exercises | String & List" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Reversing numbers" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write code that reverses even numbers from 0 to 100 (including 100) and print the result. \n" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "collapsed": true }, "outputs": [], "source": [ "end = 100001" ] }, { "cell_type": "code", "execution_count": 34, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2.62 ms ± 74.1 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" ] } ], "source": [ "%%timeit\n", "a = list(range(end))\n", "b = a[::-2]\n", "#print(b)" ] }, { "cell_type": "code", "execution_count": 35, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "8.61 ms ± 332 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)\n" ] } ], "source": [ "%%timeit\n", "a = []\n", "for i in range(end):\n", " if i % 2 ==0:\n", " a.append(i)\n", "b = a[::-1]\n", "#print(b)" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "725 µs ± 12.2 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)\n" ] } ], "source": [ "%%timeit\n", "a = list(range(end-1, -1, -2))\n", "#print(a)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List manipulations" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### We have two lists, a and b, a=[10,20,30] b=[30,60,90]. Write code that give us the following outputs: [LIST]\n", "\n", "a. [[10,20,30],[30,60,90]]\n", "\n", "b. [10,20,30,30,60,90]\n", "\n", "c. [10,20,60,90] (first two of a, last two of b)\n", "\n", "d. [20,40,60] (the element-wise differences between b and a)" ] }, { "cell_type": "code", "execution_count": 51, "metadata": { "collapsed": true }, "outputs": [], "source": [ "n = 100_000\n", "a=list(range(n))\n", "b=list(range(n))\n" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 1 0 LOAD_CONST 0 ( at 0x106ec2a50, file \"\", line 1>)\n", " 2 LOAD_CONST 1 ('')\n", " 4 MAKE_FUNCTION 0\n", " 6 LOAD_NAME 0 (range)\n", " 8 LOAD_CONST 2 (0)\n", " 10 LOAD_NAME 1 (len)\n", " 12 LOAD_NAME 2 (a)\n", " 14 CALL_FUNCTION 1\n", " 16 CALL_FUNCTION 2\n", " 18 GET_ITER\n", " 20 CALL_FUNCTION 1\n", " 22 STORE_NAME 3 (pd)\n", " 24 LOAD_CONST 3 (None)\n", " 26 RETURN_VALUE\n" ] } ], "source": [ "import dis\n", "dis.dis(\"pd = [b[i]-a[i] for i in range(0,len(a))]\")" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 2 0 BUILD_LIST 0\n", " 2 STORE_NAME 0 (l)\n", "\n", " 3 4 SETUP_LOOP 42 (to 48)\n", " 6 LOAD_NAME 1 (range)\n", " 8 LOAD_NAME 2 (len)\n", " 10 LOAD_NAME 3 (a)\n", " 12 CALL_FUNCTION 1\n", " 14 CALL_FUNCTION 1\n", " 16 GET_ITER\n", " >> 18 FOR_ITER 26 (to 46)\n", " 20 STORE_NAME 4 (i)\n", "\n", " 4 22 LOAD_NAME 0 (l)\n", " 24 LOAD_ATTR 5 (append)\n", " 26 LOAD_NAME 6 (b)\n", " 28 LOAD_NAME 4 (i)\n", " 30 BINARY_SUBSCR\n", " 32 LOAD_NAME 3 (a)\n", " 34 LOAD_NAME 4 (i)\n", " 36 BINARY_SUBSCR\n", " 38 BINARY_SUBTRACT\n", " 40 CALL_FUNCTION 1\n", " 42 POP_TOP\n", " 44 JUMP_ABSOLUTE 18\n", " >> 46 POP_BLOCK\n", " >> 48 LOAD_CONST 0 (None)\n", " 50 RETURN_VALUE\n" ] } ], "source": [ "dis.dis(\"\"\"\n", "l = []\n", "for i in range(len(a)):\n", " l.append(b[i] - a[i])\n", "\"\"\")" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 2, 4, 6, 8]" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[x for x in range(10) if x%2==0]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### List insertions\n", "\n", "**Write code that add the name \"Norah\" to the following list, after the name \"Michael\".**\n", "\n", "Make sure your code continues to do the right thing if more names are added to the list, or if the list is reordered, or if you need to find Jessica instead of Michael (or anyone else on the list).\n", "\n", "names: Akshara, Anna, Aqshems, Chester, Echo, James, Jessica, Matthew, Michael, Philip, Sarah" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['Akshara', 'Anna', 'Aqshems', 'Chester', 'Echo', 'James', 'Jessica', 'Matthew', 'Michael', 'Norah', 'Philip', 'Sarah']\n", "['Akshara', 'Anna', 'Aqshems', 'Chester', 'Echo', 'James', 'Jessica', 'Lance', 'Matthew', 'Michael', 'Norah', 'Philip', 'Sarah']\n", "['Akshara', 'Anna', 'Aqshems', 'Chester', 'Echo', 'James', 'Jessica', 'Lance', 'Matthew', 'Michael', 'Norah', 'Philip', 'Sarah']\n" ] } ], "source": [ "names = [\"Akshara\", \"Anna\", \"Aqshems\", \"Chester\", \"Echo\", \"James\", \"Jessica\", \"Matthew\", \"Michael\", \"Philip\", \"Sarah\"]\n", "\n", "def add_after(name_list, name_to_add, name_to_after):\n", " \"\"\"Add a name to a list, at the specified position.\n", " \n", " Adds....\n", " ....\n", " \n", " \"\"\"\n", " names = name_list\n", " for i, name in enumerate(names):\n", " if name == name_to_after:\n", " names.insert(i+1,name_to_add)\n", " break\n", " return names\n", "\n", "print(add_after(names, \"Norah\", \"Michael\"))\n", "print(add_after(names, \"Lance\", \"Jessica\"))\n", "print(names)" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Akshara',\n", " 'Anna',\n", " 'Aqshems',\n", " 'Chester',\n", " 'Echo',\n", " 'James',\n", " 'Jessica',\n", " 'Matthew',\n", " 'Michael',\n", " 'Norah',\n", " 'Philip',\n", " 'Sarah']" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "name = [\"Akshara\", \"Anna\", \"Aqshems\", \"Chester\", \"Echo\", \"James\", \"Jessica\", \"Matthew\", \"Michael\", \"Philip\", \"Sarah\"]\n", "name.insert(name.index('Michael')+1,\"Norah\")\n", "name" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Maximizing a sum" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**Find a list in the following list (G) whose sum of its elements is the highest.**\n", "\n", "G = [[13,9,8], [14,6,12], [10,13,11], [7,18,9]]" ] }, { "cell_type": "code", "execution_count": 23, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Type your code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Cars and brown trucks\n", "\n", "Write code that prints all colors in the list and the word 'car', one per line, **unless** the color is brown, when you should print 'truck' instead:\n", "\n", "```python\n", "colors = ['red', 'black', 'gray', 'brown', 'blue', 'white']\n", "```" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Type your code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Numbers\n", "\n", "### Reversing numbers\n", "\n", "Write a function nums_reversed that takes in an integer `n` and returns a string containing the numbers 1 through `n` including `n` in reverse order, separated\n", "by spaces. For example:\n", "\n", " >>> nums_reversed(5)\n", " '5 4 3 2 1'" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Type your code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Divisibility\n", "\n", "Write a program which will find all such numbers which are divisible by 7 but are not a multiple of 5,\n", "between 1000 and 1200 (both included).\n", "The numbers obtained should be printed in a comma-separated sequence on a single line.\n", "\n", "*Hint:* Consider using `range(#begin, #end)`." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Type your code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Write the same program but this time use while loop instead of for loop" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Type your code here" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Double trouble\n", "\n", "Write a function `double100` that takes in a list of integers\n", "and returns `True` only if the list has two `100`s next to each other.\n", "\n", " >>> double100([100, 2, 3, 100])\n", " False\n", " >>> double100([2, 3, 100, 100, 5])\n", " True" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": true }, "outputs": [], "source": [ "# Type your code here" ] } ], "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.6.3" } }, "nbformat": 4, "nbformat_minor": 2 }