Commit 6b33091f authored by geobumac's avatar geobumac

ejercicios clase 2

parent 3cf0f0cf
...@@ -24,7 +24,9 @@ ...@@ -24,7 +24,9 @@
"* Interpretado (Se ejecuta sin compilación previa)\n", "* Interpretado (Se ejecuta sin compilación previa)\n",
"* Tipificación Dinamica (Se realiza durante en tiempo de ejecución)\n", "* Tipificación Dinamica (Se realiza durante en tiempo de ejecución)\n",
"* Multiparadigma\n", "* Multiparadigma\n",
"* Interactivo (con ipython)\n" "* Interactivo (con ipython)\n",
"\n",
"*Nota: Python obtiene su nombre del programa de la BBC [Monty Python's Flying Circus](https://www.imdb.com/title/tt0063929/).*"
] ]
}, },
{ {
...@@ -921,7 +923,7 @@ ...@@ -921,7 +923,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### 1.5.5 Escribe una funcion que sume todos los uneros en una lista." "### 1.5.5 Escribe una funcion que sume todos los numeros en una lista usando for."
] ]
}, },
{ {
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 31, "execution_count": 1,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -25,7 +25,7 @@ ...@@ -25,7 +25,7 @@
"[2, 2, 4, 6, 8]" "[2, 2, 4, 6, 8]"
] ]
}, },
"execution_count": 31, "execution_count": 1,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
...@@ -41,7 +41,7 @@ ...@@ -41,7 +41,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 40, "execution_count": 2,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -50,7 +50,7 @@ ...@@ -50,7 +50,7 @@
"[2, 2, 4, 6, 8]" "[2, 2, 4, 6, 8]"
] ]
}, },
"execution_count": 40, "execution_count": 2,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
...@@ -63,7 +63,7 @@ ...@@ -63,7 +63,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 38, "execution_count": 3,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -81,7 +81,7 @@ ...@@ -81,7 +81,7 @@
" 9: 387420489}" " 9: 387420489}"
] ]
}, },
"execution_count": 38, "execution_count": 3,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
...@@ -93,7 +93,7 @@ ...@@ -93,7 +93,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 37, "execution_count": 4,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -102,7 +102,7 @@ ...@@ -102,7 +102,7 @@
"[[1, 0, 0], [0, 1, 0], [0, 0, 1]]" "[[1, 0, 0], [0, 1, 0], [0, 0, 1]]"
] ]
}, },
"execution_count": 37, "execution_count": 4,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
...@@ -114,7 +114,7 @@ ...@@ -114,7 +114,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 34,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -123,7 +123,7 @@ ...@@ -123,7 +123,7 @@
"{3, 6, 9}" "{3, 6, 9}"
] ]
}, },
"execution_count": 2, "execution_count": 34,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
...@@ -152,7 +152,7 @@ ...@@ -152,7 +152,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 77, "execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -161,7 +161,7 @@ ...@@ -161,7 +161,7 @@
"function" "function"
] ]
}, },
"execution_count": 77, "execution_count": 6,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
...@@ -173,7 +173,7 @@ ...@@ -173,7 +173,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 41,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -182,7 +182,7 @@ ...@@ -182,7 +182,7 @@
"[27, 46656, 387420489]" "[27, 46656, 387420489]"
] ]
}, },
"execution_count": 4, "execution_count": 41,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
...@@ -194,7 +194,7 @@ ...@@ -194,7 +194,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 47, "execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -203,7 +203,7 @@ ...@@ -203,7 +203,7 @@
"[(13, -3), (4, 1), (1, 2), (9, 10)]" "[(13, -3), (4, 1), (1, 2), (9, 10)]"
] ]
}, },
"execution_count": 47, "execution_count": 8,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
...@@ -216,7 +216,7 @@ ...@@ -216,7 +216,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 51, "execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -243,7 +243,7 @@ ...@@ -243,7 +243,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 55, "execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -274,7 +274,7 @@ ...@@ -274,7 +274,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 66, "execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -299,6 +299,43 @@ ...@@ -299,6 +299,43 @@
"print(list(doublesG))" "print(list(doublesG))"
] ]
}, },
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Cuatos Fibonacci?: 5\n"
]
},
{
"data": {
"text/plain": [
"7"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = int(input('Cuatos Fibonacci?: '))\n",
"\n",
"def fib(n):\n",
" a, b = 0, 1\n",
" for _ in range(n):\n",
" yield a\n",
" a, b = b, a + b\n",
"\n",
"sum(fib(a)) \n",
"#for n in fib(a):\n",
"# print(n)"
]
},
{ {
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
...@@ -310,7 +347,7 @@ ...@@ -310,7 +347,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 3, "execution_count": 67,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -339,7 +376,7 @@ ...@@ -339,7 +376,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 25, "execution_count": 69,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -369,7 +406,7 @@ ...@@ -369,7 +406,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 27, "execution_count": 72,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -440,7 +477,7 @@ ...@@ -440,7 +477,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 74,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -469,7 +506,7 @@ ...@@ -469,7 +506,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 34, "execution_count": 76,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -499,7 +536,7 @@ ...@@ -499,7 +536,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 39, "execution_count": 78,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -565,11 +602,31 @@ ...@@ -565,11 +602,31 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 80, "execution_count": 67,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"[['a', 'a'],\n",
" ['a', 'b'],\n",
" ['a', 'c'],\n",
" ['b', 'a'],\n",
" ['b', 'b'],\n",
" ['b', 'c'],\n",
" ['c', 'a'],\n",
" ['c', 'b'],\n",
" ['c', 'c']]"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"a = [\"a\", \"b\", \"c\"]\n" "a = [\"a\", \"b\", \"c\"]\n",
"[[i,n] for i in a for n in a]"
] ]
}, },
{ {
...@@ -589,17 +646,9 @@ ...@@ -589,17 +646,9 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [],
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello\n"
]
}
],
"source": [ "source": [
"from miModulo import helloWorld\n", "from miModulo import helloWorld\n",
"helloWorld.hello()" "helloWorld.hello()"
...@@ -616,7 +665,7 @@ ...@@ -616,7 +665,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 89, "execution_count": 1,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -697,7 +746,7 @@ ...@@ -697,7 +746,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": null,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [],
"source": [ "source": [
...@@ -708,49 +757,9 @@ ...@@ -708,49 +757,9 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n",
"<html><head><title>Python: module helloWorld</title>\n",
"<meta charset=\"utf-8\">\n",
"</head><body bgcolor=\"#f0f0f8\">\n",
"\n",
"<table width=\"100%\" cellspacing=0 cellpadding=2 border=0 summary=\"heading\">\n",
"<tr bgcolor=\"#7799ee\">\n",
"<td valign=bottom>&nbsp;<br>\n",
"<font color=\"#ffffff\" face=\"helvetica, arial\">&nbsp;<br><big><big><strong>helloWorld</strong></big></big></font></td\n",
"><td align=right valign=bottom\n",
"><font color=\"#ffffff\" face=\"helvetica, arial\"><a href=\".\">index</a><br><a href=\"file:/home/mchc/git/tap1012/miModulo/helloWorld.py\">/home/mchc/git/tap1012/miModulo/helloWorld.py</a></font></td></tr></table>\n",
" <p><tt>This&nbsp;example&nbsp;module&nbsp;shows&nbsp;various&nbsp;types&nbsp;of&nbsp;documentation&nbsp;available&nbsp;for&nbsp;use<br>\n",
"with&nbsp;pydoc.&nbsp;&nbsp;To&nbsp;generate&nbsp;HTML&nbsp;documentation&nbsp;for&nbsp;this&nbsp;module&nbsp;issue&nbsp;the<br>\n",
"command:<br>\n",
"&nbsp;<br>\n",
"&nbsp;&nbsp;&nbsp;&nbsp;pydoc&nbsp;-w&nbsp;foo</tt></p>\n",
"<p>\n",
"<table width=\"100%\" cellspacing=0 cellpadding=2 border=0 summary=\"section\">\n",
"<tr bgcolor=\"#eeaa77\">\n",
"<td colspan=3 valign=bottom>&nbsp;<br>\n",
"<font color=\"#ffffff\" face=\"helvetica, arial\"><big><strong>Functions</strong></big></font></td></tr>\n",
" \n",
"<tr><td bgcolor=\"#eeaa77\"><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</tt></td><td>&nbsp;</td>\n",
"<td width=\"100%\"><dl><dt><a name=\"-hello\"><strong>hello</strong></a>()</dt><dd><tt>Documentacion&nbsp;de&nbsp;función&nbsp;<a href=\"#-hello\">hello</a>()&nbsp;del&nbsp;modulo&nbsp;miModulo.</tt></dd></dl>\n",
"</td></tr></table>\n",
"</body></html>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"execution_count": 6,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "outputs": [],
}
],
"source": [ "source": [
"from IPython.display import HTML\n", "from IPython.display import HTML\n",
"HTML(filename=\"miModulo/helloWorld.html\")" "HTML(filename=\"miModulo/helloWorld.html\")"
...@@ -767,7 +776,7 @@ ...@@ -767,7 +776,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 1,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -799,7 +808,7 @@ ...@@ -799,7 +808,7 @@
"TestResults(failed=0, attempted=2)" "TestResults(failed=0, attempted=2)"
] ]
}, },
"execution_count": 10, "execution_count": 1,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
...@@ -826,6 +835,1972 @@ ...@@ -826,6 +835,1972 @@
"Escribe la documentacion y los casos de prueba para todos los ejercicios de la semana 1 y 2." "Escribe la documentacion y los casos de prueba para todos los ejercicios de la semana 1 y 2."
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.5.1 Imprimir todos los numeros pares entre 0 y 20 usando *for* o *while*."
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]\n",
"rang1to20 funcion par alistar los numero pares entre 0 y 20 \n",
"\n",
" Args:\n",
"\n",
" Yields:\n",
"\n",
" Examples:\n",
" >>> rang1to20()\n",
" [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 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",
" 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",
"1 items had no tests:\n",
" __main__\n",
"4 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.intersectab\n",
" 1 tests in __main__.printmayotto10\n",
" 1 tests in __main__.rang1to20\n",
"4 tests in 5 items.\n",
"4 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=4)"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def rang1to20():\n",
" \"\"\"rang1to20 funcion par alistar los numero pares entre 0 y 20 \n",
"\n",
" Args:\n",
"\n",
" Yields:\n",
"\n",
" Examples:\n",
" >>> rang1to20()\n",
" [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]\n",
"\n",
" \"\"\"\n",
" print([i for i in range(21) if i%2 == 0])\n",
"rang1to20()\n",
"print(rang1to20.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.5.2 Imprimir todos los numeros mayores a 10 de la lista A"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[13, 21, 34, 55, 89]\n",
"rang1to20 funcion par alistar los numeros mayores a 10 dada la lista A\n",
"\n",
" Args:\n",
"\n",
" Yields:\n",
"\n",
" Examples:\n",
" >>> printmayotto10()\n",
" [13, 21, 34, 55, 89]\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",
" 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",
"1 items had no tests:\n",
" __main__\n",
"4 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.intersectab\n",
" 1 tests in __main__.printmayotto10\n",
" 1 tests in __main__.rang1to20\n",
"4 tests in 5 items.\n",
"4 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=4)"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def printmayotto10():\n",
" \"\"\"rang1to20 funcion par alistar los numeros mayores a 10 dada la lista A\n",
"\n",
" Args:\n",
"\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> printmayotto10()\n",
" [13, 21, 34, 55, 89]\n",
"\n",
" \"\"\"\n",
" A = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]\n",
" print([i for i in A if i>10])\n",
"printmayotto10()\n",
"print(printmayotto10.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.5.3 Dadas dos listas A y B, obten una lista con sus elementos comunes (A∩B)."
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 5, 8, 13]\n",
"intersectab funcion par realizar la interseccion de las listas a y b\n",
"\n",
" Args:\n",
"\n",
" Yields:\n",
"\n",
" Examples:\n",
" >>> intersectab()\n",
" [1, 2, 3, 5, 8, 13]\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",
" 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",
"1 items had no tests:\n",
" __main__\n",
"4 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.intersectab\n",
" 1 tests in __main__.printmayotto10\n",
" 1 tests in __main__.rang1to20\n",
"4 tests in 5 items.\n",
"4 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=4)"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def intersectab():\n",
" \"\"\"intersectab funcion par realizar la interseccion de las listas a y b\n",
"\n",
" Args:\n",
"\n",
" Yields:\n",
"\n",
" Examples:\n",
" >>> intersectab()\n",
" [1, 2, 3, 5, 8, 13]\n",
"\n",
" \"\"\"\n",
" a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]\n",
" b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]\n",
" print(list(set(a) & set(b)))\n",
"\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 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",
" Yields:\n",
" \n",
" Examples:\n",
" >>> perfect(28)\n",
" True\n",
" >>> perfect(288)\n",
" False\n",
"\n",
" \"\"\"\n",
" a = range(1,x)\n",
" suma = reduce((lambda x, y: x + y), list(filter(lambda i: x%i == 0, a)))\n",
" return x == suma\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",
" Yields:\n",
" \n",
" Examples:\n",
" >>> panagrama(\"zbcdefghijkl onpqrstuvwxya\")\n",
" False\n",
" >>> panagrama(\"abcdefghijkl onpqrstuvwxyz\")\n",
" False\n",
" >>> panagrama(\"abcdefghijklmnopqrstuvwxyz\")\n",
" True\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",
" 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",
"8 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.fibonacci\n",
" 1 tests in __main__.intersectab\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",
"11 tests in 10 items.\n",
"11 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=11)"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def panagrama(frase):\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",
" >>> panagrama(\"zbcdefghijkl onpqrstuvwxya\")\n",
" False\n",
" >>> panagrama(\"abcdefghijkl onpqrstuvwxyz\")\n",
" False\n",
" >>> panagrama(\"abcdefghijklmnopqrstuvwxyz\")\n",
" True\n",
"\n",
" \"\"\"\n",
" alphabet = \"abcdefghijklmnopqrstuvwxyz\"\n",
" return not (set(alphabet) - set(frase))\n",
"print(panagrama(\"zbcdefghijkl onpqrstuvwxya\"))\n",
"print(panagrama.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 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",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> parorimpar()\n",
" ['par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar']\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",
" parorimpar()\n",
"Expecting:\n",
" ['par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar']\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",
"10 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",
" 1 tests in __main__.parorimpar\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",
"13 tests in 12 items.\n",
"13 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=13)"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def parorimpar():\n",
" \"\"\"panagrama funcion para determinar si una palabra es panagrama o no\n",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> parorimpar()\n",
" ['par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar']\n",
"\n",
" \"\"\"\n",
" a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
" b = list(map(lambda x: 'par'if x%2== 0 else 'impar', a))\n",
" print(b)\n",
"print(parorimpar.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.4.2\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",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> indicevspalabre()\n",
" [[0, 'apple'], [1, 'banana'], [2, 'grapes'], [3, 'pear']]\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",
" filtroimpares()\n",
"Expecting:\n",
" [5, 7, 97, 77, 23, 73, 61]\n",
"ok\n",
"Trying:\n",
" indicevspalabre()\n",
"Expecting:\n",
" [[0, 'apple'], [1, 'banana'], [2, 'grapes'], [3, 'pear']]\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",
" parorimpar()\n",
"Expecting:\n",
" ['par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar']\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",
"12 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.fibonacci\n",
" 1 tests in __main__.filtroimpares\n",
" 1 tests in __main__.indicevspalabre\n",
" 1 tests in __main__.intersectab\n",
" 1 tests in __main__.multiplicaletra\n",
" 3 tests in __main__.panagrama\n",
" 1 tests in __main__.parorimpar\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",
"15 tests in 14 items.\n",
"15 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=15)"
]
},
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def indicevspalabre():\n",
" \"\"\"indicevspalabre funcion imprimier tuplas con valores indice, palabra\n",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> indicevspalabre()\n",
" [[0, 'apple'], [1, 'banana'], [2, 'grapes'], [3, 'pear']]\n",
"\n",
" \"\"\"\n",
" my_list = ['apple', 'banana', 'grapes', 'pear']\n",
" print([[i,my_list[i]] for i in range(0,len(my_list))])\n",
"print(indicevspalabre.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.4.4 \n",
"Utilizando *filter* y una expresion *lambda* generar una lista que contenga todos los numeros impares de una lista de entrada **a**."
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[5, 7, 97, 77, 23, 73, 61]\n",
"filtroimpares funcion filtrar numeros impares\n",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> filtroimpares()\n",
" [5, 7, 97, 77, 23, 73, 61]\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",
" filtroimpares()\n",
"Expecting:\n",
" [5, 7, 97, 77, 23, 73, 61]\n",
"ok\n",
"Trying:\n",
" indicevspalabre()\n",
"Expecting:\n",
" [[0, 'apple'], [1, 'banana'], [2, 'grapes'], [3, 'pear']]\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",
" parorimpar()\n",
"Expecting:\n",
" ['par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar']\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",
"12 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.fibonacci\n",
" 1 tests in __main__.filtroimpares\n",
" 1 tests in __main__.indicevspalabre\n",
" 1 tests in __main__.intersectab\n",
" 1 tests in __main__.multiplicaletra\n",
" 3 tests in __main__.panagrama\n",
" 1 tests in __main__.parorimpar\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",
"15 tests in 14 items.\n",
"15 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=15)"
]
},
"execution_count": 86,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def filtroimpares():\n",
" \"\"\"filtroimpares funcion filtrar numeros impares\n",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> filtroimpares()\n",
" [5, 7, 97, 77, 23, 73, 61]\n",
"\n",
" \"\"\"\n",
" a=[5, 7, 22, 97, 54, 62, 77, 23, 73, 61]\n",
" print(list(filter(lambda x:x%2 != 0, a)))\n",
"filtroimpares()\n",
"print(filtroimpares.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.4.5\n",
"Utilizado ***reduce*** y una expresion *lambda*, obten la suma de todos lo elementos en una lista."
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"193\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",
"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",
" filtroimpares()\n",
"Expecting:\n",
" [5, 7, 97, 77, 23, 73, 61]\n",
"ok\n",
"Trying:\n",
" indicevspalabre()\n",
"Expecting:\n",
" [[0, 'apple'], [1, 'banana'], [2, 'grapes'], [3, 'pear']]\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",
" parorimpar()\n",
"Expecting:\n",
" ['par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar']\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",
" reducesuma()\n",
"Expecting:\n",
" 193\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",
"13 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.fibonacci\n",
" 1 tests in __main__.filtroimpares\n",
" 1 tests in __main__.indicevspalabre\n",
" 1 tests in __main__.intersectab\n",
" 1 tests in __main__.multiplicaletra\n",
" 3 tests in __main__.panagrama\n",
" 1 tests in __main__.parorimpar\n",
" 2 tests in __main__.perfect\n",
" 1 tests in __main__.printmayotto10\n",
" 1 tests in __main__.rang1to20\n",
" 1 tests in __main__.reducesuma\n",
" 1 tests in __main__.sumList\n",
"16 tests in 15 items.\n",
"16 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=16)"
]
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from functools import reduce\n",
"def reducesuma():\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"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[35, 45, 55, 65]\n",
"mapsuma funcion obtener suma de numeros\n",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> reducesuma()\n",
" 193\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",
" filtroimpares()\n",
"Expecting:\n",
" [5, 7, 97, 77, 23, 73, 61]\n",
"ok\n",
"Trying:\n",
" indicevspalabre()\n",
"Expecting:\n",
" [[0, 'apple'], [1, 'banana'], [2, 'grapes'], [3, 'pear']]\n",
"ok\n",
"Trying:\n",
" intersectab()\n",
"Expecting:\n",
" [1, 2, 3, 5, 8, 13]\n",
"ok\n",
"Trying:\n",
" reducesuma()\n",
"Expecting:\n",
" 193\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",
" parorimpar()\n",
"Expecting:\n",
" ['par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar']\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",
" reducesuma()\n",
"Expecting:\n",
" 193\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",
"14 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.fibonacci\n",
" 1 tests in __main__.filtroimpares\n",
" 1 tests in __main__.indicevspalabre\n",
" 1 tests in __main__.intersectab\n",
" 1 tests in __main__.mapsuma\n",
" 1 tests in __main__.multiplicaletra\n",
" 3 tests in __main__.panagrama\n",
" 1 tests in __main__.parorimpar\n",
" 2 tests in __main__.perfect\n",
" 1 tests in __main__.printmayotto10\n",
" 1 tests in __main__.rang1to20\n",
" 1 tests in __main__.reducesuma\n",
" 1 tests in __main__.sumList\n",
"17 tests in 16 items.\n",
"17 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=17)"
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def mapsuma(): \n",
" \"\"\"mapsuma funcion obtener suma de numeros\n",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> reducesuma()\n",
" 193\n",
"\n",
" \"\"\"\n",
" a = [5, 10, 15, 20]\n",
"\n",
" b = [30, 35, 40, 45]\n",
"\n",
" print(list(map(lambda x,y: x+y, a,b)))\n",
"mapsuma()\n",
"print(mapsuma.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.6.8 \n",
"Escribir un generador en forma de funcion recursiva que entregue la lista de todas las permitaciones de los elementos en una lista."
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[['a', 'a', 'a'], ['a', 'a', 'b'], ['a', 'a', 'c'], ['a', 'b', 'a'], ['a', 'b', 'b'], ['a', 'b', 'c'], ['a', 'c', 'a'], ['a', 'c', 'b'], ['a', 'c', 'c'], ['b', 'a', 'a'], ['b', 'a', 'b'], ['b', 'a', 'c'], ['b', 'b', 'a'], ['b', 'b', 'b'], ['b', 'b', 'c'], ['b', 'c', 'a'], ['b', 'c', 'b'], ['b', 'c', 'c'], ['c', 'a', 'a'], ['c', 'a', 'b'], ['c', 'a', 'c'], ['c', 'b', 'a'], ['c', 'b', 'b'], ['c', 'b', 'c'], ['c', 'c', 'a'], ['c', 'c', 'b'], ['c', 'c', 'c']]\n",
"permutacion funcion para obtener permutacion de un arreglo\n",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> permutacion()\n",
" [['a', 'a', 'a'], ['a', 'a', 'b'], ['a', 'a', 'c'], ['a', 'b', 'a'], ['a', 'b', 'b'], ['a', 'b', 'c'], ['a', 'c', 'a'], ['a', 'c', 'b'], ['a', 'c', 'c'], ['b', 'a', 'a'], ['b', 'a', 'b'], ['b', 'a', 'c'], ['b', 'b', 'a'], ['b', 'b', 'b'], ['b', 'b', 'c'], ['b', 'c', 'a'], ['b', 'c', 'b'], ['b', 'c', 'c'], ['c', 'a', 'a'], ['c', 'a', 'b'], ['c', 'a', 'c'], ['c', 'b', 'a'], ['c', 'b', 'b'], ['c', 'b', 'c'], ['c', 'c', 'a'], ['c', 'c', 'b'], ['c', 'c', 'c']]\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",
" filtroimpares()\n",
"Expecting:\n",
" [5, 7, 97, 77, 23, 73, 61]\n",
"ok\n",
"Trying:\n",
" indicevspalabre()\n",
"Expecting:\n",
" [[0, 'apple'], [1, 'banana'], [2, 'grapes'], [3, 'pear']]\n",
"ok\n",
"Trying:\n",
" intersectab()\n",
"Expecting:\n",
" [1, 2, 3, 5, 8, 13]\n",
"ok\n",
"Trying:\n",
" reducesuma()\n",
"Expecting:\n",
" 193\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",
" parorimpar()\n",
"Expecting:\n",
" ['par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar']\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",
" permutacion()\n",
"Expecting:\n",
" [['a', 'a', 'a'], ['a', 'a', 'b'], ['a', 'a', 'c'], ['a', 'b', 'a'], ['a', 'b', 'b'], ['a', 'b', 'c'], ['a', 'c', 'a'], ['a', 'c', 'b'], ['a', 'c', 'c'], ['b', 'a', 'a'], ['b', 'a', 'b'], ['b', 'a', 'c'], ['b', 'b', 'a'], ['b', 'b', 'b'], ['b', 'b', 'c'], ['b', 'c', 'a'], ['b', 'c', 'b'], ['b', 'c', 'c'], ['c', 'a', 'a'], ['c', 'a', 'b'], ['c', 'a', 'c'], ['c', 'b', 'a'], ['c', 'b', 'b'], ['c', 'b', 'c'], ['c', 'c', 'a'], ['c', 'c', 'b'], ['c', 'c', 'c']]\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",
" reducesuma()\n",
"Expecting:\n",
" 193\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",
"15 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.fibonacci\n",
" 1 tests in __main__.filtroimpares\n",
" 1 tests in __main__.indicevspalabre\n",
" 1 tests in __main__.intersectab\n",
" 1 tests in __main__.mapsuma\n",
" 1 tests in __main__.multiplicaletra\n",
" 3 tests in __main__.panagrama\n",
" 1 tests in __main__.parorimpar\n",
" 2 tests in __main__.perfect\n",
" 1 tests in __main__.permutacion\n",
" 1 tests in __main__.printmayotto10\n",
" 1 tests in __main__.rang1to20\n",
" 1 tests in __main__.reducesuma\n",
" 1 tests in __main__.sumList\n",
"18 tests in 17 items.\n",
"18 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=18)"
]
},
"execution_count": 95,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def permutacion():\n",
" \"\"\"permutacion funcion para obtener permutacion de un arreglo\n",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> permutacion()\n",
" [['a', 'a', 'a'], ['a', 'a', 'b'], ['a', 'a', 'c'], ['a', 'b', 'a'], ['a', 'b', 'b'], ['a', 'b', 'c'], ['a', 'c', 'a'], ['a', 'c', 'b'], ['a', 'c', 'c'], ['b', 'a', 'a'], ['b', 'a', 'b'], ['b', 'a', 'c'], ['b', 'b', 'a'], ['b', 'b', 'b'], ['b', 'b', 'c'], ['b', 'c', 'a'], ['b', 'c', 'b'], ['b', 'c', 'c'], ['c', 'a', 'a'], ['c', 'a', 'b'], ['c', 'a', 'c'], ['c', 'b', 'a'], ['c', 'b', 'b'], ['c', 'b', 'c'], ['c', 'c', 'a'], ['c', 'c', 'b'], ['c', 'c', 'c']]\n",
"\n",
" \"\"\"\n",
" a = [\"a\", \"b\", \"c\"]\n",
" print([[i,n,j] for i in a for n in a for j in a])\n",
"permutacion()\n",
"print(permutacion.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
......
...@@ -862,7 +862,7 @@ ...@@ -862,7 +862,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 30, "execution_count": 2,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -888,7 +888,7 @@ ...@@ -888,7 +888,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 31, "execution_count": 1,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
......
...@@ -301,9 +301,27 @@ ...@@ -301,9 +301,27 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 43,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Cuatos Fibonacci?: 5\n"
]
},
{
"data": {
"text/plain": [
"7"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"a = int(input('Cuatos Fibonacci?: '))\n", "a = int(input('Cuatos Fibonacci?: '))\n",
"\n", "\n",
...@@ -584,11 +602,31 @@ ...@@ -584,11 +602,31 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 67,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"data": {
"text/plain": [
"[['a', 'a'],\n",
" ['a', 'b'],\n",
" ['a', 'c'],\n",
" ['b', 'a'],\n",
" ['b', 'b'],\n",
" ['b', 'c'],\n",
" ['c', 'a'],\n",
" ['c', 'b'],\n",
" ['c', 'c']]"
]
},
"execution_count": 67,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [ "source": [
"a = [\"a\", \"b\", \"c\"]\n" "a = [\"a\", \"b\", \"c\"]\n",
"[[i,n] for i in a for n in a]"
] ]
}, },
{ {
...@@ -627,9 +665,50 @@ ...@@ -627,9 +665,50 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": 1,
"metadata": {}, "metadata": {},
"outputs": [], "outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Generators have a ``Yields`` section instead of a ``Returns`` section.\n",
"\n",
" Args:\n",
" n (int): The upper limit of the range to generate, from 0 to `n` - 1.\n",
"\n",
" Yields:\n",
" int: The next number in the range of 0 to `n` - 1.\n",
"\n",
" Examples:\n",
" Examples should be written in doctest format, and should illustrate how\n",
" to use the function.\n",
"\n",
" >>> print([i for i in example_generator(4)])\n",
" [0, 1, 2, 3]\n",
"\n",
" \n",
"Help on function example_generator in module __main__:\n",
"\n",
"example_generator(n)\n",
" Generators have a ``Yields`` section instead of a ``Returns`` section.\n",
" \n",
" Args:\n",
" n (int): The upper limit of the range to generate, from 0 to `n` - 1.\n",
" \n",
" Yields:\n",
" int: The next number in the range of 0 to `n` - 1.\n",
" \n",
" Examples:\n",
" Examples should be written in doctest format, and should illustrate how\n",
" to use the function.\n",
" \n",
" >>> print([i for i in example_generator(4)])\n",
" [0, 1, 2, 3]\n",
"\n"
]
}
],
"source": [ "source": [
"def example_generator(n):\n", "def example_generator(n):\n",
" \"\"\"Generators have a ``Yields`` section instead of a ``Returns`` section.\n", " \"\"\"Generators have a ``Yields`` section instead of a ``Returns`` section.\n",
...@@ -756,6 +835,1972 @@ ...@@ -756,6 +835,1972 @@
"Escribe la documentacion y los casos de prueba para todos los ejercicios de la semana 1 y 2." "Escribe la documentacion y los casos de prueba para todos los ejercicios de la semana 1 y 2."
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.5.1 Imprimir todos los numeros pares entre 0 y 20 usando *for* o *while*."
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]\n",
"rang1to20 funcion par alistar los numero pares entre 0 y 20 \n",
"\n",
" Args:\n",
"\n",
" Yields:\n",
"\n",
" Examples:\n",
" >>> rang1to20()\n",
" [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 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",
" 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",
"1 items had no tests:\n",
" __main__\n",
"4 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.intersectab\n",
" 1 tests in __main__.printmayotto10\n",
" 1 tests in __main__.rang1to20\n",
"4 tests in 5 items.\n",
"4 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=4)"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def rang1to20():\n",
" \"\"\"rang1to20 funcion par alistar los numero pares entre 0 y 20 \n",
"\n",
" Args:\n",
"\n",
" Yields:\n",
"\n",
" Examples:\n",
" >>> rang1to20()\n",
" [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]\n",
"\n",
" \"\"\"\n",
" print([i for i in range(21) if i%2 == 0])\n",
"rang1to20()\n",
"print(rang1to20.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.5.2 Imprimir todos los numeros mayores a 10 de la lista A"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[13, 21, 34, 55, 89]\n",
"rang1to20 funcion par alistar los numeros mayores a 10 dada la lista A\n",
"\n",
" Args:\n",
"\n",
" Yields:\n",
"\n",
" Examples:\n",
" >>> printmayotto10()\n",
" [13, 21, 34, 55, 89]\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",
" 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",
"1 items had no tests:\n",
" __main__\n",
"4 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.intersectab\n",
" 1 tests in __main__.printmayotto10\n",
" 1 tests in __main__.rang1to20\n",
"4 tests in 5 items.\n",
"4 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=4)"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def printmayotto10():\n",
" \"\"\"rang1to20 funcion par alistar los numeros mayores a 10 dada la lista A\n",
"\n",
" Args:\n",
"\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> printmayotto10()\n",
" [13, 21, 34, 55, 89]\n",
"\n",
" \"\"\"\n",
" A = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]\n",
" print([i for i in A if i>10])\n",
"printmayotto10()\n",
"print(printmayotto10.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.5.3 Dadas dos listas A y B, obten una lista con sus elementos comunes (A∩B)."
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 5, 8, 13]\n",
"intersectab funcion par realizar la interseccion de las listas a y b\n",
"\n",
" Args:\n",
"\n",
" Yields:\n",
"\n",
" Examples:\n",
" >>> intersectab()\n",
" [1, 2, 3, 5, 8, 13]\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",
" 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",
"1 items had no tests:\n",
" __main__\n",
"4 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.intersectab\n",
" 1 tests in __main__.printmayotto10\n",
" 1 tests in __main__.rang1to20\n",
"4 tests in 5 items.\n",
"4 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=4)"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def intersectab():\n",
" \"\"\"intersectab funcion par realizar la interseccion de las listas a y b\n",
"\n",
" Args:\n",
"\n",
" Yields:\n",
"\n",
" Examples:\n",
" >>> intersectab()\n",
" [1, 2, 3, 5, 8, 13]\n",
"\n",
" \"\"\"\n",
" a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]\n",
" b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]\n",
" print(list(set(a) & set(b)))\n",
"\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 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",
" Yields:\n",
" \n",
" Examples:\n",
" >>> perfect(28)\n",
" True\n",
" >>> perfect(288)\n",
" False\n",
"\n",
" \"\"\"\n",
" a = range(1,x)\n",
" suma = reduce((lambda x, y: x + y), list(filter(lambda i: x%i == 0, a)))\n",
" return x == suma\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",
" Yields:\n",
" \n",
" Examples:\n",
" >>> panagrama(\"zbcdefghijkl onpqrstuvwxya\")\n",
" False\n",
" >>> panagrama(\"abcdefghijkl onpqrstuvwxyz\")\n",
" False\n",
" >>> panagrama(\"abcdefghijklmnopqrstuvwxyz\")\n",
" True\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",
" 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",
"8 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.fibonacci\n",
" 1 tests in __main__.intersectab\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",
"11 tests in 10 items.\n",
"11 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=11)"
]
},
"execution_count": 62,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def panagrama(frase):\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",
" >>> panagrama(\"zbcdefghijkl onpqrstuvwxya\")\n",
" False\n",
" >>> panagrama(\"abcdefghijkl onpqrstuvwxyz\")\n",
" False\n",
" >>> panagrama(\"abcdefghijklmnopqrstuvwxyz\")\n",
" True\n",
"\n",
" \"\"\"\n",
" alphabet = \"abcdefghijklmnopqrstuvwxyz\"\n",
" return not (set(alphabet) - set(frase))\n",
"print(panagrama(\"zbcdefghijkl onpqrstuvwxya\"))\n",
"print(panagrama.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 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",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> parorimpar()\n",
" ['par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar']\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",
" parorimpar()\n",
"Expecting:\n",
" ['par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar']\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",
"10 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",
" 1 tests in __main__.parorimpar\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",
"13 tests in 12 items.\n",
"13 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=13)"
]
},
"execution_count": 70,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def parorimpar():\n",
" \"\"\"panagrama funcion para determinar si una palabra es panagrama o no\n",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> parorimpar()\n",
" ['par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar']\n",
"\n",
" \"\"\"\n",
" a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
" b = list(map(lambda x: 'par'if x%2== 0 else 'impar', a))\n",
" print(b)\n",
"print(parorimpar.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.4.2\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",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> indicevspalabre()\n",
" [[0, 'apple'], [1, 'banana'], [2, 'grapes'], [3, 'pear']]\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",
" filtroimpares()\n",
"Expecting:\n",
" [5, 7, 97, 77, 23, 73, 61]\n",
"ok\n",
"Trying:\n",
" indicevspalabre()\n",
"Expecting:\n",
" [[0, 'apple'], [1, 'banana'], [2, 'grapes'], [3, 'pear']]\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",
" parorimpar()\n",
"Expecting:\n",
" ['par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar']\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",
"12 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.fibonacci\n",
" 1 tests in __main__.filtroimpares\n",
" 1 tests in __main__.indicevspalabre\n",
" 1 tests in __main__.intersectab\n",
" 1 tests in __main__.multiplicaletra\n",
" 3 tests in __main__.panagrama\n",
" 1 tests in __main__.parorimpar\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",
"15 tests in 14 items.\n",
"15 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=15)"
]
},
"execution_count": 85,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def indicevspalabre():\n",
" \"\"\"indicevspalabre funcion imprimier tuplas con valores indice, palabra\n",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> indicevspalabre()\n",
" [[0, 'apple'], [1, 'banana'], [2, 'grapes'], [3, 'pear']]\n",
"\n",
" \"\"\"\n",
" my_list = ['apple', 'banana', 'grapes', 'pear']\n",
" print([[i,my_list[i]] for i in range(0,len(my_list))])\n",
"print(indicevspalabre.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.4.4 \n",
"Utilizando *filter* y una expresion *lambda* generar una lista que contenga todos los numeros impares de una lista de entrada **a**."
]
},
{
"cell_type": "code",
"execution_count": 86,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[5, 7, 97, 77, 23, 73, 61]\n",
"filtroimpares funcion filtrar numeros impares\n",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> filtroimpares()\n",
" [5, 7, 97, 77, 23, 73, 61]\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",
" filtroimpares()\n",
"Expecting:\n",
" [5, 7, 97, 77, 23, 73, 61]\n",
"ok\n",
"Trying:\n",
" indicevspalabre()\n",
"Expecting:\n",
" [[0, 'apple'], [1, 'banana'], [2, 'grapes'], [3, 'pear']]\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",
" parorimpar()\n",
"Expecting:\n",
" ['par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar']\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",
"12 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.fibonacci\n",
" 1 tests in __main__.filtroimpares\n",
" 1 tests in __main__.indicevspalabre\n",
" 1 tests in __main__.intersectab\n",
" 1 tests in __main__.multiplicaletra\n",
" 3 tests in __main__.panagrama\n",
" 1 tests in __main__.parorimpar\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",
"15 tests in 14 items.\n",
"15 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=15)"
]
},
"execution_count": 86,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def filtroimpares():\n",
" \"\"\"filtroimpares funcion filtrar numeros impares\n",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> filtroimpares()\n",
" [5, 7, 97, 77, 23, 73, 61]\n",
"\n",
" \"\"\"\n",
" a=[5, 7, 22, 97, 54, 62, 77, 23, 73, 61]\n",
" print(list(filter(lambda x:x%2 != 0, a)))\n",
"filtroimpares()\n",
"print(filtroimpares.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.4.5\n",
"Utilizado ***reduce*** y una expresion *lambda*, obten la suma de todos lo elementos en una lista."
]
},
{
"cell_type": "code",
"execution_count": 91,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"193\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",
"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",
" filtroimpares()\n",
"Expecting:\n",
" [5, 7, 97, 77, 23, 73, 61]\n",
"ok\n",
"Trying:\n",
" indicevspalabre()\n",
"Expecting:\n",
" [[0, 'apple'], [1, 'banana'], [2, 'grapes'], [3, 'pear']]\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",
" parorimpar()\n",
"Expecting:\n",
" ['par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar']\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",
" reducesuma()\n",
"Expecting:\n",
" 193\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",
"13 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.fibonacci\n",
" 1 tests in __main__.filtroimpares\n",
" 1 tests in __main__.indicevspalabre\n",
" 1 tests in __main__.intersectab\n",
" 1 tests in __main__.multiplicaletra\n",
" 3 tests in __main__.panagrama\n",
" 1 tests in __main__.parorimpar\n",
" 2 tests in __main__.perfect\n",
" 1 tests in __main__.printmayotto10\n",
" 1 tests in __main__.rang1to20\n",
" 1 tests in __main__.reducesuma\n",
" 1 tests in __main__.sumList\n",
"16 tests in 15 items.\n",
"16 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=16)"
]
},
"execution_count": 91,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from functools import reduce\n",
"def reducesuma():\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"
]
},
{
"cell_type": "code",
"execution_count": 92,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[35, 45, 55, 65]\n",
"mapsuma funcion obtener suma de numeros\n",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> reducesuma()\n",
" 193\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",
" filtroimpares()\n",
"Expecting:\n",
" [5, 7, 97, 77, 23, 73, 61]\n",
"ok\n",
"Trying:\n",
" indicevspalabre()\n",
"Expecting:\n",
" [[0, 'apple'], [1, 'banana'], [2, 'grapes'], [3, 'pear']]\n",
"ok\n",
"Trying:\n",
" intersectab()\n",
"Expecting:\n",
" [1, 2, 3, 5, 8, 13]\n",
"ok\n",
"Trying:\n",
" reducesuma()\n",
"Expecting:\n",
" 193\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",
" parorimpar()\n",
"Expecting:\n",
" ['par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar']\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",
" reducesuma()\n",
"Expecting:\n",
" 193\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",
"14 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.fibonacci\n",
" 1 tests in __main__.filtroimpares\n",
" 1 tests in __main__.indicevspalabre\n",
" 1 tests in __main__.intersectab\n",
" 1 tests in __main__.mapsuma\n",
" 1 tests in __main__.multiplicaletra\n",
" 3 tests in __main__.panagrama\n",
" 1 tests in __main__.parorimpar\n",
" 2 tests in __main__.perfect\n",
" 1 tests in __main__.printmayotto10\n",
" 1 tests in __main__.rang1to20\n",
" 1 tests in __main__.reducesuma\n",
" 1 tests in __main__.sumList\n",
"17 tests in 16 items.\n",
"17 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=17)"
]
},
"execution_count": 92,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def mapsuma(): \n",
" \"\"\"mapsuma funcion obtener suma de numeros\n",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> reducesuma()\n",
" 193\n",
"\n",
" \"\"\"\n",
" a = [5, 10, 15, 20]\n",
"\n",
" b = [30, 35, 40, 45]\n",
"\n",
" print(list(map(lambda x,y: x+y, a,b)))\n",
"mapsuma()\n",
"print(mapsuma.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.6.8 \n",
"Escribir un generador en forma de funcion recursiva que entregue la lista de todas las permitaciones de los elementos en una lista."
]
},
{
"cell_type": "code",
"execution_count": 95,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[['a', 'a', 'a'], ['a', 'a', 'b'], ['a', 'a', 'c'], ['a', 'b', 'a'], ['a', 'b', 'b'], ['a', 'b', 'c'], ['a', 'c', 'a'], ['a', 'c', 'b'], ['a', 'c', 'c'], ['b', 'a', 'a'], ['b', 'a', 'b'], ['b', 'a', 'c'], ['b', 'b', 'a'], ['b', 'b', 'b'], ['b', 'b', 'c'], ['b', 'c', 'a'], ['b', 'c', 'b'], ['b', 'c', 'c'], ['c', 'a', 'a'], ['c', 'a', 'b'], ['c', 'a', 'c'], ['c', 'b', 'a'], ['c', 'b', 'b'], ['c', 'b', 'c'], ['c', 'c', 'a'], ['c', 'c', 'b'], ['c', 'c', 'c']]\n",
"permutacion funcion para obtener permutacion de un arreglo\n",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> permutacion()\n",
" [['a', 'a', 'a'], ['a', 'a', 'b'], ['a', 'a', 'c'], ['a', 'b', 'a'], ['a', 'b', 'b'], ['a', 'b', 'c'], ['a', 'c', 'a'], ['a', 'c', 'b'], ['a', 'c', 'c'], ['b', 'a', 'a'], ['b', 'a', 'b'], ['b', 'a', 'c'], ['b', 'b', 'a'], ['b', 'b', 'b'], ['b', 'b', 'c'], ['b', 'c', 'a'], ['b', 'c', 'b'], ['b', 'c', 'c'], ['c', 'a', 'a'], ['c', 'a', 'b'], ['c', 'a', 'c'], ['c', 'b', 'a'], ['c', 'b', 'b'], ['c', 'b', 'c'], ['c', 'c', 'a'], ['c', 'c', 'b'], ['c', 'c', 'c']]\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",
" filtroimpares()\n",
"Expecting:\n",
" [5, 7, 97, 77, 23, 73, 61]\n",
"ok\n",
"Trying:\n",
" indicevspalabre()\n",
"Expecting:\n",
" [[0, 'apple'], [1, 'banana'], [2, 'grapes'], [3, 'pear']]\n",
"ok\n",
"Trying:\n",
" intersectab()\n",
"Expecting:\n",
" [1, 2, 3, 5, 8, 13]\n",
"ok\n",
"Trying:\n",
" reducesuma()\n",
"Expecting:\n",
" 193\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",
" parorimpar()\n",
"Expecting:\n",
" ['par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar']\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",
" permutacion()\n",
"Expecting:\n",
" [['a', 'a', 'a'], ['a', 'a', 'b'], ['a', 'a', 'c'], ['a', 'b', 'a'], ['a', 'b', 'b'], ['a', 'b', 'c'], ['a', 'c', 'a'], ['a', 'c', 'b'], ['a', 'c', 'c'], ['b', 'a', 'a'], ['b', 'a', 'b'], ['b', 'a', 'c'], ['b', 'b', 'a'], ['b', 'b', 'b'], ['b', 'b', 'c'], ['b', 'c', 'a'], ['b', 'c', 'b'], ['b', 'c', 'c'], ['c', 'a', 'a'], ['c', 'a', 'b'], ['c', 'a', 'c'], ['c', 'b', 'a'], ['c', 'b', 'b'], ['c', 'b', 'c'], ['c', 'c', 'a'], ['c', 'c', 'b'], ['c', 'c', 'c']]\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",
" reducesuma()\n",
"Expecting:\n",
" 193\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",
"15 items passed all tests:\n",
" 1 tests in __main__.example_generator\n",
" 1 tests in __main__.fibonacci\n",
" 1 tests in __main__.filtroimpares\n",
" 1 tests in __main__.indicevspalabre\n",
" 1 tests in __main__.intersectab\n",
" 1 tests in __main__.mapsuma\n",
" 1 tests in __main__.multiplicaletra\n",
" 3 tests in __main__.panagrama\n",
" 1 tests in __main__.parorimpar\n",
" 2 tests in __main__.perfect\n",
" 1 tests in __main__.permutacion\n",
" 1 tests in __main__.printmayotto10\n",
" 1 tests in __main__.rang1to20\n",
" 1 tests in __main__.reducesuma\n",
" 1 tests in __main__.sumList\n",
"18 tests in 17 items.\n",
"18 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=18)"
]
},
"execution_count": 95,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def permutacion():\n",
" \"\"\"permutacion funcion para obtener permutacion de un arreglo\n",
"\n",
" Args:\n",
" \n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> permutacion()\n",
" [['a', 'a', 'a'], ['a', 'a', 'b'], ['a', 'a', 'c'], ['a', 'b', 'a'], ['a', 'b', 'b'], ['a', 'b', 'c'], ['a', 'c', 'a'], ['a', 'c', 'b'], ['a', 'c', 'c'], ['b', 'a', 'a'], ['b', 'a', 'b'], ['b', 'a', 'c'], ['b', 'b', 'a'], ['b', 'b', 'b'], ['b', 'b', 'c'], ['b', 'c', 'a'], ['b', 'c', 'b'], ['b', 'c', 'c'], ['c', 'a', 'a'], ['c', 'a', 'b'], ['c', 'a', 'c'], ['c', 'b', 'a'], ['c', 'b', 'b'], ['c', 'b', 'c'], ['c', 'c', 'a'], ['c', 'c', 'b'], ['c', 'c', 'c']]\n",
"\n",
" \"\"\"\n",
" a = [\"a\", \"b\", \"c\"]\n",
" print([[i,n,j] for i in a for n in a for j in a])\n",
"permutacion()\n",
"print(permutacion.__doc__)\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
......
...@@ -475,7 +475,7 @@ ...@@ -475,7 +475,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.5.3" "version": "3.6.7"
} }
}, },
"nbformat": 4, "nbformat": 4,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment