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
193 changes: 160 additions & 33 deletions your-code/main.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,29 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 7,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[9, 92, 34, 63, 7, 86, 53, 9, 53, 32]\n",
"[11, 94, 36, 65, 9, 88, 55, 11, 55, 34]\n"
]
}
],
"source": [
"# your code here\n"
"import random\n",
"\n",
"random_list = [random.randint(1, 100) for _ in range(10)]\n",
"print(random_list)\n",
"\n",
"update_number = lambda x: x + 2\n",
"\n",
"empty_list = [update_number(x) for x in random_list]\n",
"\n",
"print(empty_list)"
]
},
{
Expand All @@ -52,11 +70,11 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"# Your code here:\n"
"convert_temperature = lambda x: x + 273.15\n"
]
},
{
Expand All @@ -68,13 +86,25 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 11,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[285.15, 296.15, 311.15, 218.14999999999998, 297.15]"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"temps = [12, 23, 38, -55, 24]\n",
"\n",
"# Your code here:\n"
"temps_Kelvin = [convert_temperature(x) for x in temps]\n",
"temps_Kelvin\n"
]
},
{
Expand All @@ -88,11 +118,11 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"# Your code here:\n"
"mod = lambda x, y: 1 if x % y == 1 or y % x == 1 else 0"
]
},
{
Expand All @@ -106,16 +136,13 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 14,
"metadata": {},
"outputs": [],
"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"
"def divisor(x):\n",
" mod = lambda y: 1 if x % y == 0 or y % x == 0 else 0\n",
" return mod\n"
]
},
{
Expand All @@ -127,11 +154,18 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"# Your code here:\n"
"divisor = 5\n",
"\n",
"def get_mod(x):\n",
" mod = lambda y: 1 if x % y == 0 or y % x == 0 else 0\n",
" return mod\n",
"\n",
"divisible5 = get_mod(divisor)\n",
"\n"
]
},
{
Expand All @@ -143,18 +177,40 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"divisible5(10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"divisible5(8)"
]
Expand All @@ -174,7 +230,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 6,
"metadata": {},
"outputs": [
{
Expand All @@ -186,7 +242,7 @@
" ('tomato', 'tomato')]"
]
},
"execution_count": 1,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -209,17 +265,48 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 15,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[(1, 2), (2, 3), (4, 3), (4, 5)]"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"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",
"\n",
"## Use a lambda expression to compare if: list1 element > list2 element\n"
"list(zipped)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[False, False, True, False]\n"
]
}
],
"source": [
"comparison_result = [x > y for x, y in zip(list1, list2)]\n",
"print(comparison_result)"
]
},
{
Expand All @@ -242,14 +329,54 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 31,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[('Engineering', 'Lab'),\n",
" ('Computer Science', 'Homework'),\n",
" ('Political Science', 'Essay'),\n",
" ('Mathematics', 'Module')]"
]
},
"execution_count": 31,
"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"
"zipped = zip(list1, list2)\n",
"zipped = list(zipped)\n",
"zipped"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('Political Science', 'Essay'),\n",
" ('Computer Science', 'Homework'),\n",
" ('Engineering', 'Lab'),\n",
" ('Mathematics', 'Module')]"
]
},
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"sorted_zipped = sorted(zipped, key=lambda x: x[1][0])\n",
"sorted_zipped"
]
},
{
Expand Down Expand Up @@ -296,7 +423,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.3"
"version": "3.10.9"
}
},
"nbformat": 4,
Expand Down