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
268 changes: 240 additions & 28 deletions lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,26 @@
"execution_count": null,
"id": "21625526-3fae-4c55-bab5-f91940070681",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['Open', 'Open', 'Senior', 'Open', 'Open', 'Senior']\n"
]
}
],
"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 katas_1(input):\n",
" \"\"\"It takes input(list of pairs) and it returns new_list with `Senior` or `Open` based\n",
" on the rules provided by the contract above.\"\"\"\n",
" new_list =[\"Senior\" if l[0]>=55 and l[1]>7 else \"Open\" for l in input]\n",
" return new_list\n",
"\n",
"print(katas_1(input))"
]
},
{
Expand All @@ -95,12 +112,52 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"id": "53061dce-7aa3-4476-8b56-71413c5e135c",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"78"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here\n",
"\n",
"def solution(number):\n",
" new_list1 = [n if (n % 3 == 0 or n % 5 == 0) and n >= 0 else 0 for n in range (0, number)]\n",
" suma_of_multiples = sum(new_list1)\n",
" return suma_of_multiples\n",
"solution(20)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c002789c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"9333166668"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"def solution(number):\n",
" return sum(n for n in range(number) if n % 3 == 0 or n % 5 == 0)\n",
"solution(2)"
]
},
{
Expand All @@ -127,12 +184,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 29,
"id": "a8cb1579-7065-4fc0-bd53-f91c2ad1dad9",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3]"
]
},
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"def solution2(number):\n",
" new_list2 = [int(s) for s in str(number) if number > 0 and type(number) == int]\n",
" return new_list2\n",
"solution2(123)"
]
},
{
Expand All @@ -155,12 +228,27 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 47,
"id": "86ebe759-76d8-4012-8590-c48a473a6099",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[-1, -2, -3, -4, -5]"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"def invert(numbers):\n",
" new_list3 = [-n if n > 0 else -n for n in numbers]\n",
" return new_list3\n",
"invert([1, 2, 3,4,5])"
]
},
{
Expand Down Expand Up @@ -193,12 +281,40 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 51,
"id": "6711881b-450a-44cb-a3d0-367b2c6a4464",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"{1: 1,\n",
" 2: 4,\n",
" 3: 9,\n",
" 4: 16,\n",
" 5: 25,\n",
" 6: 36,\n",
" 7: 49,\n",
" 8: 64,\n",
" 9: 81,\n",
" 10: 100,\n",
" 11: 121,\n",
" 12: 144,\n",
" 13: 169,\n",
" 14: 196,\n",
" 15: 225}"
]
},
"execution_count": 51,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"new_dict = {n: n**2 for n in range(1, 16)}\n",
"new_dict"
]
},
{
Expand All @@ -220,12 +336,31 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 60,
"id": "a1d55cea-96c3-4853-8220-17c0904a8816",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[(1, 3), (1, 4), (2, 3), (2, 4)]"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"def pairs(one_list, another_list):\n",
" \"\"\"It takes two list of integers and returns a list of all possible pairs \n",
" of integers where the first integer is from the first list and the second\n",
" integer is from the second list.\"\"\"\n",
" new_list = [(n, n2) for n in one_list for n2 in another_list]\n",
" return new_list\n",
"pairs([1,2], [3,4])"
]
},
{
Expand All @@ -246,12 +381,28 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 89,
"id": "0175239c-87fa-4ba0-8776-d62567256db7",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[('a', 1), ('a', 2), ('b', 3), ('b', 4)]"
]
},
"execution_count": 89,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"def key_value_pairs(input_dict):\n",
" new_dict = [(key, v) for key, values in input_dict.items() for v in values]\n",
" return new_dict\n",
"key_value_pairs({\"a\": [1, 2], \"b\": [3, 4]})"
]
},
{
Expand Down Expand Up @@ -281,12 +432,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 107,
"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": 107,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"def pairs_of_numbers(list_of_tuples):\n",
" all_pairs = [(n1, n2) for l1, l2 in list_of_tuples for n1 in l1 for n2 in l2]\n",
" return all_pairs\n",
"\n",
"pairs_of_numbers([([1, 2], [3, 4]), ([5, 6], [7, 8])])"
]
},
{
Expand All @@ -308,18 +476,62 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 109,
"id": "c57d9e77-6859-45dd-a2e6-2c1898f1baa2",
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"{('a', 'b'), ('c', 'd')}"
]
},
"execution_count": 109,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"def tying_characters(list_of_strings):\n",
" set_of_characters = set((char1, char2) for s1, s2 in list_of_strings for char1 in s1 for char2 in s2)\n",
" return set_of_characters\n",
"\n",
"tying_characters([\"ab\", \"cd\"])"
]
},
{
"cell_type": "code",
"execution_count": 138,
"id": "40d31ee3",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd')}"
]
},
"execution_count": 138,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# your code goes here\n",
"\n",
"def tying_characters(list_of_strings):\n",
" set_of_characters = set((char1, char2) for char1 in list_of_strings[0] for char2 in list_of_strings[1])\n",
" return set_of_characters\n",
"\n",
"tying_characters([\"ab\", \"cd\"])"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -333,7 +545,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.13.9"
}
},
"nbformat": 4,
Expand Down