Instructions
Make sure to work through the notebooks in the Reading Assignment first.
This homework is not related to Simulations directly. The goal is to make sure you are comfortable programming basic Python code using Jupyter Notebooks. Start early, ask questions. Watch video presentation for extra help.
Your entire homework description is contained in the following Jupyter Notebook:
HW2_YourName.ipynb
. Download it (right-click and ‘Save link as..’) and save it in your working folder, start Jupyter and open this notebook.
Follow the directions given in the notebook to complete all the tasks and submit the file.
{
“cells”: [
{
“cell_type”: “markdown”,
“metadata”: {
“colab_type”: “text”,
“id”: “nMzWtAd2b5SM”
},
“source”: [
“## Week 2 HW — Python Crash Course”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“colab_type”: “text”,
“id”: “ZuBXh_bFNWrR”
},
“source”: [
“#### **Step 1: Rename the Notebook!!!**\n”,
“First, rename this notebook to **HW2_LastName_FirstName.ipynb** using your name. You will have to submit this file to me via Western Online, once you’ve completed the HW.\n”,
“\n”,
“You will have to compete the tasks outlined in each problem in the provided space.\n”,
“\n”,
“Don’t forget to save your work.”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“colab_type”: “text”,
“id”: “4sdZgMJiOJ2w”
},
“source”: [
“#### **How to submit it**\n”,
“Once you’ve completed the HW, make sure to save the notebook (don’t forget to rename it).\n”,
“\n”,
“Then, submit your **.ipynb** file to me as an attachment. \n”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“colab_type”: “text”,
“id”: “Efhsx5FVOty1”
},
“source”: [
“—\n”,
“—\n”,
“**Step 2: Complete each of the problems below.**\n”,
“\n”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“colab_type”: “text”,
“id”: “pCGYcRpqLytI”
},
“source”: [
“—”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“colab_type”: “text”,
“id”: “NA5SOqo7dVTY”
},
“source”: [
“### **Problem 1:** \n”,
“Write a function **`first_4_chars()`** that would take a string as an input and return the string consisting of the the first 4 characters of the input string. If the string is shorter than 4 characters long, just return the string as is. Assume that the input is a string, no need to check it. _Hint:_ use slicing. ”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“colab_type”: “text”,
“id”: “zu9M4EMuL5Al”
},
“source”: [
“—\n”,
“\n”,
“***Your code goes here***: ”
]
},
{
“cell_type”: “code”,
“execution_count”: 0,
“metadata”: {
“colab”: {},
“colab_type”: “code”,
“id”: “hdfOv-Jvnf5h”
},
“outputs”: [],
“source”: [
“#Your code goes here:\n”,
“#Function definition:\n”,
“#…”
]
},
{
“cell_type”: “code”,
“execution_count”: 0,
“metadata”: {
“colab”: {
“base_uri”: “https://localhost:8080/”,
“height”: 34
},
“colab_type”: “code”,
“id”: “nkNOcjqLnomT”,
“outputId”: “15428aa8-4696-4505-f64a-7b6303315e90”
},
“outputs”: [
{
“data”: {
“text/plain”: [
“‘Hand'”
]
},
“execution_count”: 35,
“metadata”: {
“tags”: []
},
“output_type”: “execute_result”
}
],
“source”: [
“#Checking:\n”,
“first_4_chars(‘Handsome’)”
]
},
{
“cell_type”: “code”,
“execution_count”: 0,
“metadata”: {
“colab”: {
“base_uri”: “https://localhost:8080/”,
“height”: 34
},
“colab_type”: “code”,
“id”: “InxDIhwZo4dD”,
“outputId”: “e02b32c9-7169-44eb-97c0-fb0c45585bdc”
},
“outputs”: [
{
“data”: {
“text/plain”: [
“‘Univ'”
]
},
“execution_count”: 40,
“metadata”: {
“tags”: []
},
“output_type”: “execute_result”
}
],
“source”: [
“#Checking:\n”,
“first_4_chars(‘University’)”
]
},
{
“cell_type”: “code”,
“execution_count”: 2,
“metadata”: {
“colab”: {
“base_uri”: “https://localhost:8080/”,
“height”: 34
},
“colab_type”: “code”,
“id”: “nkNOcjqLnomT”,
“outputId”: “15428aa8-4696-4505-f64a-7b6303315e90”
},
“outputs”: [
{
“data”: {
“text/plain”: [
“‘Uni'”
]
},
“execution_count”: 2,
“metadata”: {},
“output_type”: “execute_result”
}
],
“source”: [
“#Checking:\n”,
“first_4_chars(‘Uni’)”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“colab_type”: “text”,
“id”: “qIQ3W5Mdo9vR”
},
“source”: [
“### **Problem 2:** \n”,
“Write a function **`last_n_chars()`** that would take a string and a positive integer `n` as an input, and return the string consisting of the the last `n` characters of the input string. ”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“colab_type”: “text”,
“id”: “fjlhmbQrL74O”
},
“source”: [
“—\n”,
“\n”,
“***Your code goes here***: ”
]
},
{
“cell_type”: “code”,
“execution_count”: 0,
“metadata”: {
“colab”: {},
“colab_type”: “code”,
“id”: “nPO5-SBMo6vx”
},
“outputs”: [],
“source”: [
“#Your code goes here:\n”,
“#Function definition:\n”,
“#…”
]
},
{
“cell_type”: “code”,
“execution_count”: 0,
“metadata”: {
“colab”: {
“base_uri”: “https://localhost:8080/”,
“height”: 34
},
“colab_type”: “code”,
“id”: “R2GPOS5mqYPi”,
“outputId”: “6de9b461-972b-4387-dbc9-e8ecd29be9a5”
},
“outputs”: [
{
“data”: {
“text/plain”: [
“‘ity'”
]
},
“execution_count”: 74,
“metadata”: {
“tags”: []
},
“output_type”: “execute_result”
}
],
“source”: [
“#Checking:\n”,
“last_n_chars(\”University\”, 3)”
]
},
{
“cell_type”: “code”,
“execution_count”: 0,
“metadata”: {
“colab”: {
“base_uri”: “https://localhost:8080/”,
“height”: 34
},
“colab_type”: “code”,
“id”: “b6FNBsu4qu9x”,
“outputId”: “8403dbeb-b975-4695-995a-5132359de18c”
},
“outputs”: [
{
“data”: {
“text/plain”: [
“‘versity'”
]
},
“execution_count”: 75,
“metadata”: {
“tags”: []
},
“output_type”: “execute_result”
}
],
“source”: [
“#Checking:\n”,
“last_n_chars(\”University\”, 7)”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“colab_type”: “text”,
“id”: “LWuIiwqau7fh”
},
“source”: [
“### **Problem 3:** \n”,
“Write a function **`print_even_ind()`** that would take a *list* as an input, and print only *even indexed* elements of that list. Print them all on the same line.”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“colab_type”: “text”,
“id”: “uXG5uf52MCKu”
},
“source”: [
“—\n”,
“\n”,
“***Your code goes here***: ”
]
},
{
“cell_type”: “code”,
“execution_count”: 0,
“metadata”: {
“colab”: {},
“colab_type”: “code”,
“id”: “pvpR10efveRS”
},
“outputs”: [],
“source”: [
“#Your code goes here:\n”,
“#Function definition:\n”,
“#…”
]
},
{
“cell_type”: “code”,
“execution_count”: 0,
“metadata”: {
“colab”: {
“base_uri”: “https://localhost:8080/”,
“height”: 34
},
“colab_type”: “code”,
“id”: “OGYwa_OUvsah”,
“outputId”: “acd2d58d-47e5-474c-cdc5-592ca1715c01”
},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“34 55 65\n”
]
}
],
“source”: [
“#Checking:\n”,
“print_even_ind([34, 23, 55, 51, 65])”
]
},
{
“cell_type”: “code”,
“execution_count”: 0,
“metadata”: {
“colab”: {
“base_uri”: “https://localhost:8080/”,
“height”: 34
},
“colab_type”: “code”,
“id”: “rT6Wq87IwozB”,
“outputId”: “1027cf4b-26c2-4f10-e9e3-a1a06569eb8e”
},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“index0 index2 index4\n”
]
}
],
“source”: [
“#Checking:\n”,
“print_even_ind([\”index0\”, \”index1\”, \”index2\”, \”index3\”, \”index4\”])”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“colab_type”: “text”,
“id”: “j-HdmNxcsb8F”
},
“source”: [
“### **Problem 4:** \n”,
“Explain the main difference between a list and a tuple. Write your answer in the markdown cell below, and make every appearance of the word ‘list’ **bold**, and every appearance of the word ‘tuple’ ***bold and italic***. (This should not be code, it’s just for you to practice working with markdown cells).”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“colab_type”: “text”,
“id”: “GgILSU1Nt3Uh”
},
“source”: [
“— \n”,
“Write your answer here:\n”,
“…”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“colab_type”: “text”,
“id”: “hRIOt_rMt1zi”
},
“source”: [
“### **Problem 5:** \n”,
“Write a function **`add_N_to_list()`** that would take a *list of integers* and an integer `n` as an input, and add `n` to every element of the list. The function should not return anything, but it is the _original list that_ ***has to be changed***, not a new one created (see examples below).”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“colab_type”: “text”,
“id”: “UDNdhAJ5MN6E”
},
“source”: [
“—\n”,
“\n”,
“***Your code goes here***: ”
]
},
{
“cell_type”: “code”,
“execution_count”: 0,
“metadata”: {
“colab”: {},
“colab_type”: “code”,
“id”: “2mQfqcp22rRG”
},
“outputs”: [],
“source”: [
“#Your code goes here:\n”,
“#Function definition:\n”,
“def add_N_to_list(l, n):\n”,
“#…”
]
},
{
“cell_type”: “code”,
“execution_count”: 0,
“metadata”: {
“colab”: {
“base_uri”: “https://localhost:8080/”,
“height”: 34
},
“colab_type”: “code”,
“id”: “J86m03ge3E3j”,
“outputId”: “2d87d3c9-b70d-4ffa-c5af-e77d626a247f”
},
“outputs”: [
{
“data”: {
“text/plain”: [
“[6, 7, 8]”
]
},
“execution_count”: 113,
“metadata”: {
“tags”: []
},
“output_type”: “execute_result”
}
],
“source”: [
“#Checking:\n”,
“l1=[1,2,3]\n”,
“add_N_to_list(l1,5)\n”,
“l1”
]
},
{
“cell_type”: “code”,
“execution_count”: 0,
“metadata”: {
“colab”: {
“base_uri”: “https://localhost:8080/”,
“height”: 34
},
“colab_type”: “code”,
“id”: “q75nWJH-4Hwj”,
“outputId”: “1c8d702a-d0e2-4a89-c4af-7054e64e9ebd”
},
“outputs”: [
{
“data”: {
“text/plain”: [
“[5, 6, 7, 8, 9, 10, 11, 12, 13, 14]”
]
},
“execution_count”: 117,
“metadata”: {
“tags”: []
},
“output_type”: “execute_result”
}
],
“source”: [
“#Checking:\n”,
“l1=list(range(10))\n”,
“add_N_to_list(l1,5)\n”,
“l1”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“colab_type”: “text”,
“id”: “0CP53MYPHC61”
},
“source”: [
“### **Problem 6:** \n”,
“The following dictionary containts a cipher. Write a function `decrypt()` that takes this dictionary and some encrypted message (encrypted by this cipher) and prints out the decrypted one:”
]
},
{
“cell_type”: “code”,
“execution_count”: 0,
“metadata”: {
“colab”: {},
“colab_type”: “code”,
“id”: “joxDSfc-Q9RD”
},
“outputs”: [],
“source”: [
“cipher_key = {‘A’: ‘f’, \n”,
” ‘B’: ‘w’, \n”,
” ‘C’: ‘j’, \n”,
” ‘D’: ‘u’, \n”,
” ‘E’: ‘b’, \n”,
” ‘F’: ‘z’, \n”,
” ‘G’: ‘o’, \n”,
” ‘H’: ‘n’, \n”,
” ‘I’: ‘s’, \n”,
” ‘J’: ‘a’, \n”,
” ‘K’: ‘ ‘, \n”,
” ‘L’: ‘t’, \n”,
” ‘M’: ‘y’, \n”,
” ‘N’: ‘e’, \n”,
” ‘O’: ‘x’, \n”,
” ‘P’: ‘c’, \n”,
” ‘Q’: ‘d’, \n”,
” ‘R’: ‘i’, \n”,
” ‘S’: ‘p’, \n”,
” ‘T’: ‘g’, \n”,
” ‘U’: ‘h’, \n”,
” ‘V’: ‘r’, \n”,
” ‘W’: ‘k’, \n”,
” ‘X’: ‘q’, \n”,
” ‘Y’: ‘l’,\n”,
” ‘Z’: ‘m’,\n”,
” ‘ ‘: ‘v’}”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“colab_type”: “text”,
“id”: “pMRDbUiFeMGX”
},
“source”: [
“The cipher works in a very straightfdorward way. To decode an encrypted message, you should match every character in that message to the key in the provided dictionary `cipher_key` and \”translate\” it to the corresponding value. If the character is not one of the keys, just leave it as is.\n”,
“\n”,
“For example, ‘`UNYYG!`’ should translate to ‘`hello!`’, and ‘`ZMKHJZNKRIKQJVWHNII`’ to ‘`my name is darkness`’.”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“colab_type”: “text”,
“id”: “qSFkL5XgMi8e”
},
“source”: [
“—\n”,
“\n”,
“***Your code goes here***: \n”,
“Write the function `decrypt(cipher, message)`”
]
},
{
“cell_type”: “code”,
“execution_count”: 0,
“metadata”: {
“colab”: {},
“colab_type”: “code”,
“id”: “vMNwmAPIM1MF”
},
“outputs”: [],
“source”: [
“#Your code goes here:\n”,
“#Function definition:\n”,
“# …”
]
},
{
“cell_type”: “code”,
“execution_count”: 0,
“metadata”: {
“colab”: {
“base_uri”: “https://localhost:8080/”,
“height”: 34
},
“colab_type”: “code”,
“id”: “WM3mkz0MXj_Y”,
“outputId”: “4a591e3a-cbad-408f-fa20-44ac24675780”
},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“my name is darkness\n”
]
}
],
“source”: [
“#Checking:\n”,
“decrypt(cipher_key, ‘ZMKHJZNKRIKQJVWHNII’)”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“colab_type”: “text”,
“id”: “tZYr5eetZha5”
},
“source”: [
“Decrypt the following message **MGDKJVNKQGRHTKTVNJL!KWNNSKDSKLUNKTGGQKBGVW!**”
]
},
{
“cell_type”: “code”,
“execution_count”: 0,
“metadata”: {
“colab”: {},
“colab_type”: “code”,
“id”: “ho5n3a2UZpY1”
},
“outputs”: [],
“source”: [
“#Checking. What does this decode to?\n”,
“decrypt(cipher_key, ‘MGDKJVNKQGRHTKTVNJL!KWNNSKDSKLUNKTGGQKBGVW!’)”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“colab_type”: “text”,
“id”: “BABPkTWcHE68”
},
“source”: [
“### **Problem 7:**\n”,
“One of the useful commands in Python is `input()`, which allows asking users for typing inputs. When executed, it will wait until the user enters smth and hits \”Enter\”. Whatever is entered by the user, is read in as a string.\n”,
“\n”,
“For example:”
]
},
{
“cell_type”: “code”,
“execution_count”: 0,
“metadata”: {
“colab”: {
“base_uri”: “https://localhost:8080/”,
“height”: 51
},
“colab_type”: “code”,
“id”: “ORCl6VZFdbEv”,
“outputId”: “021bddae-3520-41b5-e956-8d7106e393a8”
},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“What is your name:Michael\n”,
“Hello Michael\n”
]
}
],
“source”: [
“name=input(\”What is your name? \”)\n”,
“print(\”Hello \”+name)”
]
},
{
“cell_type”: “markdown”,
“metadata”: {
“colab_type”: “text”,
“id”: “8o8xP-BOcobj”
},
“source”: [
“If we want a user to enter numbers, we’ll have to convert the input from string to whatever type is needed:”
]
},
{
“cell_type”: “code”,
“execution_count”: 0,
“metadata”: {
“colab”: {
“base_uri”: “https://localhost:8080/”,
“height”: 51
},
“colab_type”: “code”,
“id”: “5y-nrBkdeQt_”,
“outputId”: “253eec7a-dc14-4755-a408-0e3cef4a89de”
},
“outputs”: [
{
“name”: “stdout”,
“output_type”: “stream”,
“text”: [
“How old are you? 49\n”,
“You still have 51 years until you become a centenarian.\n”
]
}
],
“source”: [
“age = int(input(\”How old are you? \”))\n”,
“if (age<100):\n",
" print(f\"You still have {100-age} years until you become a centenarian.\")\n",
"elif age==100:\n",
" print(\"Congratulation! You are a centenarian!\")\n",
"else:\n",
" print(f\"It's been {age-100} years since you turned a 100.\")"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "BSTmPyzueWIQ"
},
"source": [
"#### **Your task:**\n",
"Ask the user to enter her/his name and age (in the same input line, separated by spaces). Extract the name and the age from the input string and print out text just as in the examples above, but with additional modification: when printing a statement about years, your code should be able to handle the plural and singular cases properly.\n",
"\n",
"For example, if the user enters 99 for the age the program should say *'1 year'* and not *'1 year**s**'*, but if she puts 77 for the age, the pogram should say *'23 year**s**'*. \n",
"\n",
"See the example below."
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 85
},
"colab_type": "code",
"id": "_o-tORPAdOvx",
"outputId": "4b1398a1-2d44-47f7-b927-9467ed1f5785"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"Please enter your name and age: Peter 99\n",
"Hello Peter.\n",
"You still have 1 year until you become a centenarian.\n"
]
}
],
"source": [
"# Example output:"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "v1hWSRY0NIhD"
},
"source": [
"---\n",
"***Your code goes here:***"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "98F1_x_4HdsC"
},
"outputs": [],
"source": [
"#Your code goes here:\n",
"# ...\n"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "NA5SOqo7dVTY"
},
"source": [
"---\n",
"\n",
"### **Problem 8:** Generating a list of random integers\n",
"The following code generates a random integer, between 2 and 10 (including 2 and 10)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"id": "E6M0m3jZGted",
"outputId": "36022cdc-5946-4767-d57d-2e78147e2fd0"
},
"outputs": [
{
"data": {
"text/plain": [
"7"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import random\n",
"random.randint(2, 10)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "d-DcwDxEHVbj"
},
"source": [
"Your task is to use this command (`random.randint`) and list comprehension approach to generate a list of 100 random integers between 1 and 5000. Should be just one line of code: \n",
"\n",
"`rl = [ ....`*`list comprension here`*`...]`"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "zu9M4EMuL5Al"
},
"source": [
"---\n",
"\n",
"***Your code goes here***: "
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 54
},
"colab_type": "code",
"id": "hdfOv-Jvnf5h",
"outputId": "2384a1e0-3129-40c6-8819-fd910cf1b578"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1925, 1813, 1567, 12, 1494, 3794, 2849, 2003, 4517, 4249, 4062, 2251, 1200, 774, 3504, 3766, 4876, 2467, 1577, 732, 2975, 2581, 1097, 2862, 1546, 3831, 2073, 369, 4954, 408, 3854, 4252, 401, 412, 4076, 1859, 3579, 772, 1038, 506, 3518, 4036, 959, 753, 3395, 1678, 291, 3704, 4925, 2526, 552, 2372, 2334, 3093, 3739, 3046, 2799, 2281, 2645, 2254, 3680, 4660, 1457, 3154, 3151, 829, 2264, 2751, 557, 3346, 2612, 2351, 4665, 3039, 633, 246, 4518, 4723, 3748, 1310, 987, 3628, 840, 3357, 2498, 4247, 433, 492, 900, 2722, 1101, 3183, 4413, 1755, 1293, 1345, 1564, 2581, 2780, 895]\n"
]
}
],
"source": [
"#Your code goes here:\n",
"\n",
"#rand_list = [ # ... list comprehension here\n",
"print(rand_list)"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"id": "nkNOcjqLnomT",
"outputId": "708a464e-8dd8-4f8b-8dba-437af44159c8"
},
"outputs": [
{
"data": {
"text/plain": [
"100"
]
},
"execution_count": 246,
"metadata": {
"tags": []
},
"output_type": "execute_result"
}
],
"source": [
"#Checking:\n",
"len(rand_list)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"id": "InxDIhwZo4dD",
"outputId": "e3548b84-cf26-4440-cab1-9cd8385f2f12"
},
"outputs": [
{
"data": {
"text/plain": [
"4954"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Checking (answers will vary):\n",
"max(rand_list)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"id": "QrgtXg53JprV",
"outputId": "c391592b-2468-43b6-b069-63b651be27aa"
},
"outputs": [
{
"data": {
"text/plain": [
"12"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#Checking (answers will vary):\n",
"min(rand_list)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "j-HdmNxcsb8F"
},
"source": [
"---\n",
"\n",
"### **Problem 9:** Sorting a list \n",
"Explain the main difference between `.sort()` method for lists and `sorted()` built-in function. Format both functions as code, every time you mention them.\n",
"\n",
"Include a couple of executable examples to illustrate your explanation."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "GgILSU1Nt3Uh"
},
"source": [
"--- \n",
"***Write your answer here:*** \n",
"\n",
"...\n",
"\n",
"..."
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "3m0LBjKjayBD"
},
"outputs": [],
"source": [
"#Examples:\n",
"#..."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "LWuIiwqau7fh"
},
"source": [
"---\n",
"\n",
"### **Problem 10:** Using `*args` (arbitraty function arguments)\n",
"Write a function **`find_max`** that would take several numbers as inputs *(you don't know how many)*, and print out *the largest of all inputs* (see examples below), **or**, if there are no inputs, just state `\"No inputs found.\"`. Do not use an existing `max()` function (just pretend it doesn't exist).\n",
"\n",
"***Algorithm to use:*** Assign the first input element to `max_v` -- that's the variable to keep track of the maximum value so far. Then go through each element of `args`, and if the element is greater than `max_v`, make it a new `max_v`."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "uXG5uf52MCKu"
},
"source": [
"---\n",
"\n",
"***Your code goes here***: "
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "pvpR10efveRS"
},
"outputs": [],
"source": [
"#Your code goes here:\n",
"#Function definition:\n",
"def find_max(*args):\n",
" #..."
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"id": "OGYwa_OUvsah",
"outputId": "1994fa84-8cb3-4940-8bf2-198114647efc"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Maximum value: 65\n"
]
}
],
"source": [
"#Checking:\n",
"find_max(34, 23, 55, 51, 65)"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"id": "rT6Wq87IwozB",
"outputId": "f9d0cbd2-6fa1-4556-9f92-f50b10459ca8"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Maximum value: 3\n"
]
}
],
"source": [
"#Checking:\n",
"find_max(3)"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"id": "-1ccI0SPOLXf",
"outputId": "d4d2c3fc-7d0a-4b06-d6f4-eb484f0eed09"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"No inputs found.\n"
]
}
],
"source": [
"#Checking:\n",
"find_max()"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "BN8C-0-ewN9B"
},
"source": [
"---\n",
"\n",
"### **Problem 11:** Using `**kargs` (arbitraty **keyword** arguments)\n",
"This one is similar with previous problem, but deals with keyword inputs. \n",
"\n",
"Write a function **`find_max_key()`** that would take several keyword arguments as inputs *(again, the values should be numbers)*, and print out *the **key** with the largest value from all inputs* (see examples below). Do not use an existing `max()` function."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "k5F0r7UDMG4K"
},
"source": [
"---\n",
"\n",
"***Your code goes here***: "
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {},
"colab_type": "code",
"id": "kUu8YJNOwK6C"
},
"outputs": [],
"source": [
"#Your code goes here:\n",
"#Function definition:\n",
"def find_max_key(*args, **kargs):\n",
" #..."
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"id": "gy-cquAxyiuo",
"outputId": "47f46675-bd7a-417c-a14e-d176cb76a4a5"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Key with max value: big = 47\n"
]
}
],
"source": [
"#Checking:\n",
"find_max_key(a=34, b= 15, c=45, big=47)"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"id": "Z9NgLpRqT1K-",
"outputId": "b0f95d1a-7911-4cb2-c154-f3d82619e8b2"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Key with max value: b = 115\n"
]
}
],
"source": [
"#Checking:\n",
"find_max_key(a=34, b= 115, c=45, d=47)"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"id": "-QTadUhLysNh",
"outputId": "b7f578a0-7375-4e0d-9aa7-422ca4bf7d51"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"No keyword inputs found.\n"
]
}
],
"source": [
"#Checking:\n",
"find_max_key()"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 34
},
"colab_type": "code",
"id": "wg1DwaDgTteQ",
"outputId": "fa34cf48-87e6-4680-feaf-c7939426c135"
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"No keyword inputs found.\n"
]
}
],
"source": [
"\n",
"find_max_key(2,3,4)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text",
"id": "jf_XVYKHgrEw"
},
"source": [
"**Make sure to rename the notebook as required.**\n",
"\n",
"**Make sure to save your notebook before closing!**\n"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [],
"name": "HW1_YourName.ipynb",
"provenance": []
},
"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.4"
}
},
"nbformat": 4,
"nbformat_minor": 1
}