From ced0800d1420a551f074c3cd9143fcbcec42f581 Mon Sep 17 00:00:00 2001 From: martacarballo Date: Sat, 11 Oct 2025 14:05:25 +0200 Subject: [PATCH] solvedextra --- lab-python-list-comprehension.ipynb | 172 +++++++++++++++++++++++----- 1 file changed, 143 insertions(+), 29 deletions(-) diff --git a/lab-python-list-comprehension.ipynb b/lab-python-list-comprehension.ipynb index 6bef7fd..153fdba 100644 --- a/lab-python-list-comprehension.ipynb +++ b/lab-python-list-comprehension.ipynb @@ -71,12 +71,29 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 241, "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", + "\n", + "input = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]\n", + "\n", + "def open_or_senior(input):\n", + " return [\"Senior\" if age >= 55 and handicap > 7 else \"Open\" for age, handicap in input]\n", + "\n", + "result = open_or_senior(input)\n", + "print (result)\n", + "\n" ] }, { @@ -95,12 +112,25 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 242, "id": "53061dce-7aa3-4476-8b56-71413c5e135c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23\n" + ] + } + ], "source": [ - "# your code goes here" + "def sum_of_multiples(n: int) -> int:\n", + " \"\"\"Sum multiples of 3 or 5 below n. If n is negative, return 0.\"\"\"\n", + " if n <= 0:\n", + " return 0\n", + " return sum(i for i in range(n) if i % 3 == 0 or i % 5 == 0)\n", + "print(sum_of_multiples(10)) # Output: 23" ] }, { @@ -127,12 +157,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 243, "id": "a8cb1579-7065-4fc0-bd53-f91c2ad1dad9", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3]\n" + ] + } + ], "source": [ - "# your code goes here" + "def digits(n: int):\n", + " return [int(ch) for ch in str(n)]\n", + "print(digits(123)) " ] }, { @@ -155,12 +195,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 244, "id": "86ebe759-76d8-4012-8590-c48a473a6099", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-1, -2, -3, -4, -5]\n" + ] + } + ], "source": [ - "# your code goes here" + "def invert(nums):\n", + " return [-n for n in nums]\n", + "print(invert([1,2,3,4,5]))\n", + "\n", + " \n" ] }, { @@ -193,12 +245,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 245, "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_dict = {x: x**2 for x in range(1, 16)}\n", + "print(squares_dict)" ] }, { @@ -220,12 +282,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 246, "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", + "lista1= [1, 2]\n", + "lista2= [3, 4]\n", + "pairs = [(a, b) for a in lista1 for b in lista2]\n", + "print(pairs) " ] }, { @@ -246,12 +320,22 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 247, "id": "0175239c-87fa-4ba0-8776-d62567256db7", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{('b', 3), ('b', 4), ('a', 1), ('a', 2)}\n" + ] + } + ], "source": [ - "# your code goes here" + "dict = {\"a\": [1, 2], \"b\": [3, 4]}\n", + "lista = {(k, v) for k, values in dict.items() for v in values}\n", + "print(lista)\n" ] }, { @@ -281,12 +365,24 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 248, "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", + "\n", + "lista = [([1, 2], [3, 4]), ([5, 6], [7, 8])]\n", + "tupla = [(a, b) for izquierda, derecha in lista for a in izquierda for b in derecha]\n", + "print(tupla)\n" ] }, { @@ -308,18 +404,36 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 249, "id": "c57d9e77-6859-45dd-a2e6-2c1898f1baa2", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{('a', 'd'), ('c', 'b'), ('c', 'd'), ('a', 'b')}\n" + ] + } + ], "source": [ - "# your code goes here" + "# your code goes here\n", + "\n", + "lista = [\"ab\", \"cd\"]\n", + "s1, s2 = lista\n", + "\n", + "# pares dentro de cada string + cruces concretos\n", + "tupla = [(s1[0], s1[1]), (s2[0], s2[1])]\n", + "tupla2 = [(s1[0], s2[1]), (s2[0], s1[1])]\n", + "\n", + "fin_tupla = set(tupla + tupla2)\n", + "print(fin_tupla)" ] } ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -333,7 +447,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.12.9" } }, "nbformat": 4,