Ejercicios con Documentacion y Casos de Prueba

parent 29f7a582
{
"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",
"k = [ [ 1 if item_idx == row_idx else 0 for item_idx in range(0, 3) ] for row_idx in range(0, 3) ]\n",
"k"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{1, 2, 3, 4, 5, 6, 7, 8, 9}"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Conjunto\n",
"new_set={i for i in old_list}\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": 6,
"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": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[4, 4, 256, 46656, 16777216]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"list(map(lambda i: i**i, filter(lambda i: i%2==0, old_list)))"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
"a = [(1, 2), (4, 1), (9, 10), (13, -3)]\n",
"a.sort(key=lambda x: x[1])"
]
},
{
"cell_type": "code",
"execution_count": 9,
"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": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"15\n"
]
}
],
"source": [
"def firstn(n):\n",
" num = 0\n",
" while num < n:\n",
" yield num\n",
" print(num)\n",
" num += 1\n",
"\n",
"sum_of_first_n = sum(firstn(6))\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": 11,
"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": "markdown",
"metadata": {},
"source": [
"## 2.4 Ejercicios\n",
"### 2.4.1\n",
"Dada una lista de enteros **a**, utilizando 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": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(0, 'par'),\n",
" (1, 'impar'),\n",
" (2, 'par'),\n",
" (3, 'impar'),\n",
" (4, 'par'),\n",
" (5, 'impar'),\n",
" (6, 'par'),\n",
" (7, 'impar'),\n",
" (8, 'par'),\n",
" (9, 'impar')]"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
"\n",
"def ParImpar(a):\n",
" '''La función imprime una nueva lista que contenga \"par\" o \"impar\" en cada uno de sus elementos, \n",
" dependiendo del valor del elemento en la lista orginal.\n",
" \n",
" Args:\n",
" a (list): Lista que contenga los enteros a evaluar\n",
" \n",
" Ejemplo:\n",
" >>> ParImpar([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n",
" [(0, 'par'), (1, 'impar'), (2, 'par'), (3, 'impar'), (4, 'par'), (5, 'impar'), (6, 'par'), (7, 'impar'), (8, 'par'), (9, 'impar')]\n",
" '''\n",
"\n",
" return [ (x, 'par') if x%2==0 else (x, 'impar') for x in a ]\n",
"\n",
"[ (x, 'par') if x%2==0 else (x, 'impar') for x in a ]\n",
"\n",
"\n",
"\n",
"ParImpar(a)"
]
},
{
"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": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(0, 'apple'), (1, 'banana'), (2, 'grapes'), (3, 'pear')]"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_list = ['apple', 'banana', 'grapes', 'pear']\n",
"\n",
"def muestraIndice(my_list):\n",
" '''La función imprime una lista de tuplas, en donde cada tupla \n",
" contiene el indice y el valor de elemento de la lista orginal.\n",
" \n",
" Args:\n",
" a(list): Lista que contenga los valores a evaluar\n",
" \n",
" Ejemplo:\n",
" >>> muestraIndice(['apple', 'banana', 'grapes', 'pear'])\n",
" [(0, 'apple'), (1, 'banana'), (2, 'grapes'), (3, 'pear')]\n",
" \n",
" '''\n",
" return [ (k, my_list[k]) for k in range(len(my_list)) ]\n",
"\n",
"muestraIndice(my_list)"
]
},
{
"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": 14,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/plain": [
"[(0, 'a'),\n",
" (0, 'b'),\n",
" (0, 'c'),\n",
" (0, 'd'),\n",
" (1, 'a'),\n",
" (1, 'b'),\n",
" (1, 'c'),\n",
" (1, 'd'),\n",
" (2, 'a'),\n",
" (2, 'b'),\n",
" (2, 'c'),\n",
" (2, 'd'),\n",
" (3, 'a'),\n",
" (3, 'b'),\n",
" (3, 'c'),\n",
" (3, 'd'),\n",
" (4, 'a'),\n",
" (4, 'b'),\n",
" (4, 'c'),\n",
" (4, 'd'),\n",
" (5, 'a'),\n",
" (5, 'b'),\n",
" (5, 'c'),\n",
" (5, 'd')]"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = [0,1,2,3,4,5]\n",
"b = [\"a\",\"b\",\"c\",\"d\"]\n",
"\n",
"def paresPosibles(a, b):\n",
" '''La funcion Genera una lista que contenga una tuplas \n",
" con todos los pares posibles de elementos entre las dos listas.\n",
" \n",
" Args:\n",
" a, b (list): Listas que contengan los valores a evaluar\n",
" \n",
" Ejemplo:\n",
" >>> a = [0,1,2,3,4,5]\n",
" >>> b = [\"a\",\"b\",\"c\",\"d\"]\n",
" >>> paresPosibles(a, b)\n",
" [(0, 'a'), (0, 'b'), (0, 'c'), (0, 'd'), (1, 'a'), (1, 'b'), (1, 'c'), (1, 'd'), (2, 'a'), (2, 'b'), (2, 'c'), (2, 'd'), (3, 'a'), (3, 'b'), (3, 'c'), (3, 'd'), (4, 'a'), (4, 'b'), (4, 'c'), (4, 'd'), (5, 'a'), (5, 'b'), (5, 'c'), (5, 'd')]\n",
" '''\n",
" return [ (num, char) for num in a for char in b ]\n",
"\n",
"paresPosibles(a, 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": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[5, 7, 97, 77, 23, 73, 61]"
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a=[5, 7, 22, 97, 54, 62, 77, 23, 73, 61]\n",
"\n",
"def impares(a):\n",
" '''\n",
" La función imprime una lista que contenga todos los numeros impares de una lista de entrada **a**,\n",
" Utilizando *filter* y una expresion *lambda*. Los elementos deben ser valor entero, \n",
" \n",
" Args:\n",
" a (list): Lista que contenga valores enteros que seran evaluados\n",
" \n",
" Ejemplo:\n",
" \n",
" >>> a = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]\n",
" >>> impares(a)\n",
" [5, 7, 97, 77, 23, 73, 61]\n",
" \n",
" '''\n",
" return list(filter(lambda y: y%2==1, a))\n",
"\n",
"impares(a)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2.4.5\n",
"\n",
"Utilizado ***reduce*** y una expresion *lambda*, obten la suma de todos lo elementos en una lista."
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"193"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from functools import reduce\n",
"a = [5, 8, 10, 20, 50, 100]\n",
"\n",
"def sumaR(a):\n",
" '''\n",
" La función imprime una lista que contenga la suma de todos lo elementos en una lista,\n",
" Utilizado la funcion *reduce* y una expresion *lambda*. Los elementos de la lista deben ser enteros. \n",
" \n",
" Args:\n",
" a (list): Listas que contenga valores enteros que seran evaluados\n",
" \n",
" Ejemplo:\n",
" \n",
" >>> a = [5, 8, 10, 20, 50, 100]\n",
" >>> sumaR(a)\n",
" 193\n",
" \n",
" '''\n",
" from functools import reduce\n",
" return reduce(lambda n, m: n+m, a)\n",
"\n",
"sumaR(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": 17,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[35, 45, 55, 65]"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = [5, 10, 15, 20]\n",
"b = [30, 35, 40, 45]\n",
"\n",
"def sumaListas(a, b):\n",
" '''\n",
" La función imprime obtener una lista cuyos elementos sean la suma de los elementos \n",
" correspondientes en las listas *a* y *b* respectivamente, utilizando *map* y una expresion *lambda*\n",
" Los elementos de ambas listas deben ser enteros. \n",
" \n",
" Args:\n",
" a, b (list): Listas que contenga valores enteros que seran evaluados\n",
" \n",
" Ejemplo:\n",
" >>> a = [5, 10, 15, 20] \n",
" >>> b = [30, 35, 40, 45]\n",
" >>> sumaListas(a, b)\n",
" [35, 45, 55, 65]\n",
" \n",
" '''\n",
" return list( map( (lambda x, y: x+y), a, b ) )\n",
" \n",
"sumaListas(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": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[(3, 4, 5),\n",
" (5, 12, 13),\n",
" (6, 8, 10),\n",
" (7, 24, 25),\n",
" (8, 15, 17),\n",
" (9, 12, 15),\n",
" (9, 40, 41),\n",
" (10, 24, 26),\n",
" (12, 16, 20),\n",
" (12, 35, 37)]]"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def Terna(n):\n",
" ''' Generador en forma de expresion que obtiene las primeras n ternas pitagoricas\n",
" \n",
" Args:\n",
" n (int): Numero de Ternas Pitagoricas que serán regresadas por el generador\n",
" \n",
" Yields:\n",
" (object): Entrega un objeto que contiene todas las Ternas Pitagóricas hasta n, para visualizar\n",
" el objeto en forma de lista, es necesario utilizar la función list(Terna(n)).\n",
" \n",
" Ejemplo:\n",
" >>> list(Terna(10))\n",
" [[(3, 4, 5), (5, 12, 13), (6, 8, 10), (7, 24, 25), (8, 15, 17), (9, 12, 15), (9, 40, 41), (10, 24, 26), (12, 16, 20), (12, 35, 37)]]\n",
" \n",
" '''\n",
" \n",
" TP = [(a, b, c) for a in range(1, 50) for b in range(a + 1, 50) for c in range(b + 1, 50) if a ** 2 + b ** 2 == c ** 2 ]\n",
" yield TP[:n]\n",
"\n",
"list(Terna(10))"
]
},
{
"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": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[['a', 'b', 'c'],\n",
" ['a', 'c', 'b'],\n",
" ['b', 'a', 'c'],\n",
" ['b', 'c', 'a'],\n",
" ['c', 'a', 'b'],\n",
" ['c', 'b', 'a']]"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = [\"a\", \"b\", \"c\"]\n",
"\n",
"def Permuta(string, prefix=\"\"):\n",
" '''Generador en forma de funcion recursiva que entrega la lista de todas las permutaciones \n",
" de los elementos en una lista, dichos elementos deben ser de tipo string.\n",
" \n",
" Args:\n",
" string (str): Lista de elemntos a ser permutados\n",
" prefix (str): Elemento utilizado por el generados para realizar la permutacion, generelmante es \"\" y no debe cambiarse\n",
" \n",
" Yields:\n",
" (object): Entrega un objeto que contiene todas las permutaciones, para visualizar\n",
" el objeto en forma de lista, es necesario utilizar la función list().\n",
" \n",
" Ejemplo:\n",
" >>> list( Permuta(a) )\n",
" [['a', 'b', 'c'], ['a', 'c', 'b'], ['b', 'a', 'c'], ['b', 'c', 'a'], ['c', 'a', 'b'], ['c', 'b', 'a']]\n",
" '''\n",
" \n",
" string = ''.join(string)\n",
" if len(string) == 1:\n",
" yield list( prefix + string )\n",
" else:\n",
" for i in range(len(string)):\n",
" yield from list( Permuta(string[:i]+string[i+1:], prefix+string[i]) )\n",
"\n",
" \n",
"list(Permuta(a))"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"#no tomar en cuenta, es una prueba, el ejercicio final esta arriba\n",
"def permuta (vec,k,storage):\n",
" if k < len(vec):\n",
" for i in range(k,len(vec)):\n",
" vec[k], vec[i] = vec[i], vec[k]\n",
" permuta (vec, k+1, storage)\n",
" vec[k], vec[i] = vec[i], vec[k]\n",
" else:\n",
" storage.append(vec)\n",
" print(vec)\n"
]
},
{
"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": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"#from miModulo import helloWorld\n",
"#helloWorld.hello()\n",
"\n",
"lista=[]\n",
"for i in range(20):\n",
" lista.append(i)\n",
"lista"
]
},
{
"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": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"function"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"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)"
]
},
{
"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": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"no Python documentation found for 'helloWorld.py'\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"bash: line 1: cd: miModulo: No such file or directory\n"
]
}
],
"source": [
"%%bash\n",
"cd miModulo\n",
"pydoc -w helloWorld.py"
]
},
{
"cell_type": "code",
"execution_count": 24,
"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": 25,
"metadata": {},
"outputs": [],
"source": [
"def multiply(a, b):\n",
" '''\n",
" >>> multiply(4, 3)\n",
" 12\n",
" >>> multiply('a', 3)\n",
" 'aaa'\n",
" '''\n",
" return a * b"
]
},
{
"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": "code",
"execution_count": 26,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Help on function ParImpar in module __main__:\n",
"\n",
"ParImpar(a)\n",
" La función imprime una nueva lista que contenga \"par\" o \"impar\" en cada uno de sus elementos, \n",
" dependiendo del valor del elemento en la lista orginal.\n",
" \n",
" Args:\n",
" a (list): Lista que contenga los enteros a evaluar\n",
" \n",
" Ejemplo:\n",
" >>> ParImpar([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n",
" [(0, 'par'), (1, 'impar'), (2, 'par'), (3, 'impar'), (4, 'par'), (5, 'impar'), (6, 'par'), (7, 'impar'), (8, 'par'), (9, 'impar')]\n",
"\n",
"Help on function Permuta in module __main__:\n",
"\n",
"Permuta(string, prefix='')\n",
" Generador en forma de funcion recursiva que entrega la lista de todas las permutaciones \n",
" de los elementos en una lista, dichos elementos deben ser de tipo string.\n",
" \n",
" Args:\n",
" string (str): Lista de elemntos a ser permutados\n",
" prefix (str): Elemento utilizado por el generados para realizar la permutacion, generelmante es \"\" y no debe cambiarse\n",
" \n",
" Yields:\n",
" (object): Entrega un objeto que contiene todas las permutaciones, para visualizar\n",
" el objeto en forma de lista, es necesario utilizar la función list().\n",
" \n",
" Ejemplo:\n",
" >>> list( Permuta(a) )\n",
" [['a', 'b', 'c'], ['a', 'c', 'b'], ['b', 'a', 'c'], ['b', 'c', 'a'], ['c', 'a', 'b'], ['c', 'b', 'a']]\n",
"\n",
"Help on function Terna in module __main__:\n",
"\n",
"Terna(n)\n",
" Generador en forma de expresion que obtiene las primeras n ternas pitagoricas\n",
" \n",
" Args:\n",
" n (int): Numero de Ternas Pitagoricas que serán regresadas por el generador\n",
" \n",
" Yields:\n",
" (object): Entrega un objeto que contiene todas las Ternas Pitagóricas hasta n, para visualizar\n",
" el objeto en forma de lista, es necesario utilizar la función list(Terna(n)).\n",
" \n",
" Ejemplo:\n",
" >>> list(Terna(10))\n",
" [[(3, 4, 5), (5, 12, 13), (6, 8, 10), (7, 24, 25), (8, 15, 17), (9, 12, 15), (9, 40, 41), (10, 24, 26), (12, 16, 20), (12, 35, 37)]]\n",
"\n",
"Help on function sumaListas in module __main__:\n",
"\n",
"sumaListas(a, b)\n",
" La función imprime obtener una lista cuyos elementos sean la suma de los elementos \n",
" correspondientes en las listas *a* y *b* respectivamente, utilizando *map* y una expresion *lambda*\n",
" Los elementos de ambas listas deben ser enteros. \n",
" \n",
" Args:\n",
" a, b (list): Listas que contenga valores enteros que seran evaluados\n",
" \n",
" Ejemplo:\n",
" >>> a = [5, 10, 15, 20] \n",
" >>> b = [30, 35, 40, 45]\n",
" >>> sumaListas(a, b)\n",
" [35, 45, 55, 65]\n",
"\n",
"Help on function sumaR in module __main__:\n",
"\n",
"sumaR(a)\n",
" La función imprime una lista que contenga la suma de todos lo elementos en una lista,\n",
" Utilizado la funcion *reduce* y una expresion *lambda*. Los elementos de la lista deben ser enteros. \n",
" \n",
" Args:\n",
" a (list): Listas que contenga valores enteros que seran evaluados\n",
" \n",
" Ejemplo:\n",
" \n",
" >>> a = [5, 8, 10, 20, 50, 100]\n",
" >>> sumaR(a)\n",
" 193\n",
"\n",
"Help on function impares in module __main__:\n",
"\n",
"impares(a)\n",
" La función imprime una lista que contenga todos los numeros impares de una lista de entrada **a**,\n",
" Utilizando *filter* y una expresion *lambda*. Los elementos deben ser valor entero, \n",
" \n",
" Args:\n",
" a (list): Lista que contenga valores enteros que seran evaluados\n",
" \n",
" Ejemplo:\n",
" \n",
" >>> a = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]\n",
" >>> impares(a)\n",
" [5, 7, 97, 77, 23, 73, 61]\n",
"\n",
"Help on function paresPosibles in module __main__:\n",
"\n",
"paresPosibles(a, b)\n",
" La funcion Genera una lista que contenga una tuplas \n",
" con todos los pares posibles de elementos entre las dos listas.\n",
" \n",
" Args:\n",
" a, b (list): Listas que contengan los valores a evaluar\n",
" \n",
" Ejemplo:\n",
" >>> a = [0,1,2,3,4,5]\n",
" >>> b = [\"a\",\"b\",\"c\",\"d\"]\n",
" >>> paresPosibles(a, b)\n",
" [(0, 'a'), (0, 'b'), (0, 'c'), (0, 'd'), (1, 'a'), (1, 'b'), (1, 'c'), (1, 'd'), (2, 'a'), (2, 'b'), (2, 'c'), (2, 'd'), (3, 'a'), (3, 'b'), (3, 'c'), (3, 'd'), (4, 'a'), (4, 'b'), (4, 'c'), (4, 'd'), (5, 'a'), (5, 'b'), (5, 'c'), (5, 'd')]\n",
"\n",
"Help on function muestraIndice in module __main__:\n",
"\n",
"muestraIndice(my_list)\n",
" La función imprime una lista de tuplas, en donde cada tupla \n",
" contiene el indice y el valor de elemento de la lista orginal.\n",
" \n",
" Args:\n",
" a(list): Lista que contenga los valores a evaluar\n",
" \n",
" Ejemplo:\n",
" >>> muestraIndice(['apple', 'banana', 'grapes', 'pear'])\n",
" [(0, 'apple'), (1, 'banana'), (2, 'grapes'), (3, 'pear')]\n",
"\n",
"Trying:\n",
" ParImpar([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])\n",
"Expecting:\n",
" [(0, 'par'), (1, 'impar'), (2, 'par'), (3, 'impar'), (4, 'par'), (5, 'impar'), (6, 'par'), (7, 'impar'), (8, 'par'), (9, 'impar')]\n",
"ok\n",
"Trying:\n",
" list( Permuta(a) )\n",
"Expecting:\n",
" [['a', 'b', 'c'], ['a', 'c', 'b'], ['b', 'a', 'c'], ['b', 'c', 'a'], ['c', 'a', 'b'], ['c', 'b', 'a']]\n",
"ok\n",
"Trying:\n",
" list(Terna(10))\n",
"Expecting:\n",
" [[(3, 4, 5), (5, 12, 13), (6, 8, 10), (7, 24, 25), (8, 15, 17), (9, 12, 15), (9, 40, 41), (10, 24, 26), (12, 16, 20), (12, 35, 37)]]\n",
"ok\n",
"Trying:\n",
" print([i for i in example_generator(4)])\n",
"Expecting:\n",
" [0, 1, 2, 3]\n",
"ok\n",
"Trying:\n",
" a = [5, 7, 22, 97, 54, 62, 77, 23, 73, 61]\n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" impares(a)\n",
"Expecting:\n",
" [5, 7, 97, 77, 23, 73, 61]\n",
"ok\n",
"Trying:\n",
" muestraIndice(['apple', 'banana', 'grapes', 'pear'])\n",
"Expecting:\n",
" [(0, 'apple'), (1, 'banana'), (2, 'grapes'), (3, 'pear')]\n",
"ok\n",
"Trying:\n",
" multiply(4, 3)\n",
"Expecting:\n",
" 12\n",
"ok\n",
"Trying:\n",
" multiply('a', 3)\n",
"Expecting:\n",
" 'aaa'\n",
"ok\n",
"Trying:\n",
" a = [0,1,2,3,4,5]\n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" b = [\"a\",\"b\",\"c\",\"d\"]\n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" paresPosibles(a, b)\n",
"Expecting:\n",
" [(0, 'a'), (0, 'b'), (0, 'c'), (0, 'd'), (1, 'a'), (1, 'b'), (1, 'c'), (1, 'd'), (2, 'a'), (2, 'b'), (2, 'c'), (2, 'd'), (3, 'a'), (3, 'b'), (3, 'c'), (3, 'd'), (4, 'a'), (4, 'b'), (4, 'c'), (4, 'd'), (5, 'a'), (5, 'b'), (5, 'c'), (5, 'd')]\n",
"ok\n",
"Trying:\n",
" a = [5, 10, 15, 20] \n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" b = [30, 35, 40, 45]\n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" sumaListas(a, b)\n",
"Expecting:\n",
" [35, 45, 55, 65]\n",
"ok\n",
"Trying:\n",
" a = [5, 8, 10, 20, 50, 100]\n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" sumaR(a)\n",
"Expecting:\n",
" 193\n",
"ok\n",
"4 items had no tests:\n",
" __main__\n",
" __main__.f\n",
" __main__.firstn\n",
" __main__.permuta\n",
"10 items passed all tests:\n",
" 1 tests in __main__.ParImpar\n",
" 1 tests in __main__.Permuta\n",
" 1 tests in __main__.Terna\n",
" 1 tests in __main__.example_generator\n",
" 2 tests in __main__.impares\n",
" 1 tests in __main__.muestraIndice\n",
" 2 tests in __main__.multiply\n",
" 3 tests in __main__.paresPosibles\n",
" 3 tests in __main__.sumaListas\n",
" 2 tests in __main__.sumaR\n",
"17 tests in 14 items.\n",
"17 passed and 0 failed.\n",
"Test passed.\n"
]
},
{
"data": {
"text/plain": [
"TestResults(failed=0, attempted=17)"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"help(ParImpar)\n",
"help(Permuta)\n",
"help(Terna)\n",
"help(sumaListas)\n",
"help(sumaR)\n",
"help(impares)\n",
"help(paresPosibles)\n",
"help(muestraIndice)\n",
"\n",
"import doctest\n",
"doctest.testmod(verbose=True)"
]
}
],
"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.5.3"
}
},
"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