Skip to content

[lab-lambda-functions] Bruno Santos #196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
201 changes: 169 additions & 32 deletions your-code/main.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,44 @@
"metadata": {},
"outputs": [],
"source": [
"# your code here\n"
"# your code here\n",
"list3 = [1,2,3,4,5,6,7,8,9,10]\n",
"lambda_function = lambda x: x * 2"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def modify_list(a,b):\n",
" empty_list = []\n",
" for item in a:\n",
" item2 = b(item)\n",
" empty_list.append(item2)\n",
" return empty_list"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[6, 8, 10, 8, 6, 4, 2, 6, 14]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list2 = modify_list([3,4,5,4,3,2,1,3,7], lambda_function)\n",
"list2 "
]
},
{
Expand All @@ -52,11 +89,12 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# Your code here:\n"
"# Your code here:\n",
"a = lambda x: x + 273.15"
]
},
{
Expand All @@ -68,13 +106,25 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 8,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[285.15, 296.15, 311.15, 218.14999999999998, 297.15]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temps = [12, 23, 38, -55, 24]\n",
"\n",
"# Your code here:\n"
"temps_kelvin = []\n",
"# Your code here:\n",
"list(map(a, temps))"
]
},
{
Expand All @@ -88,11 +138,12 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"# Your code here:\n"
"# Your code here:\n",
"mod = lambda a, x: 1 if x%a==0 else 0 "
]
},
{
Expand All @@ -106,16 +157,17 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"def divisor(a):\n",
"def divisor(a,x):\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"
" # Your code here:\n",
" return mod(a,x)\n"
]
},
{
Expand All @@ -127,11 +179,13 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"# Your code here:\n"
"# Your code here:\n",
"def divisible5(x):\n",
" return divisor(5,x)\n"
]
},
{
Expand All @@ -143,18 +197,40 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 23,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"divisible5(10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 24,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"divisible5(8)"
]
Expand Down Expand Up @@ -209,17 +285,32 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 153,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[False, False, True, False]"
]
},
"execution_count": 153,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list1 = [1,2,4,4]\n",
"list2 = [2,3,3,5]\n",
"## Zip the lists together \n",
"\n",
"## Print the zipped list \n",
"\n",
"## Use a lambda expression to compare if: list1 element > list2 element\n"
"## Zip the lists together\n",
"zipped2 = zip(list1,list2)\n",
"## Print the zipped list\n",
"zipped2 = list(zipped2)\n",
"## Use a lambda expression to compare if: list1 element > list2 element\n",
"compare = lambda x, y: x > y\n",
"compared = [compare(x, y) for x, y in zipped2 ]\n",
"compared\n",
"\n"
]
},
{
Expand All @@ -242,14 +333,31 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 192,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[('Political Science', 'Essay'),\n",
" ('Computer Science', 'Homework'),\n",
" ('Engineering', 'Lab'),\n",
" ('Mathematics', 'Module')]"
]
},
"execution_count": 192,
"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"
"# Your code here:\n",
"zipped = zip(list1,list2)\n",
"letter1 = lambda x: x[1][0]\n",
"result = sorted(zip(list1, list2), key=lambda x: letter1(x))\n",
"result"
]
},
{
Expand All @@ -263,13 +371,42 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 235,
"metadata": {},
"outputs": [],
"source": [
"def Convert(tup, di):\n",
" for a, b in tup:\n",
" di.setdefault(a, []).append(b)\n",
" return di"
]
},
{
"cell_type": "code",
"execution_count": 236,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'Toyota': [1995], 'Honda': [1997], 'Audi': [2001], 'BMW': [2005]}"
]
},
"execution_count": 236,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n",
"\n",
"# Your code here:\n"
"# Your code here:\n",
"dict_result ={}\n",
"\n",
"letter1 = lambda x: x[1]\n",
"result = sorted(list(d.items()), key=lambda x: letter1(x))\n",
"#dict = dict(sorted(d.items(), key=lambda x: x[1]))\n",
"Convert(result,dict_result)"
]
},
{
Expand All @@ -296,7 +433,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.11.2"
}
},
"nbformat": 4,
Expand Down