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
230 changes: 201 additions & 29 deletions lab-python-list-comprehension.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,20 @@
"\n",
"Output will consist of a list of string values stating whether the respective member is to be placed in the senior or open category.\n",
"\n",
"### Katas - 1\n",
"\n",
"El Club de Croquet de los Suburbios Occidentales ofrece dos categorías de membresía: Senior y Abierta. Solicitan su ayuda con un formulario de solicitud que indicará a los futuros miembros en qué categoría se les asignará.\n",
"\n",
"Para ser senior, un miembro debe tener al menos 55 años y un hándicap superior a 7. En este club de croquet, los hándicaps van de -2 a +26; cuanto mejor sea el jugador, menor será el hándicap.\n",
"\n",
"**Entrada**\n",
"\n",
"La entrada consistirá en una lista de pares. Cada par contiene la información de un posible miembro. La información consiste en un número entero para la edad de la persona y un número entero para su hándicap.\n",
"\n",
"**Salida**\n",
"\n",
"La salida consistirá en una lista de valores de cadena que indicarán si el miembro correspondiente se asignará a la categoría senior o abierta.\n",
"\n",
"**Example**\n",
"\n",
"```python\n",
Expand All @@ -71,12 +85,39 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 10,
"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",
"#Para ser senior, un miembro debe tener al menos 55 años y un hándicap superior a 7. En este club de croquet, \n",
"#los hándicaps van de -2 a +26; cuanto mejor sea el jugador, menor será el hándicap.\n",
"\n",
"def member_respective(list_user):\n",
" list_valores = []\n",
" for edad,handicap in list_user:\n",
" if edad >= 55 and handicap >7:\n",
" list_valores.append(\"Senior\")\n",
" else:\n",
" list_valores.append(\"Open\")\n",
"\n",
" return list_valores\n",
"\n",
"def member_respective_comprenhision(list_user):\n",
" return [\"Senior\" if edad >= 55 and handicap > 7 else \"open\" for edad, handicap in list_user]\n",
"\n",
"input = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]\n",
"\n",
"print(member_respective_comprenhision(input))\n"
]
},
{
Expand All @@ -95,12 +136,35 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 11,
"id": "53061dce-7aa3-4476-8b56-71413c5e135c",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"27\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"def sum_multiplos_3_5(list_num):\n",
" sum=0\n",
" for num in list_num:\n",
" if num > 0:\n",
" if num%3==0 or num%5==0:\n",
" sum+=num \n",
" return sum\n",
"\n",
"def sum_multiplos_3_5_comprenhesion(list_num):\n",
" return sum(num for num in list_num if num>0 and (num%3==0 or num%5==0))\n",
"\n",
"input=[3,5,6,10,7,8,3] \n",
"\n",
"print(sum_multiplos_3_5_comprenhesion(input))"
]
},
{
Expand All @@ -127,12 +191,30 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"id": "a8cb1579-7065-4fc0-bd53-f91c2ad1dad9",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3]\n",
"[1]\n",
"[8, 6, 7, 5, 3, 0, 9]\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"def digitize(n):\n",
" return [int(d) for d in str(n)]\n",
"\n",
"# Ejemplos:\n",
"print(digitize(123)) # [1, 2, 3]\n",
"print(digitize(1)) # [1]\n",
"print(digitize(8675309)) # [8, 6, 7, 5, 3, 0, 9]"
]
},
{
Expand All @@ -155,12 +237,30 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"id": "86ebe759-76d8-4012-8590-c48a473a6099",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[-1, -2, -3, -4, -5]\n",
"[-1, 2, -3, 4, -5]\n",
"[]\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"\n",
"def invert(lst):\n",
" return [-x for x in lst]\n",
"\n",
"# Ejemplos:\n",
"print(invert([1,2,3,4,5])) # [-1,-2,-3,-4,-5]\n",
"print(invert([1,-2,3,-4,5])) # [-1,2,-3,4,-5]\n",
"print(invert([])) # []\n"
]
},
{
Expand Down Expand Up @@ -193,12 +293,22 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 14,
"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",
"squares = {x: x**2 for x in range(1, 16)}\n",
"print(squares)\n"
]
},
{
Expand All @@ -220,12 +330,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"id": "a1d55cea-96c3-4853-8220-17c0904a8816",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(1, 3), (1, 4), (2, 3), (2, 4)]\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"def all_pairs(list1, list2):\n",
" return [(x, y) for x in list1 for y in list2]\n",
"\n",
"# Ejemplo:\n",
"print(all_pairs([1, 2], [3, 4]))\n",
"# [(1, 3), (1, 4), (2, 3), (2, 4)]\n"
]
},
{
Expand All @@ -246,12 +370,26 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"id": "0175239c-87fa-4ba0-8776-d62567256db7",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[('a', 1), ('a', 2), ('b', 3), ('b', 4)]\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"def dict_pairs(d):\n",
" return [(k, v) for k in d for v in d[k]]\n",
"\n",
"# Ejemplo:\n",
"print(dict_pairs({\"a\": [1, 2], \"b\": [3, 4]}))\n",
"# [('a', 1), ('a', 2), ('b', 3), ('b', 4)]\n"
]
},
{
Expand Down Expand Up @@ -281,12 +419,29 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"id": "248918d0-f082-40a8-9d5c-c7b4ffd2bfca",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(1, 3), (1, 4), (2, 3), (2, 4), (5, 7), (5, 8), (6, 7), (6, 8)]\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"def tuple_list_pairs(tuples_list):\n",
" result = []\n",
" for a, b in tuples_list:\n",
" result.extend((x, y) for x in a for y in b)\n",
" return result\n",
"\n",
"# Ejemplo:\n",
"print(tuple_list_pairs([([1, 2], [3, 4]), ([5, 6], [7, 8])]))\n",
"# [(1, 3), (1, 4), (2, 3), (2, 4), (5, 7), (5, 8), (6, 7), (6, 8)]\n"
]
},
{
Expand All @@ -308,18 +463,35 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 18,
"id": "c57d9e77-6859-45dd-a2e6-2c1898f1baa2",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{('a', 'd'), ('b', 'd'), ('a', 'c'), ('b', 'c')}\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"def char_pairs(strings):\n",
" if len(strings) < 2:\n",
" return set()\n",
" s1, s2 = strings[0], strings[1]\n",
" return {(x, y) for x in s1 for y in s2}\n",
"\n",
"# Ejemplo:\n",
"print(char_pairs([\"ab\", \"cd\"]))\n",
"# {('a', 'c'), ('a', 'd'), ('b', 'c'), ('b', 'd')}\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
Expand All @@ -333,7 +505,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.9"
}
},
"nbformat": 4,
Expand Down