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
184 changes: 147 additions & 37 deletions your-code/main.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,31 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[3, 4, 5, 6, 7, 8, 9, 10, 11, 12]\n"
]
}
],
"source": [
"# your code here\n"
"# your code here\n",
"num_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n",
"updated_list = []\n",
"\n",
"add_two = lambda x: x + 2\n",
"\n",
"def update_list(lst, func):\n",
" for num in lst:\n",
" updated_list.append(func(num))\n",
"\n",
"\n",
"update_list(num_list, add_two)\n",
"print(updated_list)"
]
},
{
Expand All @@ -52,11 +72,12 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# Your code here:\n"
"# Your code here:\n",
"conv = lambda celsius: celsius + 273.15"
]
},
{
Expand All @@ -68,13 +89,28 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 7,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[285.15, 296.15, 311.15, 218.14999999999998, 297.15]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temps = [12, 23, 38, -55, 24]\n",
"\n",
"# Your code here:\n"
"# Your code here:\n",
"conv = lambda celsius: celsius + 273.15\n",
"\n",
"temp = list(map(conv, temps))\n",
"temp"
]
},
{
Expand All @@ -88,11 +124,28 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 22,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Your code here:\n"
"# Your code here:\n",
"\n",
"\n",
"mod = lambda x, y: 1 if x % y == 0 or y % x == 0 else 0\n",
" \n",
"res = mod(10,2)\n",
"res"
]
},
{
Expand All @@ -106,16 +159,26 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 56,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"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"
"# Your code here:\n",
"def divisor(x):\n",
" mod = lambda y: 1 if x % y == 0 or y % x == 0 else 0\n",
" return mod\n",
"\n"
]
},
{
Expand All @@ -127,11 +190,11 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 57,
"metadata": {},
"outputs": [],
"source": [
"# Your code here:\n"
"divisible5 = divisor(5) "
]
},
{
Expand All @@ -143,20 +206,44 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 59,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 59,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"divisible5(10)"
"result = divisible5(5)\n",
"result"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 61,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 61,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"divisible5(8)"
"result = divisible5(8)\n",
"result"
]
},
{
Expand Down Expand Up @@ -209,17 +296,29 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 66,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[2, 3, 4, 5]\n"
]
}
],
"source": [
"#In the following challenge, we will combine two lists using a lambda expression in a list comprehension.\n",
"\n",
"#To do this, we will need to introduce the zip function\n",
"\n",
"list1 = [1,2,4,4]\n",
"list2 = [2,3,3,5]\n",
"## Zip the lists together \n",
"\n",
"## Print the zipped list \n",
"res=[(lambda x, y: x if x > y else y)(x, y) for x, y in zip(list1, list2)]\n",
"\n",
"## Use a lambda expression to compare if: list1 element > list2 element\n"
"print(res)\n",
"\n"
]
},
{
Expand All @@ -242,14 +341,25 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 68,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('Political Science', 'Essay'), ('Computer Science', 'Homework'), ('Engineering', 'Lab'), ('Mathematics', 'Module')]\n"
]
}
],
"source": [
"list1 = ['Engineering', 'Computer Science', 'Political Science', 'Mathematics']\n",
"list2 = ['Lab', 'Homework', 'Essay', 'Module']\n",
"\n",
"# Your code here:\n"
"# Your code here:\n",
"list3 = sorted(zip(list1, list2), key=lambda x: x[1][0])\n",
"\n",
"print(list3)"
]
},
{
Expand Down Expand Up @@ -282,7 +392,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -296,7 +406,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.9.13"
}
},
"nbformat": 4,
Expand Down