"### 1.5.4 Pregunta al usario cuantos numeros de la secuancia Fibonacci quiere calcular y escribe una funcion que calcule la secuencia e imprima el resultado."
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"cuantos numeros de la secuancia Fibonacci quiere calcular:5\n",
"[0, 1, 1, 2, 3, 5]\n",
"fibonacci funcion para calcular la secuencia de fibonacci dato un numer n\n",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" int: el numero de fibonacci\n",
" Examples:\n",
" >>> print([i for i in fibonacci(6)])\n",
" [0, 1, 1, 2, 3, 5]\n",
"\n",
" \n",
"Trying:\n",
" print([i for i in example_generator(4)])\n",
"Expecting:\n",
" [0, 1, 2, 3]\n",
"ok\n",
"Trying:\n",
" print([i for i in fibonacci(6)])\n",
"Expecting:\n",
" [0, 1, 1, 2, 3, 5]\n",
"ok\n",
"Trying:\n",
" intersectab()\n",
"Expecting:\n",
" [1, 2, 3, 5, 8, 13]\n",
"ok\n",
"Trying:\n",
" printmayotto10()\n",
"Expecting:\n",
" [13, 21, 34, 55, 89]\n",
"ok\n",
"Trying:\n",
" rang1to20()\n",
"Expecting:\n",
" [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]\n",
"ok\n",
"2 items had no tests:\n",
" __main__\n",
" __main__.fib\n",
"5 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.fibonacci\n",
" 1 tests in __main__.intersectab\n",
" 1 tests in __main__.printmayotto10\n",
" 1 tests in __main__.rang1to20\n",
"5 tests in 7 items.\n",
"5 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=5)"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def fibonacci(n):\n",
" \"\"\"fibonacci funcion para calcular la secuencia de fibonacci dato un numer n\n",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" int: el numero de fibonacci\n",
" Examples:\n",
" >>> print([i for i in fibonacci(6)])\n",
" [0, 1, 1, 2, 3, 5]\n",
"\n",
" \"\"\"\n",
" \n",
" a, b = 0, 1\n",
" for _ in range(n):\n",
" yield a\n",
" a, b = b, a + b\n",
"\n",
"numero = int(input(\"cuantos numeros de la secuancia Fibonacci quiere calcular:\"))\n",
"\n",
"print([i for i in fibonacci(numero + 1)])\n",
"print(fibonacci.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.5.5 Escribe una funcion que sume todos los numeros en una lista usando for."
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"20\n",
"sumList funcion sumer los numeros de una lista\n",
"\n",
" Args:\n",
" lista (list): lista con los datos a sumar\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> sumList([8, 2, 3, 0, 7])\n",
" 20\n",
"\n",
" \n",
"Trying:\n",
" print([i for i in example_generator(4)])\n",
"Expecting:\n",
" [0, 1, 2, 3]\n",
"ok\n",
"Trying:\n",
" print([i for i in fibonacci(6)])\n",
"Expecting:\n",
" [0, 1, 1, 2, 3, 5]\n",
"ok\n",
"Trying:\n",
" intersectab()\n",
"Expecting:\n",
" [1, 2, 3, 5, 8, 13]\n",
"ok\n",
"Trying:\n",
" printmayotto10()\n",
"Expecting:\n",
" [13, 21, 34, 55, 89]\n",
"ok\n",
"Trying:\n",
" rang1to20()\n",
"Expecting:\n",
" [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]\n",
"ok\n",
"Trying:\n",
" sumList([8, 2, 3, 0, 7])\n",
"Expecting:\n",
" 20\n",
"ok\n",
"2 items had no tests:\n",
" __main__\n",
" __main__.fib\n",
"6 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.fibonacci\n",
" 1 tests in __main__.intersectab\n",
" 1 tests in __main__.printmayotto10\n",
" 1 tests in __main__.rang1to20\n",
" 1 tests in __main__.sumList\n",
"6 tests in 8 items.\n",
"6 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=6)"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = [8, 2, 3, 0, 7]\n",
"\n",
"def sumList(lista):\n",
" \"\"\"sumList funcion sumer los numeros de una lista\n",
"\n",
" Args:\n",
" lista (list): lista con los datos a sumar\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> sumList([8, 2, 3, 0, 7])\n",
" 20\n",
"\n",
" \"\"\"\n",
" print(sum(lista))\n",
" \n",
"sumList(a)\n",
"print(sumList.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.5.7 Escribe una funcion que indique si un numero es o no perfecto.\n",
"[Wikipedia:](https://es.wikipedia.org/wiki/N%C3%BAmero_perfecto) *Un número perfecto es un número natural que es igual a la suma de sus divisores propios positivos. Dicho de otra forma, un número perfecto es aquel que es amigo de sí mismo.\n",
"Así, 6 es un número perfecto porque sus divisores propios son 1, 2 y 3; y 6 = 1 + 2 + 3. Los siguientes números perfectos son 28, 496 y 8128.*\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Escribe un nuemero: 28\n",
"28 es un numero perfecto\n",
"perfect funcion para determinar si un numero es perfecto\n",
"\n",
" Args:\n",
" x (int): numero a verificar si es perfecto o no\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> perfect(28)\n",
" True\n",
" >>> perfect(288)\n",
" False\n",
"\n",
" \n",
"Trying:\n",
" print([i for i in example_generator(4)])\n",
"Expecting:\n",
" [0, 1, 2, 3]\n",
"ok\n",
"Trying:\n",
" print([i for i in fibonacci(6)])\n",
"Expecting:\n",
" [0, 1, 1, 2, 3, 5]\n",
"ok\n",
"Trying:\n",
" intersectab()\n",
"Expecting:\n",
" [1, 2, 3, 5, 8, 13]\n",
"ok\n",
"Trying:\n",
" perfect(28)\n",
"Expecting:\n",
" True\n",
"ok\n",
"Trying:\n",
" perfect(288)\n",
"Expecting:\n",
" False\n",
"ok\n",
"Trying:\n",
" printmayotto10()\n",
"Expecting:\n",
" [13, 21, 34, 55, 89]\n",
"ok\n",
"Trying:\n",
" rang1to20()\n",
"Expecting:\n",
" [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]\n",
"ok\n",
"Trying:\n",
" sumList([8, 2, 3, 0, 7])\n",
"Expecting:\n",
" 20\n",
"ok\n",
"2 items had no tests:\n",
" __main__\n",
" __main__.fib\n",
"7 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.fibonacci\n",
" 1 tests in __main__.intersectab\n",
" 2 tests in __main__.perfect\n",
" 1 tests in __main__.printmayotto10\n",
" 1 tests in __main__.rang1to20\n",
" 1 tests in __main__.sumList\n",
"8 tests in 9 items.\n",
"8 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=8)"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from functools import reduce\n",
"def perfect(x):\n",
" \"\"\"perfect funcion para determinar si un numero es perfecto\n",
"\n",
" Args:\n",
" x (int): numero a verificar si es perfecto o no\n",
"numero = int(input(\"Escribe un nuemero: \"))\n",
"if perfect(numero):\n",
" print(\"%s es un numero perfecto\" % numero)\n",
"else:\n",
" print(\"%s no es un numero perfecto\" % numero)\n",
"print(perfect.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.5.9 Escribe una funcion que indique si una frase es un panagrama.\n",
"[Wikipedia](https://es.wikipedia.org/wiki/Pangrama):Un pangrama (del griego: παν γραμμα, «todas las letras») o frase holoalfabética es un texto que usa todas las letras posibles del alfabeto de un idioma. "
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n",
"panagrama funcion para determinar si una palabra es panagrama o no\n",
"\n",
" Args:\n",
" frase (string): palabra a verificar si es panagrama o no\n",
"### 1.5.10 Escribe un programa que imprima el siguiente un **for** anidado.\n",
"1\n",
"\n",
"22\n",
"\n",
"333\n",
"\n",
"4444\n",
"\n",
"55555\n",
"\n",
"666666\n",
"\n",
"7777777\n",
"\n",
"88888888\n",
"\n",
"999999999"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"panagrama funcion para determinar si una palabra es panagrama o no\n",
"\n",
" Args:\n",
" frase (string): palabra a verificar si es panagrama o no\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> multiplicaletra()\n",
" 1\n",
" 22\n",
" 333\n",
" 4444\n",
" 55555\n",
" 666666\n",
" 7777777\n",
" 88888888\n",
" 999999999\n",
"\n",
" \n",
"Trying:\n",
" print([i for i in example_generator(4)])\n",
"Expecting:\n",
" [0, 1, 2, 3]\n",
"ok\n",
"Trying:\n",
" print([i for i in fibonacci(6)])\n",
"Expecting:\n",
" [0, 1, 1, 2, 3, 5]\n",
"ok\n",
"Trying:\n",
" intersectab()\n",
"Expecting:\n",
" [1, 2, 3, 5, 8, 13]\n",
"ok\n",
"Trying:\n",
" multiplicaletra()\n",
"Expecting:\n",
" 1\n",
" 22\n",
" 333\n",
" 4444\n",
" 55555\n",
" 666666\n",
" 7777777\n",
" 88888888\n",
" 999999999\n",
"ok\n",
"Trying:\n",
" panagrama(\"zbcdefghijkl onpqrstuvwxya\")\n",
"Expecting:\n",
" False\n",
"ok\n",
"Trying:\n",
" panagrama(\"abcdefghijkl onpqrstuvwxyz\")\n",
"Expecting:\n",
" False\n",
"ok\n",
"Trying:\n",
" panagrama(\"abcdefghijklmnopqrstuvwxyz\")\n",
"Expecting:\n",
" True\n",
"ok\n",
"Trying:\n",
" perfect(28)\n",
"Expecting:\n",
" True\n",
"ok\n",
"Trying:\n",
" perfect(288)\n",
"Expecting:\n",
" False\n",
"ok\n",
"Trying:\n",
" printmayotto10()\n",
"Expecting:\n",
" [13, 21, 34, 55, 89]\n",
"ok\n",
"Trying:\n",
" rang1to20()\n",
"Expecting:\n",
" [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]\n",
"ok\n",
"Trying:\n",
" sumList([8, 2, 3, 0, 7])\n",
"Expecting:\n",
" 20\n",
"ok\n",
"2 items had no tests:\n",
" __main__\n",
" __main__.fib\n",
"9 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.fibonacci\n",
" 1 tests in __main__.intersectab\n",
" 1 tests in __main__.multiplicaletra\n",
" 3 tests in __main__.panagrama\n",
" 2 tests in __main__.perfect\n",
" 1 tests in __main__.printmayotto10\n",
" 1 tests in __main__.rang1to20\n",
" 1 tests in __main__.sumList\n",
"12 tests in 11 items.\n",
"12 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=12)"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def multiplicaletra():\n",
" \"\"\"panagrama funcion para determinar si una palabra es panagrama o no\n",
"\n",
" Args:\n",
" frase (string): palabra a verificar si es panagrama o no\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> multiplicaletra()\n",
" 1\n",
" 22\n",
" 333\n",
" 4444\n",
" 55555\n",
" 666666\n",
" 7777777\n",
" 88888888\n",
" 999999999\n",
"\n",
" \"\"\"\n",
" for i in range(1, 10):\n",
" aux = \"\"\n",
" for j in range(1, i + 1):\n",
" aux += str(i)\n",
" print(aux);\n",
"print(multiplicaletra.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.4.1\n",
"Dada una lista de enteros **a**, utilisando una comprensión, Imprimir una nueva lista que contenga \"par\" o \"impar\" en cada uno de sus elementos, dependiendo del valor del elemento en la lista orginal."
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"panagrama funcion para determinar si una palabra es panagrama o no\n",
"Dada una lista *a*, utilizando una comprensión, imprimir una lista de tuplas, en donde cada tupla contiene el indice y el valor de elemento de la lista orginal."
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"indicevspalabre funcion imprimier tuplas con valores indice, palabra\n",
" \"\"\"reducesuma funcion suma los numeros de una lista\n",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> reducesuma()\n",
" 193\n",
"\n",
" \"\"\"\n",
" a = [5, 8, 10, 20, 50, 100] \n",
" print(reduce((lambda x, y: x + y), a))\n",
"reducesuma()\n",
"print(reducesuma.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.4.6 \n",
"Utilizando *map* y una expresion *lambda* obtener una lista cuyos elementos sean la suma de los elementos correspondientes en las listas **a** y **b**.\n"
"### 1.5.4 Pregunta al usario cuantos numeros de la secuancia Fibonacci quiere calcular y escribe una funcion que calcule la secuencia e imprima el resultado."
]
},
{
"cell_type": "code",
"execution_count": 54,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"cuantos numeros de la secuancia Fibonacci quiere calcular:5\n",
"[0, 1, 1, 2, 3, 5]\n",
"fibonacci funcion para calcular la secuencia de fibonacci dato un numer n\n",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" int: el numero de fibonacci\n",
" Examples:\n",
" >>> print([i for i in fibonacci(6)])\n",
" [0, 1, 1, 2, 3, 5]\n",
"\n",
" \n",
"Trying:\n",
" print([i for i in example_generator(4)])\n",
"Expecting:\n",
" [0, 1, 2, 3]\n",
"ok\n",
"Trying:\n",
" print([i for i in fibonacci(6)])\n",
"Expecting:\n",
" [0, 1, 1, 2, 3, 5]\n",
"ok\n",
"Trying:\n",
" intersectab()\n",
"Expecting:\n",
" [1, 2, 3, 5, 8, 13]\n",
"ok\n",
"Trying:\n",
" printmayotto10()\n",
"Expecting:\n",
" [13, 21, 34, 55, 89]\n",
"ok\n",
"Trying:\n",
" rang1to20()\n",
"Expecting:\n",
" [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]\n",
"ok\n",
"2 items had no tests:\n",
" __main__\n",
" __main__.fib\n",
"5 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.fibonacci\n",
" 1 tests in __main__.intersectab\n",
" 1 tests in __main__.printmayotto10\n",
" 1 tests in __main__.rang1to20\n",
"5 tests in 7 items.\n",
"5 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=5)"
]
},
"execution_count": 54,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def fibonacci(n):\n",
" \"\"\"fibonacci funcion para calcular la secuencia de fibonacci dato un numer n\n",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" int: el numero de fibonacci\n",
" Examples:\n",
" >>> print([i for i in fibonacci(6)])\n",
" [0, 1, 1, 2, 3, 5]\n",
"\n",
" \"\"\"\n",
" \n",
" a, b = 0, 1\n",
" for _ in range(n):\n",
" yield a\n",
" a, b = b, a + b\n",
"\n",
"numero = int(input(\"cuantos numeros de la secuancia Fibonacci quiere calcular:\"))\n",
"\n",
"print([i for i in fibonacci(numero + 1)])\n",
"print(fibonacci.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.5.5 Escribe una funcion que sume todos los numeros en una lista usando for."
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"20\n",
"sumList funcion sumer los numeros de una lista\n",
"\n",
" Args:\n",
" lista (list): lista con los datos a sumar\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> sumList([8, 2, 3, 0, 7])\n",
" 20\n",
"\n",
" \n",
"Trying:\n",
" print([i for i in example_generator(4)])\n",
"Expecting:\n",
" [0, 1, 2, 3]\n",
"ok\n",
"Trying:\n",
" print([i for i in fibonacci(6)])\n",
"Expecting:\n",
" [0, 1, 1, 2, 3, 5]\n",
"ok\n",
"Trying:\n",
" intersectab()\n",
"Expecting:\n",
" [1, 2, 3, 5, 8, 13]\n",
"ok\n",
"Trying:\n",
" printmayotto10()\n",
"Expecting:\n",
" [13, 21, 34, 55, 89]\n",
"ok\n",
"Trying:\n",
" rang1to20()\n",
"Expecting:\n",
" [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]\n",
"ok\n",
"Trying:\n",
" sumList([8, 2, 3, 0, 7])\n",
"Expecting:\n",
" 20\n",
"ok\n",
"2 items had no tests:\n",
" __main__\n",
" __main__.fib\n",
"6 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.fibonacci\n",
" 1 tests in __main__.intersectab\n",
" 1 tests in __main__.printmayotto10\n",
" 1 tests in __main__.rang1to20\n",
" 1 tests in __main__.sumList\n",
"6 tests in 8 items.\n",
"6 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=6)"
]
},
"execution_count": 56,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = [8, 2, 3, 0, 7]\n",
"\n",
"def sumList(lista):\n",
" \"\"\"sumList funcion sumer los numeros de una lista\n",
"\n",
" Args:\n",
" lista (list): lista con los datos a sumar\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> sumList([8, 2, 3, 0, 7])\n",
" 20\n",
"\n",
" \"\"\"\n",
" print(sum(lista))\n",
" \n",
"sumList(a)\n",
"print(sumList.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.5.7 Escribe una funcion que indique si un numero es o no perfecto.\n",
"[Wikipedia:](https://es.wikipedia.org/wiki/N%C3%BAmero_perfecto) *Un número perfecto es un número natural que es igual a la suma de sus divisores propios positivos. Dicho de otra forma, un número perfecto es aquel que es amigo de sí mismo.\n",
"Así, 6 es un número perfecto porque sus divisores propios son 1, 2 y 3; y 6 = 1 + 2 + 3. Los siguientes números perfectos son 28, 496 y 8128.*\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Escribe un nuemero: 28\n",
"28 es un numero perfecto\n",
"perfect funcion para determinar si un numero es perfecto\n",
"\n",
" Args:\n",
" x (int): numero a verificar si es perfecto o no\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> perfect(28)\n",
" True\n",
" >>> perfect(288)\n",
" False\n",
"\n",
" \n",
"Trying:\n",
" print([i for i in example_generator(4)])\n",
"Expecting:\n",
" [0, 1, 2, 3]\n",
"ok\n",
"Trying:\n",
" print([i for i in fibonacci(6)])\n",
"Expecting:\n",
" [0, 1, 1, 2, 3, 5]\n",
"ok\n",
"Trying:\n",
" intersectab()\n",
"Expecting:\n",
" [1, 2, 3, 5, 8, 13]\n",
"ok\n",
"Trying:\n",
" perfect(28)\n",
"Expecting:\n",
" True\n",
"ok\n",
"Trying:\n",
" perfect(288)\n",
"Expecting:\n",
" False\n",
"ok\n",
"Trying:\n",
" printmayotto10()\n",
"Expecting:\n",
" [13, 21, 34, 55, 89]\n",
"ok\n",
"Trying:\n",
" rang1to20()\n",
"Expecting:\n",
" [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]\n",
"ok\n",
"Trying:\n",
" sumList([8, 2, 3, 0, 7])\n",
"Expecting:\n",
" 20\n",
"ok\n",
"2 items had no tests:\n",
" __main__\n",
" __main__.fib\n",
"7 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.fibonacci\n",
" 1 tests in __main__.intersectab\n",
" 2 tests in __main__.perfect\n",
" 1 tests in __main__.printmayotto10\n",
" 1 tests in __main__.rang1to20\n",
" 1 tests in __main__.sumList\n",
"8 tests in 9 items.\n",
"8 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=8)"
]
},
"execution_count": 60,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from functools import reduce\n",
"def perfect(x):\n",
" \"\"\"perfect funcion para determinar si un numero es perfecto\n",
"\n",
" Args:\n",
" x (int): numero a verificar si es perfecto o no\n",
"numero = int(input(\"Escribe un nuemero: \"))\n",
"if perfect(numero):\n",
" print(\"%s es un numero perfecto\" % numero)\n",
"else:\n",
" print(\"%s no es un numero perfecto\" % numero)\n",
"print(perfect.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.5.9 Escribe una funcion que indique si una frase es un panagrama.\n",
"[Wikipedia](https://es.wikipedia.org/wiki/Pangrama):Un pangrama (del griego: παν γραμμα, «todas las letras») o frase holoalfabética es un texto que usa todas las letras posibles del alfabeto de un idioma. "
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n",
"panagrama funcion para determinar si una palabra es panagrama o no\n",
"\n",
" Args:\n",
" frase (string): palabra a verificar si es panagrama o no\n",
"### 1.5.10 Escribe un programa que imprima el siguiente un **for** anidado.\n",
"1\n",
"\n",
"22\n",
"\n",
"333\n",
"\n",
"4444\n",
"\n",
"55555\n",
"\n",
"666666\n",
"\n",
"7777777\n",
"\n",
"88888888\n",
"\n",
"999999999"
]
},
{
"cell_type": "code",
"execution_count": 66,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"panagrama funcion para determinar si una palabra es panagrama o no\n",
"\n",
" Args:\n",
" frase (string): palabra a verificar si es panagrama o no\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> multiplicaletra()\n",
" 1\n",
" 22\n",
" 333\n",
" 4444\n",
" 55555\n",
" 666666\n",
" 7777777\n",
" 88888888\n",
" 999999999\n",
"\n",
" \n",
"Trying:\n",
" print([i for i in example_generator(4)])\n",
"Expecting:\n",
" [0, 1, 2, 3]\n",
"ok\n",
"Trying:\n",
" print([i for i in fibonacci(6)])\n",
"Expecting:\n",
" [0, 1, 1, 2, 3, 5]\n",
"ok\n",
"Trying:\n",
" intersectab()\n",
"Expecting:\n",
" [1, 2, 3, 5, 8, 13]\n",
"ok\n",
"Trying:\n",
" multiplicaletra()\n",
"Expecting:\n",
" 1\n",
" 22\n",
" 333\n",
" 4444\n",
" 55555\n",
" 666666\n",
" 7777777\n",
" 88888888\n",
" 999999999\n",
"ok\n",
"Trying:\n",
" panagrama(\"zbcdefghijkl onpqrstuvwxya\")\n",
"Expecting:\n",
" False\n",
"ok\n",
"Trying:\n",
" panagrama(\"abcdefghijkl onpqrstuvwxyz\")\n",
"Expecting:\n",
" False\n",
"ok\n",
"Trying:\n",
" panagrama(\"abcdefghijklmnopqrstuvwxyz\")\n",
"Expecting:\n",
" True\n",
"ok\n",
"Trying:\n",
" perfect(28)\n",
"Expecting:\n",
" True\n",
"ok\n",
"Trying:\n",
" perfect(288)\n",
"Expecting:\n",
" False\n",
"ok\n",
"Trying:\n",
" printmayotto10()\n",
"Expecting:\n",
" [13, 21, 34, 55, 89]\n",
"ok\n",
"Trying:\n",
" rang1to20()\n",
"Expecting:\n",
" [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]\n",
"ok\n",
"Trying:\n",
" sumList([8, 2, 3, 0, 7])\n",
"Expecting:\n",
" 20\n",
"ok\n",
"2 items had no tests:\n",
" __main__\n",
" __main__.fib\n",
"9 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.fibonacci\n",
" 1 tests in __main__.intersectab\n",
" 1 tests in __main__.multiplicaletra\n",
" 3 tests in __main__.panagrama\n",
" 2 tests in __main__.perfect\n",
" 1 tests in __main__.printmayotto10\n",
" 1 tests in __main__.rang1to20\n",
" 1 tests in __main__.sumList\n",
"12 tests in 11 items.\n",
"12 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=12)"
]
},
"execution_count": 66,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def multiplicaletra():\n",
" \"\"\"panagrama funcion para determinar si una palabra es panagrama o no\n",
"\n",
" Args:\n",
" frase (string): palabra a verificar si es panagrama o no\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> multiplicaletra()\n",
" 1\n",
" 22\n",
" 333\n",
" 4444\n",
" 55555\n",
" 666666\n",
" 7777777\n",
" 88888888\n",
" 999999999\n",
"\n",
" \"\"\"\n",
" for i in range(1, 10):\n",
" aux = \"\"\n",
" for j in range(1, i + 1):\n",
" aux += str(i)\n",
" print(aux);\n",
"print(multiplicaletra.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.4.1\n",
"Dada una lista de enteros **a**, utilisando una comprensión, Imprimir una nueva lista que contenga \"par\" o \"impar\" en cada uno de sus elementos, dependiendo del valor del elemento en la lista orginal."
]
},
{
"cell_type": "code",
"execution_count": 70,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"panagrama funcion para determinar si una palabra es panagrama o no\n",
"Dada una lista *a*, utilizando una comprensión, imprimir una lista de tuplas, en donde cada tupla contiene el indice y el valor de elemento de la lista orginal."
]
},
{
"cell_type": "code",
"execution_count": 85,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"indicevspalabre funcion imprimier tuplas con valores indice, palabra\n",
" \"\"\"reducesuma funcion suma los numeros de una lista\n",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> reducesuma()\n",
" 193\n",
"\n",
" \"\"\"\n",
" a = [5, 8, 10, 20, 50, 100] \n",
" print(reduce((lambda x, y: x + y), a))\n",
"reducesuma()\n",
"print(reducesuma.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.4.6 \n",
"Utilizando *map* y una expresion *lambda* obtener una lista cuyos elementos sean la suma de los elementos correspondientes en las listas **a** y **b**.\n"