{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**More Complex Functions**\n",
"\n",
"This discussion we're going to work on some more complicated exercises together to get more practice writing longer (or otherwise harder) functions in Python."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***Exercise 1:*** Write a function `reverse_dict` that takes in a dictionary `d` and reverses it. Specifically, for every *value* `v` in the dictionary, we want a mapping in the new dictionary that is `v: ks` where `ks` is a set containing all the keys that map to `v`.\n",
"\n",
"(see my website [here](https://www.math.ucla.edu/~egeo/classes/f20_pic16a/notebook_week2b.html) or discussion video for solution)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In your next homework, you'll be asked to work with a dictionary by treating it as a simple directed graph. That is, you can \"travel\" from a key to its corresponding value, which might appear again in the dictionary as something you can travel from.\n",
"\n",
"***Exercise 2:*** Write a function `edges` that takes in a dictionary `d` and returns a dictionary that for each element in the dictionary (key or value) associates it with the number of elements you can get either to or from it.\n",
"\n",
"For example, if \n",
"`graph = {'a': 'b', 'b': 'd', 'c': 'd'}` \n",
"then \n",
"`edges(graph) = {'a': 1, 'b': 2, 'c': 1, 'd': 2}` \n",
"since `a` can reach `b`, `b` can be reached by `a` and can reach `d`, `c` can reach `d`, and `d` can be reached by `b` and `c`.\n",
"\n",
"(see my website [here](https://www.math.ucla.edu/~egeo/classes/f20_pic16a/notebook_week2b.html) or discussion video for solution)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Higher-order functions can be pretty hard to reason about. Let's do some practice sorting lists to get a sense of how higher order functions might be used and then write one for ourselves.\n",
"\n",
"Remember the Python method `list.sort()` has an optional parameter `key`. `key` is a function that takes in the elements of the list and returns a number (or any other value we can compare) and sorts based on this value.\n",
"\n",
"***Exercise 3:*** Given a list `l1` of positive integers >= 10, sort them by their second digit. Then, given another list `l2` of lowercase strings, sort them by the number of vowels (\"a\", \"e\", \"i\", \"o\", \"u\"). Lastly, given a third list `l3`, use a suitable function to randomize the list.\n",
"\n",
"(see my website [here](https://www.math.ucla.edu/~egeo/classes/f20_pic16a/notebook_week2b.html) or discussion video for solution)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"***Exercise 4:*** Write a function `sort_comp` that takes in a list `l` and a function `comp`. `comp` will take in two elements from the list `a` and `b` and return -1, 0, or 1. If \n",
"`comp(a,b) < 0`, \n",
"then assume a < b\n",
"`comp(a,b) > 0`, \n",
"then assume a > b\n",
"`comp(a,b) == 0`, \n",
"then assume a = b\n",
"\n",
"(see my website [here](https://www.math.ucla.edu/~egeo/classes/f20_pic16a/notebook_week2b.html) or discussion video for solution)"
]
},
{
"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.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 4
}