{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 8 Lists\n", "\"Open\n", "\n", "- http://openbookproject.net/thinkcs/python/english3e/lists.html\n", "\n", "## Topics\n", "- list data structure\n", "- syntax to create lists\n", "- methods or operations provided to list objects\n", "- list operators\n", "- list traversal\n", "- list applications and problems\n", "\n", "## 8.1 Lists\n", "- a type of sequence or container\n", "- ordered collection of values called elements or items\n", "- lists are similar to strings (ordered collections of characters) except that elements of a list can be of any type" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.2 Creating lists\n", "- several ways; the simplest is to enclose the elements in square brackets [ ]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [], "source": [ "alist = [] # an empty list" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [], "source": [ "blist = list() # an empty list" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "list" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "type(alist)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [], "source": [ "# creating lists with some elements of same type\n", "list1 = [10, 20, 30, 40]\n", "list2 = ['spam', 'bungee', 'swallow']" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [], "source": [ "# lists with elements of different types\n", "list3 = [\"hello\", 2.0, 10, [10, ('hi', 'world'), 3.5], (1, 'uno')]" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['hello', 2.0, 10, [10, ('hi', 'world'), 3.5], (1, 'uno')]\n" ] } ], "source": [ "# print list\n", "print(list3)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [], "source": [ "# quickly create a list of range of numbers between 1 and 19\n", "list4 = list(range(1, 20, 1))" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]\n" ] } ], "source": [ "print(list4)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[] [10, 20, 30, 40] ['spam', 'bungee', 'swallow'] ['hello', 2.0, 10, [10, ('hi', 'world'), 3.5], (1, 'uno')]\n" ] } ], "source": [ "# print multiple lists\n", "print(alist, list1, list2, list3)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "# Exercise: create a list of even numbers between 1 and 20 inclusive" ] }, { "cell_type": "code", "execution_count": 12, "metadata": {}, "outputs": [], "source": [ "# Exercise: create a list of odd numbers between 1 and 20 inclusive" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [], "source": [ "# Exercise: create a list of numbers from 20 to 1 inclusive" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.3 Accessing elements\n", "- same syntax for accessing characters of a string, the index operator: ['index']" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[10, 20, 30, 40]" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# let's see what elements are in list1\n", "list1" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "10" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# access an element, which one?\n", "list1[0]" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['hello', 2.0, 10, [10, ('hi', 'world'), 3.5], (1, 'uno')]" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list3" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'hello'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list3[2-2]" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n" ] } ], "source": [ "# list index can be variable as well\n", "index = 0\n", "print(list3[index])" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "list indices must be integers or slices, not float", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# can you use float value as an index?\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mlist3\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1.0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: list indices must be integers or slices, not float" ] } ], "source": [ "# can you use float value as an index?\n", "list3[1.0]" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "5" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# how many elements are there in list3?\n", "len(list3)" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "list index out of range", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# what happens if you access an index equal to the size of the list\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mlist3\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m5\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: list index out of range" ] } ], "source": [ "# what happens if you access an index equal to the size of the list\n", "list3[5]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['hello', 2.0, 10, [10, ('hi', 'world'), 3.5], (1, 'uno')]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list3" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [], "source": [ "# Exercise: access and print the last element of list3" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 'uno')\n" ] } ], "source": [ "# Can we use negative index?\n", "# Can you guess the output of the following code?\n", "print(list3[-1])" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "# Exercise - access and print 'world' in list3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.4 Membership\n", "- checking if some data/object is a member/element in the given list\n", "- **in** and **not in** boolean operators let's you check for membership" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "horsemen = [\"war\", \"famine\", \"pestilence\", [\"death\"]]\n", "'death' in horsemen" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "'War' not in horsemen" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[\"death\"] in horsemen" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.5 Traversing lists\n", "- for or while loop can be used to traverse through each element of a list" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['hello', 2.0, 10, [10, ('hi', 'world'), 3.5], (1, 'uno')]" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list3" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n", "2.0\n", "10\n", "[10, ('hi', 'world'), 3.5]\n", "(1, 'uno')\n" ] } ], "source": [ "# common technique; use for loop\n", "for item in list3:\n", " print(item)" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "hello\n", "2.0\n", "10\n", "10\n", "('hi', 'world')\n", "3.5\n", "1\n", "uno\n" ] } ], "source": [ "for item in list3:\n", " if isinstance(item, list) or isinstance(item, tuple):\n", " for l in item:\n", " print(l)\n", " else:\n", " print(item)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "war\n", "famine\n", "pestilence\n", "death\n" ] } ], "source": [ "horsemen = [\"war\", \"famine\", \"pestilence\", \"death\"]\n", "for i in [0, 1, 2, 3]:\n", " print(horsemen[i])\n", "# better way to do the same thing?" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "traversing using indices\n", "war\n", "famine\n", "pestilence\n", "death\n" ] } ], "source": [ "print(\"traversing using indices\")\n", "for i in range(len(horsemen)):\n", " print(horsemen[i])" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "traversing each element\n", "war\n", "famine\n", "pestilence\n", "death\n" ] } ], "source": [ "print('traversing each element')\n", "for ele in horsemen:\n", " print(ele)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.6 list operators\n", "- \\+ operator concatenates two lists and gives a bigger list\n", "- \\* operator repeats a list elements a given number of times" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['spam', 'bungee', 'swallow']" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list2" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['hello', 2.0, 10, [10, ('hi', 'world'), 3.5], (1, 'uno')]" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list3" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [], "source": [ "list4 = list2 + list3" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['spam',\n", " 'bungee',\n", " 'swallow',\n", " 'hello',\n", " 2.0,\n", " 10,\n", " [10, ('hi', 'world'), 3.5],\n", " (1, 'uno')]" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "list4" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[0]*10" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [], "source": [ "a = [1, 2, 3]*4" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "a" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [], "source": [ "b = [a]*3" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3],\n", " [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3],\n", " [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]]" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [], "source": [ "# 2-D list or matrix\n", "matrix = [[1, 2], [3, 4], [5, 6]]" ] }, { "cell_type": "code", "execution_count": 43, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[1, 2], [3, 4], [5, 6]]\n" ] } ], "source": [ "print(matrix)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1, 2], [3, 4], [5, 6]]" ] }, "execution_count": 44, "metadata": {}, "output_type": "execute_result" } ], "source": [ "matrix" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [], "source": [ "# How do you replace 5 with 50 in matrix?\n", "matrix[2][0] = 50" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[[1, 2], [3, 4], [50, 6]]" ] }, "execution_count": 46, "metadata": {}, "output_type": "execute_result" } ], "source": [ "matrix" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.7 Slicing lists\n", "- all the slice operations that work with strings also work with lists\n", "- syntax: [startIndex : endIndex : step]\n", "- startIndex is inclusive; endIndex is exclusive; step is optional by default 1" ] }, { "cell_type": "code", "execution_count": 36, "metadata": {}, "outputs": [], "source": [ "# create a list of lower-case alphabets\n", "alphas = ['a', 'b', 'c', 'd', 'e', 'f', 'g'] # add the rest..." ] }, { "cell_type": "code", "execution_count": 37, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a', 'b', 'c', 'd', 'e', 'f', 'g']" ] }, "execution_count": 37, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphas" ] }, { "cell_type": "code", "execution_count": 38, "metadata": {}, "outputs": [], "source": [ "# there's better way to create lists of all lowercase ascii\n", "import string\n", "alphas = list(string.ascii_lowercase)" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']\n" ] } ], "source": [ "print(alphas[:])" ] }, { "cell_type": "code", "execution_count": 40, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['a', 'd', 'g', 'j', 'm', 'p', 's', 'v', 'y']\n" ] } ], "source": [ "print(alphas[::3])" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['b', 'c']\n" ] } ], "source": [ "print(alphas[1:3])" ] }, { "cell_type": "code", "execution_count": 42, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['z', 'y', 'x', 'w', 'v', 'u', 't', 's', 'r', 'q', 'p', 'o', 'n', 'm', 'l', 'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a']\n" ] } ], "source": [ "print(alphas[::-1])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.8 Lists and strings\n", "- match made in heaven - work together really well :)" ] }, { "cell_type": "code", "execution_count": 44, "metadata": {}, "outputs": [], "source": [ "# convert string to list of characters\n", "alphaList = list(string.ascii_lowercase)" ] }, { "cell_type": "code", "execution_count": 45, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a',\n", " 'b',\n", " 'c',\n", " 'd',\n", " 'e',\n", " 'f',\n", " 'g',\n", " 'h',\n", " 'i',\n", " 'j',\n", " 'k',\n", " 'l',\n", " 'm',\n", " 'n',\n", " 'o',\n", " 'p',\n", " 'q',\n", " 'r',\n", " 's',\n", " 't',\n", " 'u',\n", " 'v',\n", " 'w',\n", " 'x',\n", " 'y',\n", " 'z']" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphaList" ] }, { "cell_type": "code", "execution_count": 46, "metadata": {}, "outputs": [], "source": [ "# convert list to string by joining pairs of chars with a delimiter\n", "alphaStr = '|'.join(alphaList)" ] }, { "cell_type": "code", "execution_count": 47, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r|s|t|u|v|w|x|y|z'" ] }, "execution_count": 47, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphaStr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.9 lists are mutable\n", "- we can change/replace/update list elements in place" ] }, { "cell_type": "code", "execution_count": 48, "metadata": {}, "outputs": [], "source": [ "names = [\"john\", \"David\", \"Alice\"]\n", "names[0] = \"jake\"" ] }, { "cell_type": "code", "execution_count": 49, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['jake', 'David', 'Alice']" ] }, "execution_count": 49, "metadata": {}, "output_type": "execute_result" } ], "source": [ "names" ] }, { "cell_type": "code", "execution_count": 50, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'j'" ] }, "execution_count": 50, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# How to correct spelling of jake?\n", "names[0][0]" ] }, { "cell_type": "code", "execution_count": 51, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "'str' object does not support item assignment", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnames\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m'J'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: 'str' object does not support item assignment" ] } ], "source": [ "names[0][0] = 'J'" ] }, { "cell_type": "code", "execution_count": 52, "metadata": {}, "outputs": [], "source": [ "names[0] = 'Jake'" ] }, { "cell_type": "code", "execution_count": 53, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['Jake', 'David', 'Alice']" ] }, "execution_count": 53, "metadata": {}, "output_type": "execute_result" } ], "source": [ "names" ] }, { "cell_type": "code", "execution_count": 54, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a',\n", " 'b',\n", " 'c',\n", " 'd',\n", " 'e',\n", " 'f',\n", " 'g',\n", " 'h',\n", " 'i',\n", " 'j',\n", " 'k',\n", " 'l',\n", " 'm',\n", " 'n',\n", " 'o',\n", " 'p',\n", " 'q',\n", " 'r',\n", " 's',\n", " 't',\n", " 'u',\n", " 'v',\n", " 'w',\n", " 'x',\n", " 'y',\n", " 'z']" ] }, "execution_count": 54, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphas" ] }, { "cell_type": "code", "execution_count": 55, "metadata": {}, "outputs": [], "source": [ "alphas[:4] = ['A', 'B', 'C', 'D']" ] }, { "cell_type": "code", "execution_count": 56, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['A',\n", " 'B',\n", " 'C',\n", " 'D',\n", " 'e',\n", " 'f',\n", " 'g',\n", " 'h',\n", " 'i',\n", " 'j',\n", " 'k',\n", " 'l',\n", " 'm',\n", " 'n',\n", " 'o',\n", " 'p',\n", " 'q',\n", " 'r',\n", " 's',\n", " 't',\n", " 'u',\n", " 'v',\n", " 'w',\n", " 'x',\n", " 'y',\n", " 'z']" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphas" ] }, { "cell_type": "code", "execution_count": 57, "metadata": {}, "outputs": [], "source": [ "alphas[:4] = []" ] }, { "cell_type": "code", "execution_count": 58, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['e',\n", " 'f',\n", " 'g',\n", " 'h',\n", " 'i',\n", " 'j',\n", " 'k',\n", " 'l',\n", " 'm',\n", " 'n',\n", " 'o',\n", " 'p',\n", " 'q',\n", " 'r',\n", " 's',\n", " 't',\n", " 'u',\n", " 'v',\n", " 'w',\n", " 'x',\n", " 'y',\n", " 'z']" ] }, "execution_count": 58, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphas" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.10 Deleting list elements\n", "- del statement removes an element from a list given its index" ] }, { "cell_type": "code", "execution_count": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['e',\n", " 'f',\n", " 'g',\n", " 'h',\n", " 'i',\n", " 'j',\n", " 'k',\n", " 'l',\n", " 'm',\n", " 'n',\n", " 'o',\n", " 'p',\n", " 'q',\n", " 'r',\n", " 's',\n", " 't',\n", " 'u',\n", " 'v',\n", " 'w',\n", " 'x',\n", " 'y',\n", " 'z']" ] }, "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphas" ] }, { "cell_type": "code", "execution_count": 60, "metadata": {}, "outputs": [], "source": [ "del alphas[0]" ] }, { "cell_type": "code", "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['f',\n", " 'g',\n", " 'h',\n", " 'i',\n", " 'j',\n", " 'k',\n", " 'l',\n", " 'm',\n", " 'n',\n", " 'o',\n", " 'p',\n", " 'q',\n", " 'r',\n", " 's',\n", " 't',\n", " 'u',\n", " 'v',\n", " 'w',\n", " 'x',\n", " 'y',\n", " 'z']" ] }, "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphas" ] }, { "cell_type": "code", "execution_count": 62, "metadata": {}, "outputs": [ { "ename": "IndexError", "evalue": "list assignment index out of range", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mdel\u001b[0m \u001b[0malphas\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m26\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mIndexError\u001b[0m: list assignment index out of range" ] } ], "source": [ "del alphas[26]" ] }, { "cell_type": "code", "execution_count": 63, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 63, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphas.index('z')" ] }, { "cell_type": "code", "execution_count": 64, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "20" ] }, "execution_count": 64, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphas.index(alphas[-1])" ] }, { "cell_type": "code", "execution_count": 65, "metadata": {}, "outputs": [], "source": [ "del alphas[1:3]" ] }, { "cell_type": "code", "execution_count": 66, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['f',\n", " 'i',\n", " 'j',\n", " 'k',\n", " 'l',\n", " 'm',\n", " 'n',\n", " 'o',\n", " 'p',\n", " 'q',\n", " 'r',\n", " 's',\n", " 't',\n", " 'u',\n", " 'v',\n", " 'w',\n", " 'x',\n", " 'y',\n", " 'z']" ] }, "execution_count": 66, "metadata": {}, "output_type": "execute_result" } ], "source": [ "alphas" ] }, { "cell_type": "code", "execution_count": 67, "metadata": {}, "outputs": [], "source": [ "indexOfZ = alphas.index('z')\n", "del alphas[indexOfZ]" ] }, { "cell_type": "code", "execution_count": 68, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "['f', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y']\n" ] } ], "source": [ "print(alphas)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.11 Objects and references\n", "- **is** operator can be used to test if two objects are referencing the same memory location\n", " - meaning they're essentially the same object with the same values" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 78, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# even though a and b are two separate objects is still evaluates to True\n", "a = 'apple'\n", "b = 'apple'\n", "a is b" ] }, { "cell_type": "code", "execution_count": 69, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 69, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# even though c and d are two separate objects is still evaluates to True\n", "c = 10\n", "d = 10\n", "c is d" ] }, { "cell_type": "code", "execution_count": 73, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "False\n" ] } ], "source": [ "# what abut tuple?\n", "e = (1, 2)\n", "f = (1, 2)\n", "print(e == f)\n", "print(e is f)" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "False\n" ] } ], "source": [ "# What about lists?\n", "l1 = [1, 2, 3]\n", "l2 = [1, 2, 3]\n", "print(l1 == l2)\n", "print(l1 is l2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.12 Copying lists (Shallow copy vs Deep copy)\n", "- see [PythonTutor.com to visualize aliasing](http://pythontutor.com/visualize.html#code=import%20copy%0A%0Aa%20%3D%20%5B1,%20%5B2,%203%5D%5D%0Ab%20%3D%20a%0Ac%20%3D%20a.copy%28%29%0Ad%20%3D%20a%5B%3A%5D%0Af%20%3D%20copy.deepcopy%28a%29&cumulative=false&curInstr=0&heapPrimitives=false&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)\n", "- assignment *=* operator does shallow copy" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "True\n", "True\n" ] } ], "source": [ "a = [1, 2, 3]\n", "b = a\n", "print(a is b)\n", "print(a == b)" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[10, 2, 3]\n", "[10, 2, 3]\n" ] } ], "source": [ "b[0] = 10\n", "print(a)\n", "print(b)" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [], "source": [ "# How do you actually clone lists - do a deep copy?\n", "c = a[:] # easy way shallow copy\n", "d = a.copy() # shallow copy\n", "import copy\n", "e = copy.deepcopy(b)" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 83, "metadata": {}, "output_type": "execute_result" } ], "source": [ "c is a" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "d is a" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "False" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "b is e" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.13 List methods\n", "- list objects have a bunch methods that can be invoked to work with list\n", "- run help(list)" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Help on class list in module builtins:\n", "\n", "class list(object)\n", " | list(iterable=(), /)\n", " | \n", " | Built-in mutable sequence.\n", " | \n", " | If no argument is given, the constructor creates a new empty list.\n", " | The argument must be an iterable if specified.\n", " | \n", " | Methods defined here:\n", " | \n", " | __add__(self, value, /)\n", " | Return self+value.\n", " | \n", " | __contains__(self, key, /)\n", " | Return key in self.\n", " | \n", " | __delitem__(self, key, /)\n", " | Delete self[key].\n", " | \n", " | __eq__(self, value, /)\n", " | Return self==value.\n", " | \n", " | __ge__(self, value, /)\n", " | Return self>=value.\n", " | \n", " | __getattribute__(self, name, /)\n", " | Return getattr(self, name).\n", " | \n", " | __getitem__(...)\n", " | x.__getitem__(y) <==> x[y]\n", " | \n", " | __gt__(self, value, /)\n", " | Return self>value.\n", " | \n", " | __iadd__(self, value, /)\n", " | Implement self+=value.\n", " | \n", " | __imul__(self, value, /)\n", " | Implement self*=value.\n", " | \n", " | __init__(self, /, *args, **kwargs)\n", " | Initialize self. See help(type(self)) for accurate signature.\n", " | \n", " | __iter__(self, /)\n", " | Implement iter(self).\n", " | \n", " | __le__(self, value, /)\n", " | Return self<=value.\n", " | \n", " | __len__(self, /)\n", " | Return len(self).\n", " | \n", " | __lt__(self, value, /)\n", " | Return self\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msort\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: '<' not supported between instances of 'int' and 'list'" ] } ], "source": [ "a.sort()" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [], "source": [ "blist = list(range(10, 0, -1))" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "blist" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [], "source": [ "blist.sort()" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n" ] } ], "source": [ "print(blist)" ] }, { "cell_type": "code", "execution_count": 103, "metadata": {}, "outputs": [], "source": [ "blist.sort(reverse=True)" ] }, { "cell_type": "code", "execution_count": 104, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]" ] }, "execution_count": 104, "metadata": {}, "output_type": "execute_result" } ], "source": [ "blist" ] }, { "cell_type": "code", "execution_count": 105, "metadata": {}, "outputs": [], "source": [ "m = max(blist)\n", "mI = blist.index(m)" ] }, { "cell_type": "code", "execution_count": 106, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n" ] } ], "source": [ "print(mI)" ] }, { "cell_type": "code", "execution_count": 107, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 107, "metadata": {}, "output_type": "execute_result" } ], "source": [ "min(blist)" ] }, { "cell_type": "code", "execution_count": 108, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0\n" ] } ], "source": [ "print(blist.count(100))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.14 List applications\n", "\n", "### convert a string to list of integers" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter 5 numbers separated by space: 1 2 100 5\n" ] } ], "source": [ "nums = input('Enter 5 numbers separated by space: ')" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'1 2 100 5'" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nums" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [], "source": [ "nums = nums.split(' ')" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['1', '2', '100', '5']" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "nums" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [], "source": [ "intNums = []\n", "for n in nums:\n", " intN = int(n)\n", " intNums.append(intN)" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 100, 5]" ] }, "execution_count": 79, "metadata": {}, "output_type": "execute_result" } ], "source": [ "intNums" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [], "source": [ "intNums.sort()" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[1, 2, 5, 100]" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "intNums" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### convert list of integers to string" ] }, { "cell_type": "code", "execution_count": 94, "metadata": {}, "outputs": [ { "ename": "TypeError", "evalue": "sequence item 0: expected str instance, int found", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;34m' '\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mjoin\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mintNums\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m", "\u001b[0;31mTypeError\u001b[0m: sequence item 0: expected str instance, int found" ] } ], "source": [ "' '.join(intNums)" ] }, { "cell_type": "code", "execution_count": 99, "metadata": {}, "outputs": [], "source": [ "strNum = []\n", "for n in intNums:\n", " strNum.append(str(n))" ] }, { "cell_type": "code", "execution_count": 100, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['1', '2', '5', '100']" ] }, "execution_count": 100, "metadata": {}, "output_type": "execute_result" } ], "source": [ "strNum" ] }, { "cell_type": "code", "execution_count": 101, "metadata": {}, "outputs": [], "source": [ "strNum = ' '.join(strNum)" ] }, { "cell_type": "code", "execution_count": 102, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'1 2 5 100'" ] }, "execution_count": 102, "metadata": {}, "output_type": "execute_result" } ], "source": [ "strNum" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.15 Passing list to function - pass-by-reference\n", "- mutable types such as list are passed-by-reference \n", "- an alias or reference is passed instead of a copy of the data" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [], "source": [ "def getData(someList):# someList is formal parameter\n", " for i in range(5):\n", " a = int(input('enter a number: '))\n", " someList.append(a)" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "enter a number: 1\n", "enter a number: 10\n", "enter a number: 4\n", "enter a number: 6\n", "enter a number: 90\n" ] } ], "source": [ "alist = []\n", "getData(alist) # alist is actual argument" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 10, 4, 6, 90]\n" ] } ], "source": [ "# when formal parameter is updated, actual parameter is also updated\n", "alist" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### [visualize - pass-by-reference with pythontutor.com](http://pythontutor.com/visualize.html#code=def%20getData%28someList%29%3A%23%20someList%20is%20formal%20parameter%0A%20%20%20%20for%20i%20in%20range%285%29%3A%0A%20%20%20%20%20%20%20%20a%20%3D%20int%28input%28'enter%20a%20number%3A%20'%29%29%0A%20%20%20%20%20%20%20%20someList.append%28a%29%0A%0Aalist%20%3D%20%5B%5D%0AgetData%28alist%29%20%23%20alist%20is%20actual%20argument&cumulative=false&curInstr=0&heapPrimitives=false&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.16 return list from functions\n", "- lists can be returned from functions" ] }, { "cell_type": "code", "execution_count": 121, "metadata": {}, "outputs": [], "source": [ "def getMaxMin(alist):\n", " m = max(alist)\n", " minVal = min(alist)\n", " return [m, minVal]\n", " " ] }, { "cell_type": "code", "execution_count": 122, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1999999, -1000]\n" ] } ], "source": [ "alist = list(range(-1000, 2000000))\n", "print(getMaxMin(alist))" ] }, { "cell_type": "code", "execution_count": 123, "metadata": {}, "outputs": [], "source": [ "assert getMaxMin(alist) == [1999999, -1000]" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.17 Casting list into tuple and back\n", "- since tuples are immutable it may be benefitial to cast them into lists and update\n", "- can convert list back to tuple again" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1, 2, 3]\n" ] } ], "source": [ "atuple = (1, 2, 3)\n", "alist = list(atuple)\n", "print(alist)" ] }, { "cell_type": "code", "execution_count": 89, "metadata": {}, "outputs": [], "source": [ "btuple = tuple(alist)" ] }, { "cell_type": "code", "execution_count": 90, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 2, 3)\n" ] } ], "source": [ "print(btuple)" ] }, { "cell_type": "code", "execution_count": 91, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 91, "metadata": {}, "output_type": "execute_result" } ], "source": [ "atuple == btuple" ] }, { "cell_type": "code", "execution_count": 93, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "(1, 2, 3) [1, 2, 3]\n", "False\n" ] } ], "source": [ "# eventhough the elements are equal; the types of two objects are not\n", "print(atuple, alist)\n", "print(atuple == alist)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 8.18 Exercises\n", "1. Practice with the rest of the methods of list" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. Draw memory state snapshot for a and b after the following Python code is executed:\n", "\n", "```python\n", "a = [1, 2, 3]\n", "b = a[:]\n", "b[0] = 5\n", "```" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. Lists can be used to represent mathematical vectors. Write a function `add_vectors(u, v)` that takes two lists of numbers of the same length, and returns a new list containing the sums of the corresponding elements of each. The following test cases must pass once the add_vectors is complete." ] }, { "cell_type": "code", "execution_count": 124, "metadata": {}, "outputs": [], "source": [ "def add_vectors(a, b):\n", " pass" ] }, { "cell_type": "code", "execution_count": 125, "metadata": {}, "outputs": [ { "ename": "AssertionError", "evalue": "vectors not added correctly", "output_type": "error", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;31m# test cases\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0madd_vectors\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'vectors not added correctly'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 3\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0madd_vectors\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m6\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'vectors not added correctly'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0madd_vectors\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m6\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m4\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'vectors not added correctly'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mAssertionError\u001b[0m: vectors not added correctly" ] } ], "source": [ "# test cases\n", "assert add_vectors([1, 1], [1, 1]) == [2, 2], 'vectors not added correctly'\n", "assert add_vectors([1, 2], [1, 4]) == [2, 6], 'vectors not added correctly'\n", "assert add_vectors([1, 2, 1], [1, 4, 3]) == [2, 6, 4], 'vectors not added correctly'" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Kattis problems\n", "- the following Kattis problems can be solved using list\n", "\n", "\n", "1. Dice Game - https://open.kattis.com/problems/dicegame\n", "- Height Ordering - https://open.kattis.com/problems/height\n", "- What does the fox say? - https://open.kattis.com/problems/whatdoesthefoxsay\n", "- Army Strength (Easy) - https://open.kattis.com/problems/armystrengtheasy\n", "- Army Strength (Hard) - https://open.kattis.com/problems/armystrengthhard\n", "- Black Friday - https://open.kattis.com/problems/blackfriday\n", "\n", "### List sorting with two keys\n", "1. Roll Call - https://open.kattis.com/problems/rollcall\n", "- Cooking Water - https://open.kattis.com/problems/cookingwater\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "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.7.3" } }, "nbformat": 4, "nbformat_minor": 2 }