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: 232 additions & 31 deletions lab-python-functions.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"id": "df908bed-acc6-4b67-b33a-f3b1c564a49f",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['cat', 'dog', 'bird']\n"
]
}
],
"source": [
"lst = [\"cat\", \"dog\", \"bird\", \"cat\", \"dog\", \"cat\"]\n",
"\n",
"def get_unique_list(lst):\n",
" \"\"\"\n",
" Takes a list as an argument and returns a new list with unique elements from the first list.\n",
Expand All @@ -43,7 +53,13 @@
" Returns:\n",
" list: A new list with unique elements from the input list.\n",
" \"\"\"\n",
" # your code goes here\n"
" # your code goes here\n",
" unicos = []\n",
" for i in lst:\n",
" if i not in unicos:\n",
" unicos.append(i)\n",
" return unicos \n",
"print(get_unique_list(lst))\n"
]
},
{
Expand All @@ -60,10 +76,18 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"id": "7d5c8e34-a116-4428-ab9d-e0e15e338fff",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(2, 8)\n"
]
}
],
"source": [
"def count_case(string):\n",
" \"\"\"\n",
Expand All @@ -75,7 +99,16 @@
" Returns:\n",
" A tuple containing the count of uppercase and lowercase letters in the string.\n",
" \"\"\"\n",
" # your code goes here"
" # your code goes here\n",
" upper_count = 0\n",
" lower_count = 0\n",
" for char in string:\n",
" if char.isupper():\n",
" upper_count += 1\n",
" elif char.islower():\n",
" lower_count += 1\n",
" return (upper_count, lower_count)\n",
"print (count_case(\"Hello World!\"))"
]
},
{
Expand All @@ -92,10 +125,19 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 12,
"id": "c15b91d4-cfd6-423b-9f36-76012b8792b8",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello world\n",
"2\n"
]
}
],
"source": [
"import string\n",
"\n",
Expand All @@ -110,6 +152,11 @@
" str: The sentence without any punctuation marks.\n",
" \"\"\"\n",
" # your code goes here\n",
" for ch in string.punctuation:\n",
" sentence = sentence.replace(ch, \"\")\n",
" return sentence\n",
"\n",
"print(remove_punctuation(\"Hello, world!\")) \n",
"\n",
"def word_count(sentence):\n",
" \"\"\"\n",
Expand All @@ -122,7 +169,14 @@
" Returns:\n",
" int: The number of words in the sentence.\n",
" \"\"\"\n",
" # your code goes here"
" # your code goes here\n",
" for ch in string.punctuation:\n",
" sentence = sentence.replace(ch, \"\")\n",
" # 2) Separar por espacios y contar\n",
" words = sentence.split()\n",
" return len(words)\n",
"\n",
"print(word_count(\"Hello, world!\"))"
]
},
{
Expand All @@ -138,16 +192,6 @@
"*would give you as expected output: 7*"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "f7cfd32a-f559-47ff-81c1-2576bd4fe3bf",
"metadata": {},
"outputs": [],
"source": [
"# your code goes here"
]
},
{
"cell_type": "markdown",
"id": "fb01ccba-2ecc-4eea-b9a9-2bf9d29e8804",
Expand All @@ -168,12 +212,61 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 13,
"id": "57f9afc7-8626-443c-9c3e-eb78ef503193",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3\n",
"2\n",
"-1\n",
"0.5\n",
"3\n",
"2\n",
"24\n",
"5.0\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"def calculator1 (x,y):\n",
" return x + y\n",
"print(calculator1(1,2))\n",
"\n",
"def calculator2 (x,y):\n",
" return x * y\n",
"print(calculator2(1,2))\n",
"\n",
"def calculator3 (x,y):\n",
" return x - y\n",
"print(calculator3(1,2))\n",
"\n",
"def calculator4 (x,y):\n",
" return x / y\n",
"print(calculator4(1,2))\n",
"\n",
"def calculate (x,y, operator):\n",
" if operator == \"+\":\n",
" return calculator1(x,y)\n",
" elif operator == \"*\":\n",
" return calculator2(x,y)\n",
" elif operator == \"-\":\n",
" return calculator3(x,y)\n",
" elif operator == \"/\":\n",
" return calculator4(x,y)\n",
" else:\n",
" return \"Invalid operator\"\n",
" \n",
"# pruebas\n",
"print(calculate(1, 2, '+')) # 3\n",
"print(calculate(5, 3, '-')) # 2\n",
"print(calculate(4, 6, '*')) # 24\n",
"print(calculate(10, 2, '/')) # 5.0\n",
" "
]
},
{
Expand All @@ -192,12 +285,87 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"id": "ff3e816c-13ab-447d-a6f2-bb47a8fad2e2",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Test 1 (+): 3\n",
"Test 2 (+): 10\n",
"Test 3 (-): 5\n",
"Test 4 (*): 24\n",
"Test 5 (/): 10.0\n",
"Test 6 (0): Error: division by zero\n",
"Test 7 (^): Error: operador no válido. Usa +, -, * o /.\n"
]
}
],
"source": [
"# your code goes here"
"# Basic operation functions\n",
"def calculator1(x, y): # Addition\n",
" return x + y\n",
"\n",
"def calculator2(x, y): # Multiplication\n",
" return x * y\n",
"\n",
"def calculator3(x, y): # Subtraction\n",
" return x - y\n",
"\n",
"def calculator4(x, y): # Division\n",
" if y == 0:\n",
" return \"Error: division by zero\"\n",
" return x / y\n",
"\n",
"def calculate(*args):\n",
" if len(args) < 2:\n",
" return \"Error: proporciona al menos un número y un operador\"\n",
" \n",
" operator = args[-1]\n",
" nums = args[:-1]\n",
" \n",
" if not nums:\n",
" return \"Error: faltan números\"\n",
"\n",
" result = nums[0]\n",
"\n",
" # Fixed: Changed 'calculator' to 'calculator1' to match the function definition\n",
" if operator == '+':\n",
" for n in nums[1:]:\n",
" result = calculator1(result, n)\n",
" return result\n",
"\n",
" elif operator == '*':\n",
" for n in nums[1:]:\n",
" result = calculator2(result, n)\n",
" return result\n",
"\n",
" elif operator == '-':\n",
" for n in nums[1:]:\n",
" result = calculator3(result, n)\n",
" return result\n",
"\n",
" elif operator == '/':\n",
" for n in nums[1:]:\n",
" # Check if current result is already an error string\n",
" if isinstance(result, str):\n",
" return result\n",
" result = calculator4(result, n)\n",
" return result\n",
"\n",
" else:\n",
" return \"Error: operador no válido. Usa +, -, * o /.\"\n",
"\n",
"# Tests\n",
"print(f\"Test 1 (+): {calculate(1, 2, '+')}\") # Expected: 3\n",
"print(f\"Test 2 (+): {calculate(1, 2, 3, 4, '+')}\") # Expected: 10\n",
"print(f\"Test 3 (-): {calculate(10, 2, 3, '-')}\") # Expected: 5\n",
"print(f\"Test 4 (*): {calculate(2, 3, 4, '*')}\") # Expected: 24\n",
"print(f\"Test 5 (/): {calculate(100, 2, 5, '/')}\") # Expected: 10.0\n",
"print(f\"Test 6 (0): {calculate(10, 0, '/')}\") # Expected: Error: division by zero\n",
"print(f\"Test 7 (^): {calculate(7, 3, '^')}\") # Expected: Error operador"
]
},
{
Expand Down Expand Up @@ -273,7 +441,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 16,
"id": "5832ecfe-c652-418d-8fbc-bac4b1166b40",
"metadata": {},
"outputs": [],
Expand Down Expand Up @@ -315,18 +483,51 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 17,
"id": "a1d55cea-96c3-4853-8220-17c0904a8816",
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"55\n",
"[0]\n",
"[0, 1, 1, 2, 3, 5]\n"
]
}
],
"source": [
"# your code goes here"
"# your code goes here\n",
"def fib(n):\n",
" # Fibonacci recursivo básico\n",
" if n < 0:\n",
" raise ValueError(\"n debe ser >= 0\")\n",
" if n == 0:\n",
" return 0\n",
" if n == 1:\n",
" return 1\n",
" return fib(n - 1) + fib(n - 2)\n",
"\n",
"def fib_sequence(n):\n",
" # Lista desde fib(0) hasta fib(n)\n",
" if n < 0:\n",
" raise ValueError(\"n debe ser >= 0\")\n",
" seq = []\n",
" for i in range(n + 1):\n",
" seq.append(fib(i))\n",
" return seq\n",
"\n",
"# Pruebas\n",
"print(fib(10)) # 55\n",
"print(fib_sequence(0)) # [0]\n",
"print(fib_sequence(5)) # [0, 1, 1, 2, 3, 5]"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"display_name": "base",
"language": "python",
"name": "python3"
},
Expand All @@ -340,7 +541,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.13"
"version": "3.12.10"
}
},
"nbformat": 4,
Expand Down