Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
166 changes: 140 additions & 26 deletions your-code/main.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,14 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"def modify_list(lst, lmbda):\n",
" \"\"\"\n",
" Input: list and lambda expression\n",
" Output: the transformed list\n",
" \"\"\"\n",
" \n",
" # your code here\n",
" \n",
" return list(map(lmbda , lst))\n",
" "
]
},
Expand All @@ -46,11 +43,24 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 16,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"<function __main__.<lambda>(x)>"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code here"
"to_kelvin = lambda x : x + 273.15\n",
"\n",
"to_kelvin"
]
},
{
Expand All @@ -62,13 +72,31 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"temps = [12, 23, 38, -55, 24]\n",
"\n",
"# your code here"
"temps = [12, 23, 38, -55, 24]"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[285.15, 296.15, 311.15, 218.14999999999998, 297.15]"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"modify_list (temps,to_kelvin)"
]
},
{
Expand All @@ -84,9 +112,22 @@
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code here"
"mod = lambda a,b : 1 if a % b == 0 else 0\n",
"\n",
"mod(8,4)\n"
]
},
{
Expand All @@ -100,7 +141,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -110,6 +151,10 @@
" Output: a function that returns 1 if the number is \n",
" divisible by another number (to be passed later) and zero otherwise.\n",
" \"\"\"\n",
" return lambda a : mod(a,b)\n",
"\n",
"\n",
"\n",
" \n",
" # your code here"
]
Expand All @@ -123,11 +168,11 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# your code here"
"divisible5 = divisor(5)"
]
},
{
Expand All @@ -139,18 +184,40 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"divisible5(10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"divisible5(8)"
]
Expand All @@ -159,7 +226,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Bonus Challenge - Using Lambda Expressions in List Comprehensions\n",
"# <span class=\"burk\">Bonus Challenge - Using Lambda Expressions in List Comprehensions</span>\n",
"\n",
"In the following challenge, we will combine two lists using a lambda expression in a list comprehension. \n",
"\n",
Expand Down Expand Up @@ -213,7 +280,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Bonus Challenge - Using Lambda Expressions as Arguments\n",
"# <span class=\"burk\">Bonus Challenge - Using Lambda Expressions as Arguments</span>\n",
"\n",
"#### In this challenge, we will zip together two lists and sort by the resulting tuple.\n",
"\n",
Expand All @@ -226,6 +293,9 @@
"metadata": {},
"outputs": [],
"source": [
"# BONUS, NO NEEDED TO DO IT\n",
"\n",
"\n",
"list1 = ['Engineering', 'Computer Science', 'Political Science', 'Mathematics']\n",
"list2 = ['Lab', 'Homework', 'Essay', 'Module']\n",
"\n",
Expand All @@ -236,7 +306,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Bonus Challenge - Sort a Dictionary by Values\n",
"# <span class=\"burk\">Bonus Challenge - Sort a Dictionary by Values</span>\n",
"\n",
"Given the dictionary below, sort it by values rather than by keys. Use a lambda function to specify the values as a sorting key."
]
Expand All @@ -247,6 +317,8 @@
"metadata": {},
"outputs": [],
"source": [
"#BONUS, NO NEEDED TO DO IT\n",
"\n",
"d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n",
"\n",
"# your code here"
Expand All @@ -269,7 +341,49 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
"version": "3.8.5"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": true
},
"varInspector": {
"cols": {
"lenName": 16,
"lenType": 16,
"lenVar": 40
},
"kernels_config": {
"python": {
"delete_cmd_postfix": "",
"delete_cmd_prefix": "del ",
"library": "var_list.py",
"varRefreshCmd": "print(var_dic_list())"
},
"r": {
"delete_cmd_postfix": ") ",
"delete_cmd_prefix": "rm(",
"library": "var_list.r",
"varRefreshCmd": "cat(var_dic_list()) "
}
},
"types_to_exclude": [
"module",
"function",
"builtin_function_or_method",
"instance",
"_Feature"
],
"window_display": false
}
},
"nbformat": 4,
Expand Down