corrección de primer examen

parent 4f6aad80
...@@ -7,6 +7,524 @@ ...@@ -7,6 +7,524 @@
"# Examen Unidad 1" "# Examen Unidad 1"
] ]
}, },
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"34.5"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def promedio(lista):\n",
" '''La función obtiene el promedio de los numeros en una lista.\n",
" \n",
" Args:\n",
" lista(list): Lista que contenga los valores a evaluar\n",
" \n",
" Ejemplo:\n",
" >>> a = [2,7,43,87,3,65]\n",
" >>> promedio(a)\n",
" 34.5\n",
" '''\n",
" return sum(lista) / len(lista)\n",
"\n",
"a = [2,7,43,87,3,65]\n",
"promedio(a)\n"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"11.5"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from functools import reduce\n",
"\n",
"def ejercicio1(lista):\n",
" \"\"\"ejercicio1 funcion para realizar el promedio de una lista dada\n",
"\n",
" Args:\n",
" lista (list): lista que contiene los numeros a los cuales se realizara el promedio\n",
" Return:\n",
" promedio (int): la suma entre la longitud\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 suma / len(lista)\n",
"\n",
"ejercicio1([10, 11,12,13])"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"4.0\n"
]
}
],
"source": [
"'''4.1 Obten el promedio de los numeros en una lista.'''\n",
"lista1=[2,3,5,6]\n",
"def ejercicio1(lista):\n",
" contador=0\n",
" j=0\n",
" for i in lista:\n",
" j +=i\n",
" contador +=1\n",
" resultado=j/contador\n",
" print(resultado)\n",
" pass\n",
"\n",
"ejercicio1(lista1)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1.0, 2.0, 3.0, 4.0, 5.0]"
]
},
"execution_count": 11,
"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)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"120"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def factorial(n):\n",
" '''La función Encuentra el factorial de un numero usando recursion\n",
" \n",
" Args:\n",
" n(int): El numero al que se quiere calcular su factorial\n",
" \n",
" Ejemplo:\n",
" >>> num = 5\n",
" >>> factorial(num)\n",
" 120\n",
" '''\n",
" if n == 1:\n",
" return n\n",
" else:\n",
" return n*factorial(n-1)\n",
"\n",
"num = 5\n",
"factorial(num)\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[11, 33, 55, 77, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191]"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def palindromo_impar(minimo, maximo):\n",
" '''La función Encuentra todos los numeros que sean impares y palindromos en un rango dado.\n",
" \n",
" Args:\n",
" minimo(int): El valor inicial del rango a evaluar\n",
" maximo(int): El valor fial del rango a evaluar\n",
" \n",
" Ejemplo:\n",
" >>> minimo = 10\n",
" >>> maximo = 300\n",
" >>> palindromo_impar(minimo,maximo)\n",
" [11, 33, 55, 77, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191]\n",
" '''\n",
" lista = []\n",
" for k in range(minimo, maximo):\n",
" if list(str(k)) == list(reversed(str(k))):\n",
" if k %2 != 0:\n",
" lista.append(k)\n",
" return lista\n",
"\n",
"minimo = 10\n",
"maximo = 300\n",
"palindromo_impar(minimo,maximo)\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[11, 33, 55, 77, 99, 101, 111, 121, 131, 141, 151, 161, 171, 181, 191]\n"
]
}
],
"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(10,300))"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Introduce una palabra:hey\n",
"Introduce numero de intentos:5\n",
"_ _ _\n",
"\n",
"Dame una letra: t\n",
"\n",
"Error, te quedan 4 intentos\n",
"\n",
"_ _ _\n",
"\n",
"Dame una letra: y\n",
"\n",
"_ _ y\n",
"\n",
"Dame una letra: e\n",
"\n",
"_ e y\n",
"\n",
"Dame una letra: h\n",
"\n",
"Ganaste, la palabra es \" hey \"\n"
]
}
],
"source": [
"def ahorcado(palabra, intentos):\n",
" '''La función recibe una palabra y un nḿero e interactua con el usuario para jugar *ahorcado*.\n",
" \n",
" Args:\n",
" palabra(str): La palabra a adivinar\n",
" intentos(int): el valor del numero maximo de intentos para adivinar la palabra\n",
" \n",
" Ejemplo:\n",
" >>> ahorcado(palabra, intentos)\n",
" Introduce una palabra:mocoso\n",
" Introduce numero de intentos:3\n",
" _ _ _ _ _ _\n",
" '''\n",
" \n",
" palabraAdivinar = palabra\n",
" intentos = int(intentos)\n",
" \n",
" listaPalabraAdiv = []\n",
" listaPalabraMost = [] \n",
" letra = ''\n",
"\n",
" listaPalabraAdiv = list(palabraAdivinar) #Lo convierto en lista\n",
"\n",
" for item in listaPalabraAdiv:\n",
" listaPalabraMost.append('_')\n",
"\n",
" while True:\n",
"\n",
" print(' '.join(listaPalabraMost)) # Imprime los guiones\n",
"\n",
" letra = input('\\nDame una letra: ')\n",
" print()\n",
"\n",
" fallo = False\n",
"\n",
" if letra not in listaPalabraAdiv: #Intento Fallido\n",
" fallo = True\n",
" intentos -= 1\n",
" print('Error, te quedan', intentos, 'intentos\\n')\n",
" else:\n",
" for key, value in enumerate(listaPalabraAdiv): #Acertó en la palabra\n",
" if value == letra:\n",
" listaPalabraMost[key] = value\n",
"\n",
" if intentos == 0:\n",
" print('Perdiste, la palabra es \"',''.join(listaPalabraAdiv),'\"')\n",
" break\n",
" elif listaPalabraAdiv == listaPalabraMost: \n",
" print('Ganaste, la palabra es \"',''.join(listaPalabraAdiv),'\"')\n",
" break\n",
" \n",
"palabra = input('Introduce una palabra:')\n",
"numero = input('Introduce numero de intentos:')\n",
"ahorcado(palabra, numero)\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"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": 22,
"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",
" list_of_words = texto.split(\" \")\n",
" list_of_words.sort()\n",
" dictCOunt = {word: aText.count(word) for word in aText}\n",
" return(dictCOunt)\n",
"ejercicio6(\"este es un texto con texto, que, es repetido y repetido\")"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"MCMLXXIX\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(1979)\n",
"roma.numeroToRomano()\n",
"import doctest\n",
"doctest.run_docstring_examples(ejercicio7, globals(), verbose=True)"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": null, "execution_count": null,
...@@ -31,7 +549,7 @@ ...@@ -31,7 +549,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.6.8rc1" "version": "3.7.1"
} }
}, },
"nbformat": 4, "nbformat": 4,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment