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
246 changes: 214 additions & 32 deletions your-code/main.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,53 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"import math"
]
},
{
"cell_type": "code",
"execution_count": 81,
"metadata": {},
"outputs": [],
"source": [
"def modify_list(lst, lmbda):\n",
" \"\"\"\n",
" Input: list and lambda expression\n",
" Output: the transformed list\n",
" return square\n",
" \"\"\"\n",
" # your code here, I chose the function to return square to every number in a list \n",
" i = 0\n",
" \n",
" # your code here\n",
" "
" while i < len(lst):\n",
" lst[i] = lmbda(lst[i])\n",
" i += 1 \n"
]
},
{
"cell_type": "code",
"execution_count": 82,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "'int' object is not callable",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-82-47ac0589ba82>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mmodify_list\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-81-73221edf975e>\u001b[0m in \u001b[0;36mmodify_list\u001b[0;34m(lst, lmbda)\u001b[0m\n\u001b[1;32m 9\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 10\u001b[0m \u001b[0;32mwhile\u001b[0m \u001b[0mi\u001b[0m \u001b[0;34m<\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlst\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 11\u001b[0;31m \u001b[0mlst\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mlmbda\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mlst\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0mi\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 12\u001b[0m \u001b[0mi\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mTypeError\u001b[0m: 'int' object is not callable"
]
}
],
"source": [
"modify_list([2,3], 3)"
]
},
{
Expand All @@ -46,11 +81,12 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 47,
"metadata": {},
"outputs": [],
"source": [
"# your code here"
"# your code here\n",
"celsius_to_kelvin = lambda x: x + 273.15"
]
},
{
Expand All @@ -62,13 +98,31 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 52,
"metadata": {},
"outputs": [],
"source": [
"temps = [12, 23, 38, -55, 24]\n",
"\n",
"# your code here"
"# your code here\n",
"kelvin = list(map(celsius_to_kelvin, temps))"
]
},
{
"cell_type": "code",
"execution_count": 53,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[285.15, 296.15, 311.15, 218.14999999999998, 297.15]\n"
]
}
],
"source": [
"print(kelvin)"
]
},
{
Expand All @@ -82,11 +136,32 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 49,
"metadata": {},
"outputs": [],
"source": [
"# your code here"
"# your code here\n",
"mod = lambda x, y: 1 if x % y == 0 or y % x == 0 else 0"
]
},
{
"cell_type": "code",
"execution_count": 51,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mod(4,2)"
]
},
{
Expand All @@ -100,7 +175,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 83,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -111,7 +186,12 @@
" divisible by another number (to be passed later) and zero otherwise.\n",
" \"\"\"\n",
" \n",
" # your code here"
" # your code here\n",
" \n",
" for i in range(2,b):\n",
" if mod(i,b) == 1:\n",
" return 1 \n",
" return 0"
]
},
{
Expand All @@ -123,11 +203,41 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 84,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 84,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code here"
"divisor(5)"
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n"
]
}
],
"source": [
"# your code here\n",
"divisible5 = divisor(5)\n",
"print(divisible5)\n"
]
},
{
Expand All @@ -139,20 +249,42 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 89,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 89,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"divisible5(10)"
"divisor(10)"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 88,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 88,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"divisible5(8)"
"divisor(8)"
]
},
{
Expand All @@ -168,7 +300,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 30,
"metadata": {},
"outputs": [
{
Expand All @@ -177,7 +309,7 @@
"[(1,), (2,), (3,), (4,), (5,)]"
]
},
"execution_count": 10,
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -199,14 +331,30 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 69,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[('Green', 'eggs'),\n",
" ('cheese', 'cheese'),\n",
" ('English', 'cucumber'),\n",
" ('tomato', 'tomato')]"
]
},
"execution_count": 69,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list1 = ['Green', 'cheese', 'English', 'tomato']\n",
"list2 = ['eggs', 'cheese', 'cucumber', 'tomato']\n",
"\n",
"# your code here"
"\n",
"# your code here\n",
"[x for x in zip(list1,list2)]"
]
},
{
Expand All @@ -222,14 +370,29 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 70,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[('Engineering', 'Lab'),\n",
" ('Computer Science', 'Homework'),\n",
" ('Political Science', 'Essay'),\n",
" ('Mathematics', 'Module')]"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list1 = ['Engineering', 'Computer Science', 'Political Science', 'Mathematics']\n",
"list2 = ['Lab', 'Homework', 'Essay', 'Module']\n",
"\n",
"# your code here"
"# your code here\n",
"[x for x in zip(list1, list2)]"
]
},
{
Expand All @@ -243,14 +406,33 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 71,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[('Toyota', 1995), ('Honda', 1997), ('Audi', 2001), ('BMW', 2005)]"
]
},
"execution_count": 71,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"d = {'Honda': 1997, 'Toyota': 1995, 'Audi': 2001, 'BMW': 2005}\n",
"\n",
"# your code here"
"# your code here\n",
"sorted(d.items(), key = lambda x: x[1])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -269,7 +451,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.2"
"version": "3.8.5"
}
},
"nbformat": 4,
Expand Down