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
232 changes: 202 additions & 30 deletions your-code/main.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,34 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 6,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[3, 4, 5, 6, 8, 9, 10, 10, 11, 12]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code here\n"
"my_list = [1, 2, 3, 4, 6, 7, 8, 8, 9, 10]\n",
"lambda_func = lambda x: x + 2\n",
"my_list2 = []\n",
"\n",
"\n",
"def modify_list (y,z):\n",
" my_list2 = []\n",
" for i in y:\n",
" my_list2.append(z(i))\n",
" return my_list2\n",
"\n",
"\n",
"modify_list(my_list, lambda_func)\n"
]
},
{
Expand All @@ -52,11 +75,22 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 9,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"288.15"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your code here:\n"
"(lambda x : x + 273.15)(15)"
]
},
{
Expand All @@ -68,13 +102,24 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 38,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[285.15, 296.15, 311.15, 218.14999999999998, 297.15]"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temps = [12, 23, 38, -55, 24]\n",
"\n",
"# Your code here:\n"
"[(lambda x : x + 273.15)(x) for x in temps]\n"
]
},
{
Expand All @@ -88,11 +133,23 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 25,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n"
]
}
],
"source": [
"# Your code here:\n"
"mod = lambda x,y : 1 if x%y == 0 else 0\n",
"\n",
"print(mod(1,2)) \n",
"print(mod(4,2))"
]
},
{
Expand All @@ -106,16 +163,27 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 30,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<function <lambda> at 0x7fdb390dca60>\n"
]
}
],
"source": [
"def divisor(a):\n",
" \"\"\"\n",
" input: a number\n",
" output: a function that returns 1 if the number is divisible by another number (to be passed later) and zero otherwise\n",
" \"\"\"\n",
" # Your code here:\n"
" new_mod = lambda x: mod(x,a)\n",
" return(new_mod)\n",
"\n",
"\n"
]
},
{
Expand All @@ -127,11 +195,23 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 31,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"<function __main__.divisor.<locals>.<lambda>(x)>"
]
},
"execution_count": 31,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your code here:\n"
"divisible5 = divisor(5)\n",
"divisible5"
]
},
{
Expand All @@ -143,18 +223,40 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 32,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"divisible5(10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 33,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"divisible5(8)"
]
Expand Down Expand Up @@ -209,17 +311,44 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 36,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(1, 2), (2, 3), (4, 3), (4, 5)]\n"
]
},
{
"data": {
"text/plain": [
"[False, False, True, False]"
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list1 = [1,2,4,4]\n",
"list2 = [2,3,3,5]\n",
"## Zip the lists together \n",
"\n",
"zipped_1_2= list(zip(list1, list2))\n",
"\n",
"\n",
"## Print the zipped list \n",
"\n",
"## Use a lambda expression to compare if: list1 element > list2 element\n"
"print(zipped_1_2)\n",
"\n",
"\n",
"## Use a lambda expression to compare if: list1 element > list2 element\n",
"\n",
"[(lambda x: True if x[0] > x[1] else False)(x)for x in zipped_1_2]\n",
"\n"
]
},
{
Expand All @@ -242,14 +371,54 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 44,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[('Engineering', 'Lab'),\n",
" ('Computer Science', 'Homework'),\n",
" ('Political Science', 'Essay'),\n",
" ('Mathematics', 'Module')]"
]
},
"execution_count": 44,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list1 = ['Engineering', 'Computer Science', 'Political Science', 'Mathematics']\n",
"list2 = ['Lab', 'Homework', 'Essay', 'Module']\n",
"\n",
"# Your code here:\n"
"new_list = list(zip(list1, list2))\n",
"new_list"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('Political Science', 'Essay'),\n",
" ('Computer Science', 'Homework'),\n",
" ('Engineering', 'Lab'),\n",
" ('Mathematics', 'Module')]"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"new_list.sort(key=lambda y: y[1])\n",
"\n",
"new_list\n"
]
},
{
Expand Down Expand Up @@ -281,8 +450,11 @@
}
],
"metadata": {
"interpreter": {
"hash": "b96152229c0aa041890ed6e500eda9d95de98ffd377b3d5c0519f522a619c9ec"
},
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3.9.7 ('base')",
"language": "python",
"name": "python3"
},
Expand All @@ -296,7 +468,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.9.7"
}
},
"nbformat": 4,
Expand Down