Commit 8eb93479 authored by geobumac's avatar geobumac

merge

parent 242885bc
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"![Logo](https://www.python.org/static/img/python-logo.png)\n",
"\n",
"<h1><center>Tópicos Avanzados de Programación</center></h1>\n",
"\n",
"# 1. Python \n",
"## 1.1 Introduccion"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Python fue creado en 1991 por Guido van Rossum, con la filosofia de crear codigo legible, por lo que la identacion es obligatoria. Su desarrollo esta administrado por **Python Software Foundation** (python.org).\n",
"\n",
"\n",
"Caracteristicas:\n",
"* Alto Nivel\n",
"* Interpretado (Se ejecuta sin compilación previa)\n",
"* Tipificación Dinamica (Se realiza durante en tiempo de ejecución)\n",
"* Multiparadigma\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/).*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1.2 Tipos de Datos\n",
"#### Numericos\n",
"* int\n",
"* float\n",
"* complex"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"int"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type (1)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"float"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type (1.)"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"complex"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type ((1+2j))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Secuencias\n",
"* list\n",
"* tuple \n",
"* range\n"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"list"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type([1,2,3])"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tuple"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type((1,2))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"range"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(range(1))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Texto\n",
"* str\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"str"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(\"Hola\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Mapeo\n",
"* dict"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"dict"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type({\"mensaje\":\"hola mundo\"})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Conjuntos\n",
"* set\n",
"* frozenset"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1, 2, 3, 5, 6}"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"set([1,2,3,1,5,2,6])\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1.2 Operadores\n",
"### Comparación \n",
"| Simbolo | Operador |\n",
"|---------|----------|\n",
"| < | menor que |\n",
"| <= | Menor o igual que |\n",
"| >\t| Mayor que |Mayor o igual que | \n",
"| == | Igual |\n",
"| != | Desigual |\n",
"| is | Identidad de Objetos |\n",
"| is not | Identidad Negada de Objetos|\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"6 < 9"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"6 > 9"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"6 == 9"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"6 != 9"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"False"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"6 is float(6)\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"6 is int(6)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Operaciones\n",
"| Operacion\t| Result |\n",
"|-----------|--------|\n",
"| x + y\t| Suma |\n",
"| x - y\t| Resta |\n",
"| x * y\t| Multiplicacion |\n",
"| x / y\t| Division |\n",
"| x // y | Division (Parte entera) |\n",
"| x % y\t| Modulo|\n",
"| x ** y | x a la potencia y |\n",
"\n",
"### Operador a nivel de bits\n",
"| Operation | Result |\n",
"|-----------|--------|\n",
"| x &#124; y\t| x OR y |\n",
"| x ^ y\t| x XOR y |\n",
"| x & y\t| x AND y |\n",
"| x << n | x desplazado n bits a la izquierda |\n",
"| x >> n | x desplazado n bits a la derecha |\n",
"| ~x | x Negado |\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1.3 Control de Flujo\n",
"Las estructuruas de control de flijo permiten modificar el flujo de ejecución de las instrucciones de un programa mediante el valor binario de una condicion. Python cuenta con las siguientes estructuras de control:\n",
"\n",
"* if, elif\n",
"* else\n",
"* for\n",
"* while\n",
"* continue\n",
"* break\n",
"* pass\n",
"* try, exeption\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n"
]
}
],
"source": [
"if True:\n",
" print(\"True\")\n",
"else:\n",
" print(\"False\")"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n"
]
}
],
"source": [
"for i in range(5):\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Vacio\n"
]
}
],
"source": [
"for i in range(0):\n",
" print(i)\n",
"else:\n",
" print(\"Vacio\")"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"3\n",
"4\n",
"5\n"
]
}
],
"source": [
"i = 0\n",
"while i < 5:\n",
" i+=1\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"3\n",
"4\n"
]
}
],
"source": [
"for i in range(5):\n",
" if(i==2):\n",
" continue\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n"
]
}
],
"source": [
"for i in range(5):\n",
" if(i==2):\n",
" break\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n"
]
}
],
"source": [
"for i in range(5):\n",
" if(i==2):\n",
" pass\n",
" print(i)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.5\n",
"0.6666666666666666\n",
"1.0\n",
"2.0\n",
"ERROR\n"
]
}
],
"source": [
"for i in reversed(range(5)):\n",
" try:\n",
" print(2/i)\n",
" except:\n",
" print(\"ERROR\")\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Todos los objetos de python son considerados verdaders (True) exepto los siguintes:\n",
"* Constantes definidas como: None o False.\n",
"* Cero de cualquier tipo numerico: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)\n",
"* Secuencias y colecciones vacias: '', (), [], {}, set(), range(0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1.4 Funciones\n",
"Una función es un conjunto de setencias que pueden ser invocadas varias veces durante la ejecución de un programa. Permiten minimizar el codigo, amuentar su legibilidad y permiten reutilizar código. En python las funciones son definida por la palabra reservada **def**. \n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello World\n"
]
}
],
"source": [
"def HelloWorld():\n",
" print(\"Hello World\")\n",
"\n",
"HelloWorld()\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.4.1 Parametros\n",
"La funciones pueden aceptar arguentos de entrada y devoler resultados."
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello Mario\n"
]
}
],
"source": [
"def Hello(name):\n",
" print(\"Hello \"+name)\n",
"Hello(\"Mario\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.4.2 Parametros Opcionales\n",
"Las funciones tampien pueden aceptar parametros opcionales, los cuales toman en valor indicado por defecto si no son pasados a la funcion."
]
},
{
"cell_type": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hello Mario!!!\n",
"Hello Mario?\n",
"Hello Mario!\n",
"Hello Alex!!\n"
]
},
{
"data": {
"text/plain": [
"[1]"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def Hello(name, x=\"!!!\"):\n",
" print(\"Hello \" + name + x)\n",
"Hello(\"Mario\")\n",
"Hello(\"Mario\",\"?\")\n",
"Hello(x=\"!\", name=\"Mario\")\n",
"p = {\"name\":\"Alex\", \"x\":\"!!\"}\n",
"Hello(**p)\n",
"[1,]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.4.3 Desempaquetado"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 1 2\n",
"0 4 5\n",
"[6, 7, 8] 1 2\n",
"6 7 8\n",
"{'a': 9, 'b': 10, 'c': 11} 1 2\n",
"9 10 11\n"
]
}
],
"source": [
"def unpack(a,b=1,c=2):\n",
" print(a,b,c)\n",
"\n",
"l = [6,7,8] \n",
"d = {\"a\":9,\"b\":10,\"c\":11} \n",
"unpack(0)\n",
"unpack(0, 4, 5)\n",
"unpack(l)\n",
"unpack(*l)\n",
"unpack(d)\n",
"unpack(**d)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 1.5 Ejercicios\n",
"### 1.5.1 Imprimir todos los numeros pares entre 0 y 20 usando *for* o *while*."
<<<<<<< HEAD
]
},
{
"cell_type": "code",
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"6\n",
"7\n",
"8\n",
"9\n",
"10\n",
"11\n",
"12\n",
"13\n",
"14\n",
"15\n",
"16\n",
"17\n",
"18\n",
"19\n",
"20\n"
]
}
],
"source": [
"for i in range(21):\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.5.2 Imprimir todos los numeros mayores a 10 de la lista A"
]
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"13\n",
"21\n",
"34\n",
"55\n",
"89\n",
"[13, 21, 34, 55, 89]\n"
]
}
],
"source": [
"A = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]\n",
"i=0\n",
"for i in A:\n",
" if i > 10:\n",
" print(i)\n",
"print([i for i in A if i>10])"
=======
>>>>>>> master
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
<<<<<<< HEAD
"### 1.5.3 Dadas dos listas A y B, obten una lista con sus elementos comunes (A∩B)."
=======
"### 1.5.2 Imprimir todos los numeros mayores a 10 de la lista A"
>>>>>>> master
]
},
{
"cell_type": "code",
<<<<<<< HEAD
"execution_count": 30,
=======
"execution_count": 28,
>>>>>>> master
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{1, 2, 3, 5, 8, 13}\n"
]
}
],
"source": [
"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(set(a) & set(b))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
<<<<<<< HEAD
"### 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."
=======
"### 1.5.3 Dadas dos listas A y B, obten una lista con sus elementos comunes (A∩B)."
>>>>>>> master
]
},
{
"cell_type": "code",
<<<<<<< HEAD
"execution_count": 31,
=======
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
"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"
]
},
{
"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": 30,
"metadata": {},
"outputs": [],
"source": [
"def fibonacci(n):\n",
" pass"
]
},
{
"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": 31,
"metadata": {},
"outputs": [],
"source": [
"a = [8, 2, 3, 0, 7]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.5.6 Escribe una funcion que tome una lista y regrese los elementos unicos en la lista.\n"
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
"a = [1,2,2,3,3,3,3,4,5,5]"
]
},
{
"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": 34,
>>>>>>> master
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
<<<<<<< HEAD
"cuantos numeros de la secuancia Fibonacci quiere calcular:5\n",
"0\n",
"1\n",
"1\n",
"2\n",
"3\n",
"5\n"
=======
"Escribe un nuemero:8\n"
>>>>>>> master
]
}
],
"source": [
<<<<<<< HEAD
"def fibonacci(n):\n",
" if n == 0: \n",
" return 0\n",
" elif n == 1:\n",
" return 1\n",
" else: \n",
" return fibonacci(n-1)+fibonacci(n-2)\n",
"\n",
"numero = int(input(\"cuantos numeros de la secuancia Fibonacci quiere calcular:\"))\n",
"for i in range(numero+1):\n",
" print(fibonacci(i))"
=======
"def perfect(x):\n",
" pass\n",
"numero = input(\"Escribe un nuemero:\")\n",
"perfect(numero)"
>>>>>>> master
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
<<<<<<< HEAD
"### 1.5.5 Escribe una funcion que sume todos los numeros en una lista usando for."
]
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"20\n"
]
}
],
"source": [
"a = [8, 2, 3, 0, 7]\n",
"\n",
"def sumList(lista):\n",
" print(sum(lista))\n",
" \n",
"sumList(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.5.6 Escribe una funcion que tome una lista y regrese los elementos unicos en la lista.\n"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5]\n"
]
}
],
"source": [
"a = [1,2,2,3,3,3,3,4,5,5]\n",
"def printUnique(a):\n",
" print(list(set(a)))\n",
" \n",
"printUnique(a)"
]
},
{
"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"
=======
"### 1.5.8 Escribe una funcion que imprima las prieras *n* filas del triangulo de Pascal.\n",
"[Wolfram](http://mathworld.wolfram.com/PascalsTriangle.html):\n",
"El triángulo de Pascal es un triángulo numérico con números dispuestos en filas escalonadas de manera tal que:\n",
"$a_{nr}=\\frac{n!}{r!(n-r)!}=\\binom{n}{r}$\n"
>>>>>>> master
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Escribe un nuemero:28\n",
"28 es un numero perfecto\n",
"28 es un numero perfecto\n"
]
}
],
"source": [
"from functools import reduce\n",
"def perfect(x):\n",
" suma = 0\n",
" for i in range(1,x):\n",
" if x % i == 0:\n",
" suma += i\n",
" return suma\n",
"\n",
"def perfect2(x):\n",
" a = range(1,x)\n",
" suma = reduce((lambda x, y: x + y), list(filter(lambda i: x%i == 0, a)))\n",
" return suma\n",
" \n",
"numero = int(input(\"Escribe un nuemero:\"))\n",
"sum1 = perfect(numero)\n",
"sum2 = perfect2(numero)\n",
"\n",
"if numero == sum1:\n",
" print(\"%s es un numero perfecto\" % numero)\n",
"else:\n",
" print(\"%s no es un numero perfecto\" % numero)\n",
"if numero == sum2:\n",
" print(\"%s es un numero perfecto\" % numero)\n",
"else:\n",
" print(\"%s no es un numero perfecto\" % numero)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 1.5.8 Escribe una funcion que imprima las prieras *n* filas del triangulo de Pascal.\n",
"[Wolfram](http://mathworld.wolfram.com/PascalsTriangle.html):\n",
"El triángulo de Pascal es un triángulo numérico con números dispuestos en filas escalonadas de manera tal que:\n",
"$a_{nr}=\\frac{n!}{r!(n-r)!}=\\binom{n}{r}$\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def pascal(n):\n",
<<<<<<< HEAD
" lista = [[1],[1,1]]\n",
" print([1])\n",
" print([1,1])\n",
" for i in range(1, n):\n",
" linea = [1]\n",
" for j in range(0,len(lista[i])-1):\n",
" linea.extend([ lista[i][j] + lista[i][j+1] ])\n",
" linea.append(1)\n",
" lista.append(linea)\n",
" print(linea)\n",
"numero = int(input(\"Indica el numero de filas:\"))\n",
"pascal(numero)"
=======
" pass\n",
"numero = input(\"Indica el numero de filas:\")\n",
"pascal(numero)\n"
>>>>>>> master
]
},
{
"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. "
]
},
{
<<<<<<< HEAD
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def panagrama(phrase):\n",
" alphabet = \"abcdefghijklmnopqrstuvwxyz\"\n",
" return not (set(alphabet) - set(phrase))\n",
"print(panagrama(\"zbcdefghijkl onpqrstuvwxya\"))\n",
"print(panagrama(\"abcdefghijkl onpqrstuvwxyz\"))\n",
"print(panagrama(\"abcdefghijklmnopqrstuvwxyz\"))"
]
},
{
=======
>>>>>>> master
"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"
<<<<<<< HEAD
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"for i in range(1, 10):\n",
" aux = \"\"\n",
" for j in range(1, i + 1):\n",
" aux += str(i)\n",
" print(aux);"
=======
>>>>>>> master
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 2. Python\n",
"## 2.1 Comprensiónes\n",
"Las comprensiones de python proveen de una forma consisa de crear listas, diccionarios y conjuntos. Su nombre proviene de teria de conjuntos en donde la *notación contructiva de conjuntos* o comprensión se define como:\n",
"\n",
"\n",
"[Wikipedia](https://en.wikipedia.org/wiki/Set-builder_notation): Definir conjuntos por propiedades también se conoce como ***comprensión de conjuntos***, abstracción de conjuntos o como definición de la intención de un conjunto.\n",
"\n",
"En python la estructura de una comprensión es la siguiente:![img](https://python-3-patterns-idioms-test.readthedocs.io/en/latest/_images/listComprehensions.gif)"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 2, 4, 6, 8]"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"old_list = [1,2,2,3,4,5,6,7,7,8,9]\n",
"new_list = []\n",
"for i in old_list:\n",
" if i%2==0:\n",
" new_list.append(i)\n",
"new_list"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[2, 2, 4, 6, 8]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Lista\n",
"new_list=[i for i in old_list if i%2==0]\n",
"new_list"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{0: 1,\n",
" 1: 1,\n",
" 2: 4,\n",
" 3: 27,\n",
" 4: 256,\n",
" 5: 3125,\n",
" 6: 46656,\n",
" 7: 823543,\n",
" 8: 16777216,\n",
" 9: 387420489}"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Diccionario\n",
"{ i:i**i for i in range(10)}"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[1, 0, 0], [0, 1, 0], [0, 0, 1]]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Comprensión anidada\n",
"[ [ 1 if item_idx == row_idx else 0 for item_idx in range(0, 3) ] for row_idx in range(0, 3) ]"
]
},
{
"cell_type": "code",
"execution_count": 34,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{3, 6, 9}"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Conjunto\n",
"old_list = [1,2,2,3,4,5,6,7,7,8,9]\n",
"new_set={i for i in old_list if i%3 == 0}\n",
"new_set"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2.2 Expresiones lambda $\\lambda$\n",
"Una función anónima o **expresión lambda** es una subrutina definida que no está enlazada a un identificador. Las funciones lambda generalmente son Argumentos que son pasados a otras funciónes de orden superior o Usadas para construir el resultado de una función de orden superior que necesita retornar una función [[Wikiedia](https://en.wikipedia.org/wiki/Anonymous_function)].\n",
"\n",
"En Python las expresiones lambda no pueden utilizar ciclos ni utilizar la plabra reservada **return**, su sintaxis es:```lambda <parametros>:<expresion>```\n",
"\n",
"*Nota: **filter** regresa una lista de elementos para los cuales una funcion regresa **True**; **map** aplica una funcion a todos los ementos de una lista.*\n",
"\n",
"\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"function"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f = lambda x: x * x\n",
"type(f)"
]
},
{
"cell_type": "code",
"execution_count": 41,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[27, 46656, 387420489]"
]
},
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"old_list = [1,2,2,3,4,5,6,7,7,8,9]\n",
"list(map(lambda i: i**i, filter(lambda i: i%3==0, old_list)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(13, -3), (4, 1), (1, 2), (9, 10)]"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = [(1, 2), (4, 1), (9, 10), (13, -3)]\n",
"a.sort(key=lambda x: x[1])\n",
"a"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[102.56, 97.7, 99.14, 100.03999999999999]\n"
]
}
],
"source": [
"Celsius = [39.2, 36.5, 37.3, 37.8]\n",
"Fahrenheit = map(lambda x: (float(9)/5)*x + 32, Celsius)\n",
"print(list(Fahrenheit))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2.3 Generadores: Funciones y Expresiones\n",
"Los Generadores son funciones o expresiones que regesan un valor iterador en lugar de un valor, las para que una funcion regrese un generador en lugar de un valor se utiliza la palabra reservada **yield** en lugar de **return**."
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"499999500000\n"
]
}
],
"source": [
"def firstn(n):\n",
" num = 0\n",
" while num < n:\n",
" yield num\n",
" num += 1\n",
"\n",
"sum_of_first_n = sum(firstn(1000000))\n",
"print(sum_of_first_n)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tambien es posible construir generadores utilizando expresiones utilizando parentesis en lugar de corchetes. Esto resulta util cuando utilizar una expresion generaria una lista muy grande que ocuparia mucha memoria."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]\n",
"0\n",
"2\n",
"4\n",
"[6, 8, 10, 12, 14, 16, 18]\n"
]
}
],
"source": [
"doublesC = [2 * n for n in range(10)]\n",
"print(doublesC)\n",
"doublesG = (2 * n for n in range(10))\n",
"print(next(doublesG))\n",
"print(next(doublesG))\n",
"print(next(doublesG))\n",
"print(list(doublesG))"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Cuatos Fibonacci?: 6\n",
"0\n",
"1\n",
"1\n",
"2\n",
"3\n",
"5\n"
]
}
],
"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",
"for n in fib(a):\n",
" print(n)"
]
},
{
"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",
"metadata": {},
"source": [
"## 2.4 Ejercicios\n",
"### 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": 67,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"['par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar', 'par', 'impar']\n",
"pares [0, 2, 4, 6, 8]\n",
"impares [1, 3, 5, 7, 9]\n"
]
}
],
"source": [
"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)"
]
},
{
"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": 69,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[0, 'apple'], [1, 'banana'], [2, 'grapes'], [3, 'pear']]"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_list = ['apple', 'banana', 'grapes', 'pear']\n",
"[[i,my_list[i]] for i in range(0,len(my_list))]\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.4.3\n",
"Genera una lista que contenga una tuplas con todos los pares posibles de elementos entre las dos listas."
]
},
{
"cell_type": "code",
"execution_count": 72,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[0, 'a'],\n",
" [0, 'b'],\n",
" [0, 'c'],\n",
" [0, 'd'],\n",
" [0, 'e'],\n",
" [0, 'f'],\n",
" [1, 'a'],\n",
" [1, 'b'],\n",
" [1, 'c'],\n",
" [1, 'd'],\n",
" [1, 'e'],\n",
" [1, 'f'],\n",
" [2, 'a'],\n",
" [2, 'b'],\n",
" [2, 'c'],\n",
" [2, 'd'],\n",
" [2, 'e'],\n",
" [2, 'f'],\n",
" [3, 'a'],\n",
" [3, 'b'],\n",
" [3, 'c'],\n",
" [3, 'd'],\n",
" [3, 'e'],\n",
" [3, 'f'],\n",
" [4, 'a'],\n",
" [4, 'b'],\n",
" [4, 'c'],\n",
" [4, 'd'],\n",
" [4, 'e'],\n",
" [4, 'f'],\n",
" [5, 'a'],\n",
" [5, 'b'],\n",
" [5, 'c'],\n",
" [5, 'd'],\n",
" [5, 'e'],\n",
" [5, 'f'],\n",
" [6, 'a'],\n",
" [6, 'b'],\n",
" [6, 'c'],\n",
" [6, 'd'],\n",
" [6, 'e'],\n",
" [6, 'f']]"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = [0,1,2,3,4,5,6]\n",
"b = [\"a\",\"b\",\"c\",\"d\",\"e\",\"f\"]\n",
"[[i,n] for i in a for n in b]"
]
},
{
"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": 74,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[5, 7, 97, 77, 23, 73, 61]"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a=[5, 7, 22, 97, 54, 62, 77, 23, 73, 61]\n",
"list(filter(lambda x:x%2 != 0, a))"
]
},
{
"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": 76,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"193"
]
},
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from functools import reduce\n",
"a = [5, 8, 10, 20, 50, 100] \n",
"reduce((lambda x, y: x + y), a)"
]
},
{
"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": 78,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[35, 45, 55, 65]"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = [5, 10, 15, 20]\n",
" \n",
"b = [30, 35, 40, 45]\n",
"\n",
"list(map(lambda x,y: x+y, a,b))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.6.7\n",
"Escribir un generador en forma de expresion que obtenga las primeras 10 ternas pitagoricas.\n",
"\n",
"[Wikipedia](https://es.wikipedia.org/wiki/Terna_pitag%C3%B3rica): Una terna pitagórica es un conjunto ordenado de tres números enteros positivos a, b, c, y son solución de la ecuación diofantina cuadrática $a^{2}+b^{2}=c^{2}$.\n"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n"
]
}
],
"source": [
"def pitagoricas(n):\n",
" num = 0\n",
" while num <= n:\n",
" yield []\n",
" num += 1\n",
"\n",
"pit = pitagoricas(10)\n",
"print(next(pit))"
]
},
{
"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": 67,
"metadata": {},
"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": [
"a = [\"a\", \"b\", \"c\"]\n",
"[[i,n] for i in a for n in a]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2.5 Modulos y Paquetes\n",
"En Python, cada uno de nuestros archivos .py se denominan módulos. Estos módulos, a la vez, pueden formar parte de paquetes. Un paquete, es una carpeta que contiene archivos .py. Para que una carpeta pueda ser considerada un paquete, debe contener un archivo de inicio llamado ``__init__.py``. Este archivo, no necesita contener ninguna instrucción. De hecho, puede estar vacío.\n",
"\n",
"\n",
"``\n",
"└── miModulo\n",
" ├── __init__.py \n",
" └── helloWOrld.py \n",
"``"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from miModulo import helloWorld\n",
"helloWorld.hello()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2.6 Documentacion (docstring)\n",
"\n",
"Python **Docstring** es el texto de documentación que puede aprecer en la definición de una clase, módulo, función o método, y se escribe como la primera declaración. Se puede acceder a las cadenas de documenacion desde el atributo doc para cualquiera de los objetos de Python y también con la función incorporada **help()**."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"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": [
"def 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",
" \"\"\"\n",
" for i in range(n):\n",
" yield i\n",
"\n",
"type(example_generator)\n",
"print(example_generator.__doc__)\n",
"help(example_generator)\n",
"\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.6.1 pydoc\n",
"En la linea de comando el modulo **pydoc** permite general la documentacion de los modulo en formato html:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%%bash\n",
"cd miModulo\n",
"pydoc -w helloWorld.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from IPython.display import HTML\n",
"HTML(filename=\"miModulo/helloWorld.html\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2.7 Casos de Prueba (doctest)\n",
"\n",
"doctest prueba el código fuente ejecutando ejemplos incrustados en la documentación y verificando que producen los resultados esperados. Funciona al analizar el texto de ayuda para encontrar ejemplos, ejecutarlos y luego comparar el texto de salida con el valor esperado.\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Trying:\n",
" multiply(4, 3)\n",
"Expecting:\n",
" 12\n",
"ok\n",
"Trying:\n",
" multiply('a', 3)\n",
"Expecting:\n",
" 'aaa'\n",
"ok\n",
"1 items had no tests:\n",
" __main__\n",
"1 items passed all tests:\n",
" 2 tests in __main__.multiply\n",
"2 tests in 2 items.\n",
"2 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=2)"
]
},
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def multiply(a, b):\n",
" '''\n",
" >>> multiply(4, 3)\n",
" 12\n",
" >>> multiply('a', 3)\n",
" 'aaa'\n",
" '''\n",
" return a * b\n",
"import doctest\n",
"\n",
"doctest.testmod(verbose=True)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2.8 Ejercicios\n",
"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",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.7"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
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