Commit 171629ea authored by geobumac's avatar geobumac

examen 1

parent 8e6e49d6
......@@ -4,7 +4,475 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Examen Unidad 1"
"# 4 Examen Unidad 1\n",
"Escribe una función o classe y su docstring para los siguientes ejercicios. "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4.1 Obten el promedio de los numeros en una lista."
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'El promedio es: 11.5'"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from functools import reduce\n",
"def ejercicio1(lista):\n",
" \"\"\"ejercicio1 funcion para realizar el promedio de una lista dada\n",
"\n",
" Args:\n",
" lista (slist): lista que contiene los numeros a los cuales se realizara el promedio\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> ejercicio1([10, 11,12,13])\n",
" 'El promedio es: 11.5'\n",
" >>> ejercicio1([])\n",
" 'la lista debe contener almenos un elemento'\n",
" >>> ejercicio1([20, 21,22,23])\n",
" 'El promedio es: 21.5'\n",
"\n",
" \"\"\"\n",
" if len(lista) == 0: \n",
" return \"la lista debe contener almenos un elemento\"\n",
" suma = reduce((lambda x, y: x + y), lista)\n",
" return \"El promedio es: \" + str(suma / len(lista))\n",
"\n",
"ejercicio1([10, 11,12,13])\n",
"#import doctest\n",
"\n",
"#doctest.testmod(verbose=True)\n",
"#doctest.run_docstring_examples(ejercicio1, globals(), verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4.2 Regresa todos los numeros enteros en una lista dividida por un numero."
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1.0, 2.0, 3.0, 4.0, 5.0]"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def ejercicio2(lista, numero):\n",
" \"\"\"ejercicio2 funcion que devuelve una lista dentro la cual sus elementos son dividiso por un numero\n",
"\n",
" Args:\n",
" lista (slist): lista de elementos a los cuales se realizara la operacion\n",
" numero (int): numero el cual servira como divisor de los elementos\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> ejercicio2([10, 11,12,13],0)\n",
" 'El segundo elemento debe ser mayor a 0'\n",
" >>> ejercicio2([2,4,6,8,10], 2)\n",
" [1.0, 2.0, 3.0, 4.0, 5.0]\n",
" >>> ejercicio2([], 1)\n",
" []\n",
"\n",
" \"\"\"\n",
" if numero == 0:\n",
" return \"El segundo elemento debe ser mayor a 0\"\n",
" return list(map(lambda i: i/numero, lista))\n",
"ejercicio2([2,4,6,8,10], 2)\n",
"#import doctest\n",
"#doctest.run_docstring_examples(ejercicio2, globals(), verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4.3 Encuentra el factorial de un numero usando recursion.\n"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def ejercicio3(numero):\n",
" \"\"\"ejercicio3 funcion devuelve el factorial de un numero\n",
"\n",
" Args:\n",
" numero (int): numero al cual se calculara su factorial\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> ejercicio3(3)\n",
" 6\n",
" >>> ejercicio3(10)\n",
" 3628800\n",
" >>> ejercicio3(0)\n",
" 1\n",
"\n",
" \"\"\"\n",
" if numero < 1:\n",
" return 1\n",
" else:\n",
" return numero * ejercicio3( numero - 1 )\n",
"ejercicio3(0)\n",
"#import doctest\n",
"#doctest.run_docstring_examples(ejercicio3, globals(), verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4.4 Encuentra todos los numeros que sean impares y palindromos en un rango dado."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def ejercicio4(minimo, maximo):\n",
" \"\"\"ejercicio4 funcion que devuelve una lista de elementos los cuales son impares y palindormos\n",
"\n",
" Args:\n",
" minimo (int): numero inicial para generar el rango de donde se tomaran los valores\n",
" maximo (int): numero final para generar el rango de donde se tomaran los valores\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> ejercicio4(0,10)\n",
" [1, 3, 5, 7, 9]\n",
" >>> ejercicio4(0,1000)\n",
" [1, 3, 5, 7, 9, 11, 33, 55, 77, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 303, 313, 323, 333, 343, 353, 363, 373, 383, 393, 505, 515, 525, 535, 545, 555, 565, 575, 585, 595, 707, 717, 727, 737, 747, 757, 767, 777, 787, 797, 909, 919, 929, 939, 949, 959, 969, 979, 989, 999]\n",
" >>> ejercicio4(0,500)\n",
" [1, 3, 5, 7, 9, 11, 33, 55, 77, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 303, 313, 323, 333, 343, 353, 363, 373, 383, 393]\n",
"\n",
" \"\"\"\n",
" impares = [i for i in range(minimo, maximo) if i%2 != 0]\n",
" #reversestring = [int(str(i)[::-1]) for i in impares]\n",
" return list(filter(lambda x: x == int(str(x)[::-1]), impares))\n",
"print(ejercicio4(0,10))\n",
"#import doctest\n",
"#doctest.run_docstring_examples(ejercicio4, globals(), verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4.5 Escribe una funcion que reciba una plabra y un numero e interactue con el usuario para jugar *ahorcado*, el usuario tiene un numero maximo de intentos para adivinar la palabra:\n",
"[Wikipedia](https://es.wikipedia.org/wiki/Ahorcado_(juego))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ingresa una palabra: as\n",
"Te quedan 4 intentos, hazlo de nuevo:\n",
"Ingresa una palabra: jose\n",
"Eres un adivino\n"
]
}
],
"source": [
"def ejercicio5(palabra, intentos):\n",
" \"\"\"ejercicio5 funcion la cual genera un pequeño juego en el cual se tartara de adivinar una palabra \n",
" con un numero de intentos definidos\n",
"\n",
" Args:\n",
" palabra (string): palabra la cual se tratara de adivinar\n",
" intentos (int): numero de intentos que tendra el usuario para adivinar la palabra\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> ejercicio5(\"jose\", 5)\n",
" Te quedan 4 intentos, hazlo de nuevo:\n",
" Te quedan 3 intentos, hazlo de nuevo:\n",
" Te quedan 2 intentos, hazlo de nuevo:\n",
" Te quedan 1 intentos, hazlo de nuevo:\n",
" Lo siento, perdiste\n",
" >>> ejercicio5(\"jose\", 5)\n",
" Te quedan 4 intentos, hazlo de nuevo:\n",
" Te quedan 3 intentos, hazlo de nuevo:\n",
" Eres un adivino\n",
" >>> ejercicio5(\"jose\", 5)\n",
" Te quedan 4 intentos, hazlo de nuevo:\n",
" Eres un adivino\n",
"\n",
" \"\"\"\n",
" while 0 < intentos:\n",
" intentos -= 1\n",
" adivina = input(\"Ingresa una palabra: \")\n",
" if adivina == palabra:\n",
" print(\"Eres un adivino\")\n",
" break\n",
" else:\n",
" if intentos == 0:\n",
" print(\"Lo siento, perdiste\")\n",
" else:\n",
" output = \"Te quedan \" + str(intentos) + \" intentos, hazlo de nuevo:\"\n",
" print(output)\n",
"#palabra = input(\"Ingrese la palabra a adivinar: \")\n",
"#intentos = int(input(\"Ingrese el numero de intentos que se tendran:\"))\n",
"ejercicio5(\"jose\", 5)\n",
"#import doctest\n",
"#doctest.run_docstring_examples(ejercicio5, globals(), verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4.6 Escribe una funcion que reciva un texto y regrese un diccionario ordenado con el numero de occurencias de cada palabra en el texto."
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'con': 1,\n",
" 'es': 2,\n",
" 'este': 1,\n",
" 'que': 1,\n",
" 'repetido': 2,\n",
" 'texto': 2,\n",
" 'un': 1,\n",
" 'y': 1}"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import re\n",
"def ejercicio6(texto):\n",
" \"\"\"ejercicio6 funcion que devuelve un dictionario con el numero de coincidencias de palabras en un texto\n",
"\n",
" Args:\n",
" texto (string): texto del cual sera base para generar el diccionario de coincidencias de palabras\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> ejercicio6(\"este es un texto con texto que es repetido y repetido\")\n",
" {'con': 1, 'es': 2, 'este': 1, 'que': 1, 'repetido': 2, 'texto': 2, 'un': 1, 'y': 1}\n",
" \"\"\"\n",
" texto = re.sub(r\"[^\\w\\s]\", '', texto)\n",
" dicty = {}\n",
" aText = texto.split(\" \")\n",
" aText.sort()\n",
" dictCOunt = {i:aText.count(i) for i in aText}\n",
" return(dictCOunt)\n",
"ejercicio6(\"este es un texto con texto, que, es repetido y repetido\")\n",
"#import doctest\n",
"#doctest.run_docstring_examples(ejercicio6, globals(), verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4.7 Escribe una clase que convierta un numero entero a numero romano."
]
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"MMCMXXXV\n",
"Finding tests in NoName\n",
"Trying:\n",
" roma = ejercicio7(2935)\n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" roma.numeroToRomano()\n",
"Expecting:\n",
" MMCMXXXV\n",
"ok\n",
"Trying:\n",
" roma = ejercicio7(935)\n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" roma.numeroToRomano()\n",
"Expecting:\n",
" CMXXXV\n",
"ok\n",
"Trying:\n",
" roma = ejercicio7(35)\n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" roma.numeroToRomano()\n",
"Expecting:\n",
" XXXV\n",
"ok\n",
"Trying:\n",
" roma = ejercicio7(5)\n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" roma.numeroToRomano()\n",
"Expecting:\n",
" V\n",
"ok\n"
]
}
],
"source": [
"class ejercicio7:\n",
" \"\"\"ejercicio7 Clase que resuelve la conversion de numeros decimales a romanos, funcion para convertir solo \n",
" resuelve numero menores a 4000 :(\n",
"\n",
" Args:\n",
" numer (int): numero a convertir a romanos\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> roma = ejercicio7(2935)\n",
" >>> roma.numeroToRomano()\n",
" MMCMXXXV\n",
" >>> roma = ejercicio7(935)\n",
" >>> roma.numeroToRomano()\n",
" CMXXXV\n",
" >>> roma = ejercicio7(35)\n",
" >>> roma.numeroToRomano()\n",
" XXXV\n",
" >>> roma = ejercicio7(5)\n",
" >>> roma.numeroToRomano()\n",
" V\n",
" \"\"\"\n",
" unidad=[\"\", \"I\", \"II\", \"III\", \"IV\", \"V\", \"VI\", \"VII\", \"VIII\", \"IX\"]\n",
" decena=[\"\", \"X\", \"XX\", \"XXX\", \"XL\", \"L\", \"LX\", \"LXX\", \"LXXX\", \"XC\"]\n",
" centena=[\"\", \"C\", \"CC\", \"CCC\", \"CD\", \"D\", \"DC\", \"DCC\", \"DCCC\", \"CM\"]\n",
" millar=[\"\", \"M\", \"MM\", \"MMM\"]\n",
" def __init__(self, numero):\n",
" self.numero = numero\n",
" def getUnidades(self, numero):\n",
" return numero%10\n",
" def getDecenas(self, numero):\n",
" return int(numero/10)\n",
" def getCentenas(self, numero):\n",
" return int(numero/100)\n",
" def getMillares(self, numero):\n",
" return int(numero/1000)\n",
" def numeroToRomano(self):\n",
" if self.numero >= 4000:\n",
" print(\"No puedo resolverlo\")\n",
" else:\n",
" if self.numero >= 1000:\n",
" millares = int(self.getMillares(self.numero))\n",
" vactual = self.numero - (millares*1000)\n",
" centenas = int(self.getCentenas(vactual))\n",
" vactual = vactual - (centenas * 100)\n",
" decenas = self.getDecenas(vactual)\n",
" unidades = vactual - (decenas*10)\n",
" print(self.millar[millares] + self.centena[centenas] + self.decena[decenas] + self.unidad[unidades])\n",
" else:\n",
" if self.numero >= 100:\n",
" centenas = int(self.getCentenas(self.numero))\n",
" vactual = self.numero - (centenas * 100)\n",
" decenas = self.getDecenas(vactual)\n",
" unidades = vactual - (decenas*10)\n",
" print(self.centena[centenas] + self.decena[decenas] + self.unidad[unidades])\n",
" else:\n",
" if self.numero >= 10:\n",
" decenas = self.getDecenas(self.numero)\n",
" unidades = self.numero - (decenas*10)\n",
" print(self.decena[decenas] + self.unidad[unidades])\n",
" else:\n",
" print(self.unidad[self.numero])\n",
" \n",
"roma = ejercicio7(2935)\n",
"roma.numeroToRomano()\n",
"import doctest\n",
"doctest.run_docstring_examples(ejercicio7, globals(), verbose=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4.8 Escribe una funcion que regrese el cuadrado mágico del tamaño indicado.\n",
"[Wikipedia](https://es.wikipedia.org/wiki/Cuadrado_m%C3%A1gico) Cuadrado Mágico"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"15.0\n"
]
}
],
"source": [
"def ejercicio8(n):\n",
" conmag = (n*(n**2 + 1)) / 2\n",
" print(conmag)\n",
"ejercicio8(3)"
]
},
{
......@@ -31,7 +499,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8rc1"
"version": "3.6.7"
}
},
"nbformat": 4,
......
......@@ -17,12 +17,48 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 20,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"'El promedio es: 11.5'"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from functools import reduce\n",
"def ejercicio1(lista):\n",
" pass"
" \"\"\"ejercicio1 funcion para realizar el promedio de una lista dada\n",
"\n",
" Args:\n",
" lista (slist): lista que contiene los numeros a los cuales se realizara el promedio\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> ejercicio1([10, 11,12,13])\n",
" 'El promedio es: 11.5'\n",
" >>> ejercicio1([])\n",
" 'la lista debe contener almenos un elemento'\n",
" >>> ejercicio1([20, 21,22,23])\n",
" 'El promedio es: 21.5'\n",
"\n",
" \"\"\"\n",
" if len(lista) == 0: \n",
" return \"la lista debe contener almenos un elemento\"\n",
" suma = reduce((lambda x, y: x + y), lista)\n",
" return \"El promedio es: \" + str(suma / len(lista))\n",
"\n",
"ejercicio1([10, 11,12,13])\n",
"#import doctest\n",
"\n",
"#doctest.testmod(verbose=True)\n",
"#doctest.run_docstring_examples(ejercicio1, globals(), verbose=True)"
]
},
{
......@@ -34,12 +70,44 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 21,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[1.0, 2.0, 3.0, 4.0, 5.0]"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def ejercicio2(lista, numero):\n",
" pass"
" \"\"\"ejercicio2 funcion que devuelve una lista dentro la cual sus elementos son dividiso por un numero\n",
"\n",
" Args:\n",
" lista (slist): lista de elementos a los cuales se realizara la operacion\n",
" numero (int): numero el cual servira como divisor de los elementos\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> ejercicio2([10, 11,12,13],0)\n",
" 'El segundo elemento debe ser mayor a 0'\n",
" >>> ejercicio2([2,4,6,8,10], 2)\n",
" [1.0, 2.0, 3.0, 4.0, 5.0]\n",
" >>> ejercicio2([], 1)\n",
" []\n",
"\n",
" \"\"\"\n",
" if numero == 0:\n",
" return \"El segundo elemento debe ser mayor a 0\"\n",
" return list(map(lambda i: i/numero, lista))\n",
"ejercicio2([2,4,6,8,10], 2)\n",
"#import doctest\n",
"#doctest.run_docstring_examples(ejercicio2, globals(), verbose=True)"
]
},
{
......@@ -51,12 +119,44 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 35,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"1"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def ejercicio3(numero):\n",
" pass"
" \"\"\"ejercicio3 funcion devuelve el factorial de un numero\n",
"\n",
" Args:\n",
" numero (int): numero al cual se calculara su factorial\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> ejercicio3(3)\n",
" 6\n",
" >>> ejercicio3(10)\n",
" 3628800\n",
" >>> ejercicio3(0)\n",
" 1\n",
"\n",
" \"\"\"\n",
" if numero < 1:\n",
" return 1\n",
" else:\n",
" return numero * ejercicio3( numero - 1 )\n",
"ejercicio3(0)\n",
"#import doctest\n",
"#doctest.run_docstring_examples(ejercicio3, globals(), verbose=True)"
]
},
{
......@@ -68,12 +168,33 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def ejercicio4(minimo, maximo):\n",
" pass"
" \"\"\"ejercicio4 funcion que devuelve una lista de elementos los cuales son impares y palindormos\n",
"\n",
" Args:\n",
" minimo (int): numero inicial para generar el rango de donde se tomaran los valores\n",
" maximo (int): numero final para generar el rango de donde se tomaran los valores\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> ejercicio4(0,10)\n",
" [1, 3, 5, 7, 9]\n",
" >>> ejercicio4(0,1000)\n",
" [1, 3, 5, 7, 9, 11, 33, 55, 77, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 303, 313, 323, 333, 343, 353, 363, 373, 383, 393, 505, 515, 525, 535, 545, 555, 565, 575, 585, 595, 707, 717, 727, 737, 747, 757, 767, 777, 787, 797, 909, 919, 929, 939, 949, 959, 969, 979, 989, 999]\n",
" >>> ejercicio4(0,500)\n",
" [1, 3, 5, 7, 9, 11, 33, 55, 77, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191, 303, 313, 323, 333, 343, 353, 363, 373, 383, 393]\n",
"\n",
" \"\"\"\n",
" impares = [i for i in range(minimo, maximo) if i%2 != 0]\n",
" #reversestring = [int(str(i)[::-1]) for i in impares]\n",
" return list(filter(lambda x: x == int(str(x)[::-1]), impares))\n",
"print(ejercicio4(0,10))\n",
"#import doctest\n",
"#doctest.run_docstring_examples(ejercicio4, globals(), verbose=True)"
]
},
{
......@@ -86,12 +207,63 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 3,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ingresa una palabra: as\n",
"Te quedan 4 intentos, hazlo de nuevo:\n",
"Ingresa una palabra: jose\n",
"Eres un adivino\n"
]
}
],
"source": [
"def ejercicio5(palabra, intentos):\n",
" pass"
" \"\"\"ejercicio5 funcion la cual genera un pequeño juego en el cual se tartara de adivinar una palabra \n",
" con un numero de intentos definidos\n",
"\n",
" Args:\n",
" palabra (string): palabra la cual se tratara de adivinar\n",
" intentos (int): numero de intentos que tendra el usuario para adivinar la palabra\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> ejercicio5(\"jose\", 5)\n",
" Te quedan 4 intentos, hazlo de nuevo:\n",
" Te quedan 3 intentos, hazlo de nuevo:\n",
" Te quedan 2 intentos, hazlo de nuevo:\n",
" Te quedan 1 intentos, hazlo de nuevo:\n",
" Lo siento, perdiste\n",
" >>> ejercicio5(\"jose\", 5)\n",
" Te quedan 4 intentos, hazlo de nuevo:\n",
" Te quedan 3 intentos, hazlo de nuevo:\n",
" Eres un adivino\n",
" >>> ejercicio5(\"jose\", 5)\n",
" Te quedan 4 intentos, hazlo de nuevo:\n",
" Eres un adivino\n",
"\n",
" \"\"\"\n",
" while 0 < intentos:\n",
" intentos -= 1\n",
" adivina = input(\"Ingresa una palabra: \")\n",
" if adivina == palabra:\n",
" print(\"Eres un adivino\")\n",
" break\n",
" else:\n",
" if intentos == 0:\n",
" print(\"Lo siento, perdiste\")\n",
" else:\n",
" output = \"Te quedan \" + str(intentos) + \" intentos, hazlo de nuevo:\"\n",
" print(output)\n",
"#palabra = input(\"Ingrese la palabra a adivinar: \")\n",
"#intentos = int(input(\"Ingrese el numero de intentos que se tendran:\"))\n",
"ejercicio5(\"jose\", 5)\n",
"#import doctest\n",
"#doctest.run_docstring_examples(ejercicio5, globals(), verbose=True)"
]
},
{
......@@ -103,12 +275,49 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 35,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"{'con': 1,\n",
" 'es': 2,\n",
" 'este': 1,\n",
" 'que': 1,\n",
" 'repetido': 2,\n",
" 'texto': 2,\n",
" 'un': 1,\n",
" 'y': 1}"
]
},
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import re\n",
"def ejercicio6(texto):\n",
" pass"
" \"\"\"ejercicio6 funcion que devuelve un dictionario con el numero de coincidencias de palabras en un texto\n",
"\n",
" Args:\n",
" texto (string): texto del cual sera base para generar el diccionario de coincidencias de palabras\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> ejercicio6(\"este es un texto con texto que es repetido y repetido\")\n",
" {'con': 1, 'es': 2, 'este': 1, 'que': 1, 'repetido': 2, 'texto': 2, 'un': 1, 'y': 1}\n",
" \"\"\"\n",
" texto = re.sub(r\"[^\\w\\s]\", '', texto)\n",
" dicty = {}\n",
" aText = texto.split(\" \")\n",
" aText.sort()\n",
" dictCOunt = {i:aText.count(i) for i in aText}\n",
" return(dictCOunt)\n",
"ejercicio6(\"este es un texto con texto, que, es repetido y repetido\")\n",
"#import doctest\n",
"#doctest.run_docstring_examples(ejercicio6, globals(), verbose=True)"
]
},
{
......@@ -120,12 +329,122 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 30,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"MMCMXXXV\n",
"Finding tests in NoName\n",
"Trying:\n",
" roma = ejercicio7(2935)\n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" roma.numeroToRomano()\n",
"Expecting:\n",
" MMCMXXXV\n",
"ok\n",
"Trying:\n",
" roma = ejercicio7(935)\n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" roma.numeroToRomano()\n",
"Expecting:\n",
" CMXXXV\n",
"ok\n",
"Trying:\n",
" roma = ejercicio7(35)\n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" roma.numeroToRomano()\n",
"Expecting:\n",
" XXXV\n",
"ok\n",
"Trying:\n",
" roma = ejercicio7(5)\n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" roma.numeroToRomano()\n",
"Expecting:\n",
" V\n",
"ok\n"
]
}
],
"source": [
"class ejercicio7:\n",
" pass"
" \"\"\"ejercicio7 Clase que resuelve la conversion de numeros decimales a romanos, funcion para convertir solo \n",
" resuelve numero menores a 4000 :(\n",
"\n",
" Args:\n",
" numer (int): numero a convertir a romanos\n",
" Yields:\n",
" \n",
" Examples:\n",
" >>> roma = ejercicio7(2935)\n",
" >>> roma.numeroToRomano()\n",
" MMCMXXXV\n",
" >>> roma = ejercicio7(935)\n",
" >>> roma.numeroToRomano()\n",
" CMXXXV\n",
" >>> roma = ejercicio7(35)\n",
" >>> roma.numeroToRomano()\n",
" XXXV\n",
" >>> roma = ejercicio7(5)\n",
" >>> roma.numeroToRomano()\n",
" V\n",
" \"\"\"\n",
" unidad=[\"\", \"I\", \"II\", \"III\", \"IV\", \"V\", \"VI\", \"VII\", \"VIII\", \"IX\"]\n",
" decena=[\"\", \"X\", \"XX\", \"XXX\", \"XL\", \"L\", \"LX\", \"LXX\", \"LXXX\", \"XC\"]\n",
" centena=[\"\", \"C\", \"CC\", \"CCC\", \"CD\", \"D\", \"DC\", \"DCC\", \"DCCC\", \"CM\"]\n",
" millar=[\"\", \"M\", \"MM\", \"MMM\"]\n",
" def __init__(self, numero):\n",
" self.numero = numero\n",
" def getUnidades(self, numero):\n",
" return numero%10\n",
" def getDecenas(self, numero):\n",
" return int(numero/10)\n",
" def getCentenas(self, numero):\n",
" return int(numero/100)\n",
" def getMillares(self, numero):\n",
" return int(numero/1000)\n",
" def numeroToRomano(self):\n",
" if self.numero >= 4000:\n",
" print(\"No puedo resolverlo\")\n",
" else:\n",
" if self.numero >= 1000:\n",
" millares = int(self.getMillares(self.numero))\n",
" vactual = self.numero - (millares*1000)\n",
" centenas = int(self.getCentenas(vactual))\n",
" vactual = vactual - (centenas * 100)\n",
" decenas = self.getDecenas(vactual)\n",
" unidades = vactual - (decenas*10)\n",
" print(self.millar[millares] + self.centena[centenas] + self.decena[decenas] + self.unidad[unidades])\n",
" else:\n",
" if self.numero >= 100:\n",
" centenas = int(self.getCentenas(self.numero))\n",
" vactual = self.numero - (centenas * 100)\n",
" decenas = self.getDecenas(vactual)\n",
" unidades = vactual - (decenas*10)\n",
" print(self.centena[centenas] + self.decena[decenas] + self.unidad[unidades])\n",
" else:\n",
" if self.numero >= 10:\n",
" decenas = self.getDecenas(self.numero)\n",
" unidades = self.numero - (decenas*10)\n",
" print(self.decena[decenas] + self.unidad[unidades])\n",
" else:\n",
" print(self.unidad[self.numero])\n",
" \n",
"roma = ejercicio7(2935)\n",
"roma.numeroToRomano()\n",
"import doctest\n",
"doctest.run_docstring_examples(ejercicio7, globals(), verbose=True)"
]
},
{
......@@ -138,12 +457,22 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 37,
"metadata": {},
"outputs": [],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"15.0\n"
]
}
],
"source": [
"def ejercicio8(n):\n",
" pass"
" conmag = (n*(n**2 + 1)) / 2\n",
" print(conmag)\n",
"ejercicio8(3)"
]
},
{
......@@ -170,7 +499,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.3"
"version": "3.6.7"
}
},
"nbformat": 4,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment