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
263 changes: 234 additions & 29 deletions lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,37 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "21625526-3fae-4c55-bab5-f91940070681",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"['Open', 'Open', 'Senior', 'Open', 'Open', 'Senior']"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"input = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]\n",
"\n",
"def categorize(list):\n",
" new_list = []\n",
" for l in list:\n",
" if l[1] < -2 or l[1] > 26:\n",
" print(\"Error: Handicap out of range\", l)\n",
" elif l[0] >= 55 and l[1] > 7:\n",
" new_list.append(\"Senior\")\n",
" else:\n",
" new_list.append(\"Open\")\n",
" return new_list\n",
"\n",
"categorize(input)"
]
},
{
Expand All @@ -95,12 +120,48 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"id": "53061dce-7aa3-4476-8b56-71413c5e135c",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{0, 3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24, 25, 27, 30, 33, 35, 36, 39, 40, 42, 45, 48}\n"
]
},
{
"data": {
"text/plain": [
"543"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"def sum_mult(number):\n",
" if number == 0:\n",
" return 0\n",
" n = []\n",
" s = set()\n",
" for i in range(number):\n",
" n.append(i)\n",
"\n",
" for i in range(len(n)):\n",
" if n[i] %3 == 0:\n",
" s.add(n[i])\n",
" elif n[i] %5 == 0:\n",
" s.add(n[i])\n",
" print(s)\n",
" return sum(s)\n",
"\n",
"sum_mult(50)\n"
]
},
{
Expand All @@ -127,12 +188,41 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"id": "a8cb1579-7065-4fc0-bd53-f91c2ad1dad9",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[8, 6, 7, 5, 3, 0, 9]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"def return_Array():\n",
"\n",
" arr = []\n",
"\n",
" while True:\n",
" try:\n",
" n = input(\"Enter a number: \")\n",
" if int(n) < 0:\n",
" print(\"Please enter a non-negative integer.\")\n",
" else:\n",
" break\n",
" except ValueError:\n",
" print(\"Invalid input. Please enter a valid integer.\")\n",
" for i in range(len(n)):\n",
" arr.append(int(n[i]))\n",
" return arr\n",
"\n",
"return_Array()"
]
},
{
Expand All @@ -155,12 +245,30 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"id": "86ebe759-76d8-4012-8590-c48a473a6099",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[-1, 2, -3, 4, -5]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"def invert(list):\n",
" inv_list = []\n",
" for element in list:\n",
" inv_list.append(-element)\n",
" return inv_list\n",
"\n",
"invert([1, -2, 3, -4, 5])\n"
]
},
{
Expand Down Expand Up @@ -193,12 +301,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"id": "6711881b-450a-44cb-a3d0-367b2c6a4464",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{1: 1, 2: 4, 3: 9, 4: 16, 5: 25, 6: 36, 7: 49, 8: 64, 9: 81, 10: 100, 11: 121, 12: 144, 13: 169, 14: 196, 15: 225}\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"i = 15\n",
"d = {}\n",
"for i in range(1, i+1):\n",
" d[i] = i**2\n",
"\n",
"print(d)\n"
]
},
{
Expand All @@ -220,12 +342,31 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"id": "a1d55cea-96c3-4853-8220-17c0904a8816",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[(1, 3), (1, 4), (2, 3), (2, 4)]"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"def all_possible_pairs(list1, list2):\n",
" pairs = []\n",
" for i in range(len(list1)):\n",
" for j in range(len(list2)):\n",
" pairs.append((list1[i], list2[j]))\n",
" return pairs\n",
"\n",
"all_possible_pairs([1, 2], [3, 4])"
]
},
{
Expand All @@ -246,12 +387,34 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"id": "0175239c-87fa-4ba0-8776-d62567256db7",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[('a', 1), ('a', 2), ('b', 3), ('b', 4)]"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"def a12(di):\n",
" li = []\n",
" for key in di:\n",
" li.append((key, di[key][0]))\n",
" li.append((key, di[key][1]))\n",
" return li\n",
"\n",
"a12({'a': [1, 2], 'b': [3, 4]})\n",
"\n",
"\n"
]
},
{
Expand Down Expand Up @@ -281,12 +444,34 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 93,
"id": "248918d0-f082-40a8-9d5c-c7b4ffd2bfca",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[(1, 3), (1, 4), (2, 3), (2, 4), (5, 7), (5, 8), (6, 7), (6, 8)]"
]
},
"execution_count": 93,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"def list_tup2(lista):\n",
" l = []\n",
" for i in range(2):\n",
" for n in range(2):\n",
" p1 = ((lista[i][0][n]),(lista[i][1][0]))\n",
" p2 = ((lista[i][0][n]),(lista[i][1][1]))\n",
" l.append(p1)\n",
" l.append(p2)\n",
" return l\n",
"\n",
"list_tup2([([1, 2], [3, 4]), ([5, 6], [7, 8])])"
]
},
{
Expand All @@ -308,18 +493,38 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 94,
"id": "c57d9e77-6859-45dd-a2e6-2c1898f1baa2",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'a1b2c345'"
]
},
"execution_count": 94,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"def mix_strings(str1, str2):\n",
" mixed = ''\n",
" length = min(len(str1), len(str2))\n",
" for i in range(length):\n",
" mixed += str1[i] + str2[i]\n",
" mixed += str1[length:] + str2[length:]\n",
" return mixed\n",
"\n",
"mix_strings(\"abc\", \"12345\")\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -333,7 +538,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down