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
231 changes: 197 additions & 34 deletions your-code/main.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,36 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[4, 7, 8, 1, 12, 14, 166, 18, 3, 2]\n"
]
}
],
"source": [
"# your code here\n"
"initial_list = [2, 5, 6, -1, 10, 12, 164, 16, 1, 0]\n",
"\n",
"add_two = lambda x : x + 2\n",
"\n",
"updated_list = []\n",
"\n",
"def modify_list(initial_list, add_two):\n",
" '''\n",
" this function takes as input an initial list of 10 numbers\n",
" and a lambda expression that adds 2 to each number of the list\n",
" it returns an updated list where the initial numbers are incremented by 2\n",
" '''\n",
" for number in initial_list:\n",
" updated_list.append(add_two(number))\n",
" \n",
"modify_list(initial_list, add_two)\n",
" \n",
"print(updated_list)"
]
},
{
Expand All @@ -52,11 +77,11 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"# Your code here:\n"
"C_to_K = lambda x : round((x + 273.15), 2)"
]
},
{
Expand All @@ -68,13 +93,56 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 14,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[285.15, 296.15, 311.15, 218.15, 297.15]\n"
]
}
],
"source": [
"temps = [12, 23, 38, -55, 24]\n",
"\n",
"# Your code here:\n"
"K_temps = []\n",
"\n",
"def modify_temps(temps, C_to_K):\n",
" '''\n",
" this function takes as input an initial list of temperatures in Celsius\n",
" and a lambda expression that converts a temperature from Celsius to Kelvin\n",
" it returns a list where the initial temperatures have been converted\n",
" '''\n",
" for temperature in temps:\n",
" K_temps.append(C_to_K(temperature))\n",
" \n",
"modify_temps(temps, C_to_K)\n",
"\n",
"print(K_temps)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[285.15, 296.15, 311.15, 218.15, 297.15]"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# with list comprehension\n",
"K_temps = [C_to_K(temperature) for temperature in temps]\n",
"K_temps"
]
},
{
Expand All @@ -88,11 +156,11 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"# Your code here:\n"
"mod = lambda x, y : 1 if (x % y == 0 or y % x == 0) else 0"
]
},
{
Expand All @@ -106,7 +174,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -115,7 +183,8 @@
" 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"
" mod = lambda x : 1 if (a % x == 0 or x % a == 0) else 0\n",
" return mod"
]
},
{
Expand All @@ -127,11 +196,11 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
"# Your code here:\n"
"divisible5 = divisor(5)"
]
},
{
Expand All @@ -143,18 +212,40 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 19,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"divisible5(10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 20,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"divisible5(8)"
]
Expand All @@ -174,7 +265,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 21,
"metadata": {},
"outputs": [
{
Expand All @@ -186,15 +277,17 @@
" ('tomato', 'tomato')]"
]
},
"execution_count": 1,
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list1 = ['Green', 'cheese', 'English', 'tomato']\n",
"list2 = ['eggs', 'cheese', 'cucumber', 'tomato']\n",
"zipped = zip(list1,list2)\n",
"\n",
"zipped = zip(list1, list2)\n",
"\n",
"list(zipped)"
]
},
Expand All @@ -209,17 +302,42 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 37,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(1, 2), (2, 3), (4, 3), (4, 5)]\n"
]
},
{
"data": {
"text/plain": [
"[0, 0, 1, 0]"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list1 = [1,2,4,4]\n",
"list2 = [2,3,3,5]\n",
"list1 = [1, 2, 4, 4]\n",
"list2 = [2, 3, 3, 5]\n",
"\n",
"## Zip the lists together \n",
"zipped = zip(list1, list2)\n",
"\n",
"## Print the zipped list \n",
"print(list(zipped)) # zipped is consumed here, so need to re-make it down\n",
"\n",
"## Use a lambda expression to compare if: list1 element > list2 element\n"
"## Use a lambda expression to compare if: list1 element > list2 element\n",
"compare = lambda x, y : 1 if x > y else 0\n",
"\n",
"result = [compare(x, y) for x, y in zip(list1, list2)]\n",
"result"
]
},
{
Expand All @@ -242,14 +360,45 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 40,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('Engineering', 'Lab'), ('Computer Science', 'Homework'), ('Political Science', 'Essay'), ('Mathematics', 'Module')]\n"
]
},
{
"data": {
"text/plain": [
"[('Political Science', 'Essay'),\n",
" ('Computer Science', 'Homework'),\n",
" ('Engineering', 'Lab'),\n",
" ('Mathematics', 'Module')]"
]
},
"execution_count": 40,
"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"
"# Zip the lists together\n",
"zipped = zip(list1, list2)\n",
"\n",
"# Print the zipped list\n",
"print(list(zipped))\n",
"\n",
"# Sort the lists by using a lambda expression that calls upon the second element of each tuple\n",
"sort = lambda x : x[1][0]\n",
"\n",
"result = sorted(zip(list1, list2), key=sort)\n",
"result"
]
},
{
Expand All @@ -263,13 +412,27 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 42,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[('Toyota', 1995), ('Honda', 1997), ('Audi', 2001), ('BMW', 2005)]"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n",
"\n",
"# Your code here:\n"
"sort = lambda x : x[1]\n",
"\n",
"result = sorted(d.items(), key=sort)\n",
"result"
]
},
{
Expand All @@ -282,7 +445,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
Expand All @@ -296,7 +459,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.10.10"
}
},
"nbformat": 4,
Expand Down