diff --git a/.ipynb_checkpoints/lab-python-list-comprehension-checkpoint.ipynb b/.ipynb_checkpoints/lab-python-list-comprehension-checkpoint.ipynb new file mode 100644 index 0000000..b740581 --- /dev/null +++ b/.ipynb_checkpoints/lab-python-list-comprehension-checkpoint.ipynb @@ -0,0 +1,668 @@ +{ + "cells": [ + { + "cell_type": "markdown", + "id": "25d7736c-ba17-4aff-b6bb-66eba20fbf4e", + "metadata": {}, + "source": [ + "# Lab | List, Dict and Set Comprehension" + ] + }, + { + "cell_type": "markdown", + "id": "6f8e446f-16b4-4e21-92e7-9d3d1eb551b6", + "metadata": {}, + "source": [ + "Objective: Practice how to work with list, dict and set comprehensions to improve the efficiency and clarity of your Python code." + ] + }, + { + "cell_type": "markdown", + "id": "7b3b329e-c56a-4009-b6f2-8849c02c0cb8", + "metadata": {}, + "source": [ + "Hint: If you're having trouble writing a solution using a comprehension, try writing it out in a more traditional way first. This can help you break the problem down into smaller steps and better understand what you need to do. Once you have a working solution, you can then try to transform it into a comprehension if desired. Comprehensions can often be more concise and readable, but it's important to prioritize clarity and correctness over brevity." + ] + }, + { + "cell_type": "markdown", + "id": "20c3e882-9625-471e-afb4-48a4f40c5d1b", + "metadata": { + "tags": [] + }, + "source": [ + "## Challenge 1 - Katas" + ] + }, + { + "cell_type": "markdown", + "id": "75f816bf-f5a2-49a4-89de-89d8a533155f", + "metadata": {}, + "source": [ + "Do the following katas using list, dict or set comprehension." + ] + }, + { + "cell_type": "markdown", + "id": "0cdcc026-2830-4db5-9ec3-76d71833df93", + "metadata": {}, + "source": [ + "### Katas - 1\n", + "\n", + "The Western Suburbs Croquet Club has two categories of membership, Senior and Open. They would like your help with an application form that will tell prospective members which category they will be placed.\n", + "\n", + "To be a senior, a member must be at least 55 years old and have a handicap greater than 7. In this croquet club, handicaps range from -2 to +26; the better the player the lower the handicap.\n", + "\n", + "**Input**\n", + "\n", + "Input will consist of a list of pairs. Each pair contains information for a single potential member. Information consists of an integer for the person's age and an integer for the person's handicap.\n", + "\n", + "**Output**\n", + "\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", + "**Example**\n", + "\n", + "```python\n", + "input = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]\n", + "output = [\"Open\", \"Open\", \"Senior\", \"Open\", \"Open\", \"Senior\"]\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "id": "21625526-3fae-4c55-bab5-f91940070681", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Open', 'Open', 'Senior', 'Open', 'Open', 'Senior']\n" + ] + } + ], + "source": [ + "def open_or_senior(pares):\n", + " resultado = []\n", + " for age, handicap in pares:\n", + " if age >= 55 and handicap > 7:\n", + " resultado.append(\"Senior\")\n", + " else:\n", + " resultado.append(\"Open\")\n", + " return resultado \n", + "\n", + "input = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]\n", + "\n", + "print(open_or_senior(input))" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "0288dca6-4031-4bdd-a73f-2d19d456d690", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Open', 'Open', 'Senior', 'Open', 'Open', 'Senior']\n" + ] + } + ], + "source": [ + "def open_or_senior_comp(pares):\n", + " return [\n", + " \"Senior\" if (edad >= 55 and handicap > 7) else \"Open\"\n", + " for edad, handicap in pares\n", + " ]\n", + "input = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]\n", + "\n", + "print(open_or_senior(input))" + ] + }, + { + "cell_type": "markdown", + "id": "ad8758b7-bc71-4af8-999c-5e4def116906", + "metadata": {}, + "source": [ + "### Katas - 2\n", + "\n", + "If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.\n", + "\n", + "Write a solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in. Additionally, if the number is negative, return 0.\n", + "\n", + "Note: If the number is a multiple of both 3 and 5, only count it once." + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "id": "53061dce-7aa3-4476-8b56-71413c5e135c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23\n" + ] + } + ], + "source": [ + "def sum_multiples_3_5(n):\n", + " if n <= 0:\n", + " return 0 # caso especial: números negativos\n", + " total = 0\n", + " for k in range(n): # recorre 0,1,2,...,n-1\n", + " if (k % 3 == 0) or (k % 5 == 0):\n", + " total += k\n", + " return total\n", + "# n=10\n", + "\n", + "print(sum_multiples_3_5(10))\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "aecf8193-8f4c-4b87-b5bd-52b746708823", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23\n" + ] + } + ], + "source": [ + "def sum_multiples_3_5_comp(n):\n", + " if n <= 0:\n", + " return 0\n", + " return sum(k for k in range(n) if (k % 3 == 0) or (k % 5 == 0))\n", + "print(sum_multiples_3_5(10))" + ] + }, + { + "cell_type": "markdown", + "id": "bed9c59d-159a-41c4-86c0-d02b131d7070", + "metadata": {}, + "source": [ + "### Katas - 3\n", + "\n", + "Given a non-negative integer, return an array / a list of the individual digits in order.\n", + "\n", + "Examples:\n", + "\n", + "```python\n", + "\n", + "123 => [1,2,3]\n", + "\n", + "1 => [1]\n", + "\n", + "8675309 => [8,6,7,5,3,0,9]\n", + "\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 14, + "id": "a8cb1579-7065-4fc0-bd53-f91c2ad1dad9", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3]\n", + "[1]\n", + "[8, 6, 7, 5, 3, 0, 9]\n", + "[0]\n" + ] + } + ], + "source": [ + "def digits(n):\n", + " resultado = []\n", + " for digito in str(n): # recorre cada carácter en la cadena\n", + " resultado.append(int(digito)) # lo convierte a número\n", + " return resultado\n", + "\n", + "print(digits(123)) # [1, 2, 3]\n", + "print(digits(1)) # [1]\n", + "print(digits(8675309)) # [8, 6, 7, 5, 3, 0, 9]\n", + "print(digits(0)) # [0]\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f4bf79cd-41cc-41b0-8d57-b3a85a5fcdc0", + "metadata": {}, + "outputs": [], + "source": [ + "def digits_comp(n):\n", + " return [int(digito) for digito in str(n)]\n" + ] + }, + { + "cell_type": "markdown", + "id": "ab3f6eaa-c74d-45fa-a84a-516ae6ff6e0f", + "metadata": {}, + "source": [ + "### Katas - 4\n", + "\n", + "Given a set of numbers, create a function that returns the additive inverse of each. Each positive becomes negatives, and the negatives become positives.\n", + "\n", + "```python\n", + "invert([1,2,3,4,5]) == [-1,-2,-3,-4,-5]\n", + "invert([1,-2,3,-4,5]) == [-1,2,-3,4,-5]\n", + "invert([]) == []\n", + "```\n", + "\n", + "You can assume that all values are integers. Do not mutate the input array/list.\n" + ] + }, + { + "cell_type": "code", + "execution_count": 16, + "id": "86ebe759-76d8-4012-8590-c48a473a6099", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-1, -2, -3, -4, -5]\n", + "[-1, 2, -3, 4, -5]\n", + "[]\n", + "[0, -10, 7]\n" + ] + } + ], + "source": [ + "def invert(lst):\n", + " resultado = []\n", + " for x in lst:\n", + " resultado.append(-x)\n", + " return resultado\n", + "\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", + "print(invert([0, 10, -7])) # [0, -10, 7]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "b27a1a3b-6d1d-42a1-9043-844772f70bd3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-1, -2, -3, -4, -5]\n", + "[-1, 2, -3, 4, -5]\n", + "[]\n", + "[0, -10, 7]\n" + ] + } + ], + "source": [ + "def invert_comp(lst):\n", + " return [-x for x in lst]\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", + "print(invert([0, 10, -7])) # [0, -10, 7]" + ] + }, + { + "cell_type": "markdown", + "id": "3a9f2c20-48d2-4d08-a8c5-76bb53caaafb", + "metadata": { + "tags": [] + }, + "source": [ + "## Challenge 2 - exercises" + ] + }, + { + "cell_type": "markdown", + "id": "1c3bfaae-d514-4d9d-adab-59b66af621c9", + "metadata": {}, + "source": [ + "Do the following exercises using list, dict or set comprehension." + ] + }, + { + "cell_type": "markdown", + "id": "f5471e4a-a939-4626-9715-ad45f32c2487", + "metadata": {}, + "source": [ + "### Exercise 1\n", + "\n", + "Create a dictionary whose keys (`key`) are integers between 1 and 15 (both inclusive) and the values (`value`) are the square of the key (`key`)." + ] + }, + { + "cell_type": "code", + "execution_count": 19, + "id": "6711881b-450a-44cb-a3d0-367b2c6a4464", + "metadata": {}, + "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": [ + "def cuadrados_dict():\n", + " resultado = {}\n", + " for k in range(1,16): # del 1 al 15\n", + " resultado[k] = k**2\n", + " return resultado\n", + "print(cuadrados_dict())" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "e596aa10-0f68-4e47-a0da-b436e186fb60", + "metadata": {}, + "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": [ + "def cuadrados_dict_comp():\n", + " \n", + " return{k: k**2 for k in range(1,16)}\n", + "print(cuadrados_dict_comp())" + ] + }, + { + "cell_type": "markdown", + "id": "47dafbe6-5f2c-4f51-94d9-ca52f6d11492", + "metadata": {}, + "source": [ + "### Exercise 2\n", + "\n", + "Write a program that takes two lists of integers, and returns a list of all possible pairs of integers where the first integer is from the first list, and the second integer is from the second list.\n", + "\n", + "Example:\n", + "\n", + "```python\n", + "Input: [1, 2], [3, 4]\n", + "Output: [(1, 3), (1, 4), (2, 3), (2, 4)]\n", + "```\n" + ] + }, + { + "cell_type": "code", + "execution_count": 25, + "id": "a1d55cea-96c3-4853-8220-17c0904a8816", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(1, 3), (1, 4), (2, 3), (2, 4)]\n" + ] + } + ], + "source": [ + "def all_pairs(lista1, lista2):\n", + " resultado = []\n", + " for x in lista1:\n", + " for y in lista2:\n", + " resultado.append((x, y))\n", + " return resultado\n", + "print(all_pairs([1, 2], [3, 4]))" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "a19c08f4-fbe9-4385-8c85-1d58a8e62c7c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(1, 3), (1, 4), (2, 3), (2, 4)]\n" + ] + } + ], + "source": [ + "def all_pairs_comp(lista1, lista2):\n", + " return [(x, y) for x in lista1 for y in lista2]\n", + "print(all_pairs([1, 2], [3, 4]))" + ] + }, + { + "cell_type": "markdown", + "id": "03110a8a-0dd9-4eb4-b6ce-116233b4d6cf", + "metadata": {}, + "source": [ + "### Exercise 3\n", + "\n", + "Write a program that takes a dictionary of lists as input, and returns a list of all possible key-value pairs, where the key is from the dictionary, and the value is from the corresponding list.\n", + "Example:\n", + " \n", + "```python\n", + "Input: {\"a\": [1, 2], \"b\": [3, 4]}\n", + "Output: [(\"a\", 1), (\"a\", 2), (\"b\", 3), (\"b\", 4)]\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 27, + "id": "0175239c-87fa-4ba0-8776-d62567256db7", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('a', 1), ('a', 2), ('b', 3), ('b', 4)]\n" + ] + } + ], + "source": [ + "def dict_pairs(dic):\n", + " resultado = []\n", + " for clave, lista_valores in dic.items():\n", + " for valor in lista_valores:\n", + " resultado.append((clave, valor))\n", + " return resultado\n", + "print(dict_pairs({\"a\": [1, 2], \"b\": [3, 4]}))" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "2faf76cd-1319-4d54-b750-b5e074b6cc0b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('a', 1), ('a', 2), ('b', 3), ('b', 4)]\n" + ] + } + ], + "source": [ + "def dict_pairs_comp(dic):\n", + " return [(clave, valor) for clave, lista_valores in dic.items() for valor in lista_valores]\n", + "print(dict_pairs({\"a\": [1, 2], \"b\": [3, 4]}))" + ] + }, + { + "cell_type": "markdown", + "id": "0cd7ff45-52eb-409c-9805-f838cdd95cc1", + "metadata": {}, + "source": [ + "# Bonus exercises" + ] + }, + { + "cell_type": "markdown", + "id": "16d1e67a-6bd7-4932-a998-4542f06f029a", + "metadata": {}, + "source": [ + "### Exercise 1\n", + "\n", + "Write a program that takes a list of tuples, where each tuple contains two lists of integers, and returns a list of all possible pairs of integers where the first integer is from the first list in a tuple, and the second integer is from the second list in the same tuple.\n", + "\n", + "Example:\n", + "```python\n", + "Input: [([1, 2], [3, 4]), ([5, 6], [7, 8])]\n", + "Output: [(1, 3), (1, 4), (2, 3), (2, 4), (5, 7), (5, 8), (6, 7), (6, 8)]\n", + "\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 29, + "id": "248918d0-f082-40a8-9d5c-c7b4ffd2bfca", + "metadata": {}, + "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": [ + "def pairs_from_tuples(lista_de_tuplas):\n", + " resultado = []\n", + " for lista1, lista2 in lista_de_tuplas:\n", + " for x in lista1:\n", + " for y in lista2:\n", + " resultado.append((x, y))\n", + " return resultado\n", + "print(pairs_from_tuples([([1, 2], [3, 4]), ([5, 6], [7, 8])]))" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "ff3fa3a5-92a3-4071-b046-81ad1eb502fe", + "metadata": {}, + "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": [ + "def pairs_from_tuples(lista_de_tuplas):\n", + " return[(x, y) for lista1, lista2 in lista_de_tuplas for x in lista1 for y in lista2]\n", + "print(pairs_from_tuples([([1, 2], [3, 4]), ([5, 6], [7, 8])]))" + ] + }, + { + "cell_type": "markdown", + "id": "737478d3-2e93-445f-b732-00043f9e0541", + "metadata": {}, + "source": [ + "### Exercise 2\n", + "\n", + "Write a program that takes a list of strings, and returns a set of all possible pairs of characters where the first character is from the first string, and the second character is from the second string.\n", + "\n", + "Example:\n", + " \n", + "```python\n", + "Input: [\"ab\", \"cd\"]\n", + "Output: {('a', 'b'), ('c', 'd'), ('a', 'd'), ('c', 'b')}\n", + "```" + ] + }, + { + "cell_type": "code", + "execution_count": 32, + "id": "c57d9e77-6859-45dd-a2e6-2c1898f1baa2", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{('a', 'd'), ('b', 'd'), ('b', 'c'), ('a', 'c')}\n" + ] + } + ], + "source": [ + "def char_pairs(lista_de_strings):\n", + " if len(lista_de_strings) < 2:\n", + " return set()\n", + " s1, s2 = lista_de_strings[0], lista_de_strings[1]\n", + " resultado = set()\n", + " for a in s1: # cada caracter del primer string\n", + " for b in s2: # cada caracter del segundo string\n", + " resultado.add((a, b))\n", + " return resultado\n", + "\n", + "print(char_pairs([\"ab\", \"cd\"]))" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d4fa9fd8-42e7-49a4-8623-e03e6e7e6a3a", + "metadata": {}, + "outputs": [], + "source": [] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.5" + } + }, + "nbformat": 4, + "nbformat_minor": 5 +} diff --git a/lab-python-list-comprehension.ipynb b/lab-python-list-comprehension.ipynb index 6bef7fd..b740581 100644 --- a/lab-python-list-comprehension.ipynb +++ b/lab-python-list-comprehension.ipynb @@ -71,12 +71,56 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 6, "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" + "def open_or_senior(pares):\n", + " resultado = []\n", + " for age, handicap in pares:\n", + " if age >= 55 and handicap > 7:\n", + " resultado.append(\"Senior\")\n", + " else:\n", + " resultado.append(\"Open\")\n", + " return resultado \n", + "\n", + "input = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]\n", + "\n", + "print(open_or_senior(input))" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "id": "0288dca6-4031-4bdd-a73f-2d19d456d690", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "['Open', 'Open', 'Senior', 'Open', 'Open', 'Senior']\n" + ] + } + ], + "source": [ + "def open_or_senior_comp(pares):\n", + " return [\n", + " \"Senior\" if (edad >= 55 and handicap > 7) else \"Open\"\n", + " for edad, handicap in pares\n", + " ]\n", + "input = [[18, 20], [45, 2], [61, 12], [37, 6], [21, 21], [78, 9]]\n", + "\n", + "print(open_or_senior(input))" ] }, { @@ -95,12 +139,54 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 12, "id": "53061dce-7aa3-4476-8b56-71413c5e135c", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23\n" + ] + } + ], + "source": [ + "def sum_multiples_3_5(n):\n", + " if n <= 0:\n", + " return 0 # caso especial: números negativos\n", + " total = 0\n", + " for k in range(n): # recorre 0,1,2,...,n-1\n", + " if (k % 3 == 0) or (k % 5 == 0):\n", + " total += k\n", + " return total\n", + "# n=10\n", + "\n", + "print(sum_multiples_3_5(10))\n", + "\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "id": "aecf8193-8f4c-4b87-b5bd-52b746708823", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "23\n" + ] + } + ], "source": [ - "# your code goes here" + "def sum_multiples_3_5_comp(n):\n", + " if n <= 0:\n", + " return 0\n", + " return sum(k for k in range(n) if (k % 3 == 0) or (k % 5 == 0))\n", + "print(sum_multiples_3_5(10))" ] }, { @@ -127,12 +213,44 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 14, "id": "a8cb1579-7065-4fc0-bd53-f91c2ad1dad9", "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[1, 2, 3]\n", + "[1]\n", + "[8, 6, 7, 5, 3, 0, 9]\n", + "[0]\n" + ] + } + ], + "source": [ + "def digits(n):\n", + " resultado = []\n", + " for digito in str(n): # recorre cada carácter en la cadena\n", + " resultado.append(int(digito)) # lo convierte a número\n", + " return resultado\n", + "\n", + "print(digits(123)) # [1, 2, 3]\n", + "print(digits(1)) # [1]\n", + "print(digits(8675309)) # [8, 6, 7, 5, 3, 0, 9]\n", + "print(digits(0)) # [0]\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "f4bf79cd-41cc-41b0-8d57-b3a85a5fcdc0", + "metadata": {}, "outputs": [], "source": [ - "# your code goes here" + "def digits_comp(n):\n", + " return [int(digito) for digito in str(n)]\n" ] }, { @@ -155,12 +273,58 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 16, "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", + "[0, -10, 7]\n" + ] + } + ], + "source": [ + "def invert(lst):\n", + " resultado = []\n", + " for x in lst:\n", + " resultado.append(-x)\n", + " return resultado\n", + "\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", + "print(invert([0, 10, -7])) # [0, -10, 7]\n" + ] + }, + { + "cell_type": "code", + "execution_count": 17, + "id": "b27a1a3b-6d1d-42a1-9043-844772f70bd3", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[-1, -2, -3, -4, -5]\n", + "[-1, 2, -3, 4, -5]\n", + "[]\n", + "[0, -10, 7]\n" + ] + } + ], "source": [ - "# your code goes here" + "def invert_comp(lst):\n", + " return [-x for x in lst]\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", + "print(invert([0, 10, -7])) # [0, -10, 7]" ] }, { @@ -193,12 +357,46 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 19, "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" + "def cuadrados_dict():\n", + " resultado = {}\n", + " for k in range(1,16): # del 1 al 15\n", + " resultado[k] = k**2\n", + " return resultado\n", + "print(cuadrados_dict())" + ] + }, + { + "cell_type": "code", + "execution_count": 24, + "id": "e596aa10-0f68-4e47-a0da-b436e186fb60", + "metadata": {}, + "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": [ + "def cuadrados_dict_comp():\n", + " \n", + " return{k: k**2 for k in range(1,16)}\n", + "print(cuadrados_dict_comp())" ] }, { @@ -220,12 +418,46 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 25, "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" + "def all_pairs(lista1, lista2):\n", + " resultado = []\n", + " for x in lista1:\n", + " for y in lista2:\n", + " resultado.append((x, y))\n", + " return resultado\n", + "print(all_pairs([1, 2], [3, 4]))" + ] + }, + { + "cell_type": "code", + "execution_count": 26, + "id": "a19c08f4-fbe9-4385-8c85-1d58a8e62c7c", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[(1, 3), (1, 4), (2, 3), (2, 4)]\n" + ] + } + ], + "source": [ + "def all_pairs_comp(lista1, lista2):\n", + " return [(x, y) for x in lista1 for y in lista2]\n", + "print(all_pairs([1, 2], [3, 4]))" ] }, { @@ -246,12 +478,46 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 27, "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" + "def dict_pairs(dic):\n", + " resultado = []\n", + " for clave, lista_valores in dic.items():\n", + " for valor in lista_valores:\n", + " resultado.append((clave, valor))\n", + " return resultado\n", + "print(dict_pairs({\"a\": [1, 2], \"b\": [3, 4]}))" + ] + }, + { + "cell_type": "code", + "execution_count": 28, + "id": "2faf76cd-1319-4d54-b750-b5e074b6cc0b", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[('a', 1), ('a', 2), ('b', 3), ('b', 4)]\n" + ] + } + ], + "source": [ + "def dict_pairs_comp(dic):\n", + " return [(clave, valor) for clave, lista_valores in dic.items() for valor in lista_valores]\n", + "print(dict_pairs({\"a\": [1, 2], \"b\": [3, 4]}))" ] }, { @@ -281,12 +547,47 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 29, "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": [ + "def pairs_from_tuples(lista_de_tuplas):\n", + " resultado = []\n", + " for lista1, lista2 in lista_de_tuplas:\n", + " for x in lista1:\n", + " for y in lista2:\n", + " resultado.append((x, y))\n", + " return resultado\n", + "print(pairs_from_tuples([([1, 2], [3, 4]), ([5, 6], [7, 8])]))" + ] + }, + { + "cell_type": "code", + "execution_count": 30, + "id": "ff3fa3a5-92a3-4071-b046-81ad1eb502fe", + "metadata": {}, + "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" + "def pairs_from_tuples(lista_de_tuplas):\n", + " return[(x, y) for lista1, lista2 in lista_de_tuplas for x in lista1 for y in lista2]\n", + "print(pairs_from_tuples([([1, 2], [3, 4]), ([5, 6], [7, 8])]))" ] }, { @@ -308,13 +609,39 @@ }, { "cell_type": "code", - "execution_count": null, + "execution_count": 32, "id": "c57d9e77-6859-45dd-a2e6-2c1898f1baa2", "metadata": {}, - "outputs": [], + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{('a', 'd'), ('b', 'd'), ('b', 'c'), ('a', 'c')}\n" + ] + } + ], "source": [ - "# your code goes here" + "def char_pairs(lista_de_strings):\n", + " if len(lista_de_strings) < 2:\n", + " return set()\n", + " s1, s2 = lista_de_strings[0], lista_de_strings[1]\n", + " resultado = set()\n", + " for a in s1: # cada caracter del primer string\n", + " for b in s2: # cada caracter del segundo string\n", + " resultado.add((a, b))\n", + " return resultado\n", + "\n", + "print(char_pairs([\"ab\", \"cd\"]))" ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "d4fa9fd8-42e7-49a4-8623-e03e6e7e6a3a", + "metadata": {}, + "outputs": [], + "source": [] } ], "metadata": { @@ -333,7 +660,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.13" + "version": "3.13.5" } }, "nbformat": 4,