{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Assume L is a list with at least two elements. Which of the following program finishes with L having the same elements in the same order it had at the beginning of the program? Comment your answers "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"L.append(3)\n",
"L.pop()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x=L.pop()\n",
"L.append(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x=L.pop()\n",
"y=L.pop()\n",
"L.append(x)\n",
"L.append(y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"..."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x=L.pop()\n",
"y=L.pop()\n",
"L.append(y)\n",
"L.append(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Lists"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a list, called \"Numbers\". Let it contain the numbers 10, 25, 40, and 50."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Print the second element from the list.\n",
"
Note that \"second\" means \"second\" in English, not in Pythonic"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Print the second-last element. Suppose that you do not have any idea of the length of Numbers and cannot use len function (*this clarification is written because several of you enjoy themselves perverting any easy and straightforward way of solving this task*)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Substitute the number in the first position (in English, not in Pythonic) with the string \"blabla\". Use only one statement (*also this one is here because students pervert my requests :-(*"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Delete the number in second position (always English and not Pythonic) from the Numbers list."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### List Slicing"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Slicing is a technique which can be applied to strings and to lists to extract a part of them, exactly the same that you do for strings. \n",
"
You just specify two numbers separated by a colon and get the elements from the index of the first number and up to the second one excluded. If the first number is omitted it is assumed to be 0. If the second number is omitted it is assumed to be the length of the list/string. If a number is negative, it counts from the end (-1 is the last one, -2 is the second last one)."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"Numbers = [15, 40, 50, 100, 115, 140]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using list slicing, obtain the numbers 100 and 115."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using slicing, extract the first four elements from the list."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using slicing, extract all the elements from the list from the 3rd position (in English, not Pythonic) onwards. Suppose that you do not know the length of the list and cannot use the len function."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using slicing, extract the last 4 elements from the list supposing that you do not know how many elements does the list have and cannot use the len function."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Which is the position of the value 15? I want a program telling it, not you."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Create a list, called \"Two_Numbers\". Let its elements be the values 1 and 2. Then, create a new one, named \"All_Numbers\", that will contain all the elements from both the \"Numbers\" and the \"Two_Numbers\" lists together (not a list of lists)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using the .sort method, sort all the numbers in the \"Numbers\" list from the largest to the smallest."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Append the number 100 to the Numbers list."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Numbers = [15, 40, 50]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"With the help of the .extend, add the numbers 115 an 140 to the list."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Print \"The fourth element of the Numbers list is:\" followed by the value of the fourth element. Do this using two different print instructions, but the output must stay only on one line. \n",
"
Click here for a little suggestion\n",
" Strategy: As usual, think how would you solve it manually, without worrying about the -1 and without worrying about which kind of loop do you use (probably starting with while is much easier). There are dozens of different ways to solve it. Do it really!!! Once you have written it, fix it for the case in which the element is not in the list, also here there are several ways for doing it. And finally convert the loop into the other type and see which one is easier in this case."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## index method and in operator"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In Python there exist list method **.index(e)** which accepts as parameter an element and returns the position of that element in the list. For example:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"L=[3,7,2,0]\n",
"print(L.index(7))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Unfortunately this method gives an error whenever the element is not in the list (try please). But luckily there exist operator **in** which returns a boolean telling you whether an element is in a list and operator **not in** which returns exactly the opposite. For example:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"False\n",
"True\n"
]
}
],
"source": [
"L=[3,7,2,0]\n",
"print(7 in L)\n",
"print(8 in L)\n",
"print(8 not in L)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now that you know these method and operators, rewrite function findElement using them instead of using a loop."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Union"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Define a function Union which takes as input two lists A and B and returns a list consisting in the union of the two lists without insering the duplicated elements twice. The function must skip also the duplicated elements in the same list.\n",
"\n",
"For example, Union([1,2,3,4],[4,9,2,7]) should return [1,2,3,4,9,7] and Union([1,1,1,2],[3,3,3,2]) should return [1,2,3]\n",
"\n",
" Strategy: As always, think at how would you, human, solve it on paper. Write down all the steps slowly, one by one, carefully, with easy steps that a stupid computer can do using the tools described above without any superior knowledge or intuition. Do it really, or you will produce a mess or a highly inefficient program. "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Determine **Union**'s complexity supposing that the two lists have length N and M.\n",
"\n",
"Supposing that checking whether element x in L has a complexity equal to len(L) comparisons (you are not required to use the in operator, I write here its complexity in case you do), while for x in L does not have any complexity (in fact in this case it goes through the elements and it is much easier that a x in L). "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"..."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### splitting strings"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"DIFFICULT and OPTIONAL\n",
"\n",
"Without using the .split() method, define a function splitString that takes as input a string and splits it at any ortographic symbol (space, comma, point, question mark, exclamation mark, semicolon, colon). It returns the split words into a list.\n",
" Strategy: after having thought at the problem, if you can't come up with a working algorithm click below to reveal it. I said after having thought\n",
"
Write two functions, one which uses while loop and another one which uses for loop.\n",
"
Do not use the .index(< Number>) method and do not use the in operator to check whether something is inside the list (you may still use it for a for loop).\n",
" \n",
"For example, findElement([\"Albert\",\"John\",\"Mara\"],\"Mara\") returns 2 and find([7,0,\"blabla\",6.7],\"bloblo\") returns -1.\n",
"\n",
"
For example: splitString(\"This is my day! Why? Not: this isn't\") returns [\"This\",\"is\",\"my\",\"day\",\"Why\",\"Not\",\"this\",\"isn't\"]\n",
"