04-Test1_updated.ipynb

parent 30026052
......@@ -54,7 +54,6 @@
}
],
"source": [
"a = [2,7,43,87,3,65]\n",
"def promedio(lista):\n",
" '''La función obtiene el promedio de los numeros en una lista.\n",
" \n",
......@@ -67,6 +66,8 @@
" 34.5\n",
" '''\n",
" return sum(lista) / len(lista)\n",
"\n",
"a = [2,7,43,87,3,65]\n",
"promedio(a)\n",
"\n",
"import doctest\n",
......@@ -82,7 +83,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"outputs": [
{
......@@ -117,14 +118,12 @@
"TestResults(failed=0, attempted=3)"
]
},
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"b = [10, 20, 30, 40, 50]\n",
"num = 4\n",
"def divide(lista, numero):\n",
" '''La función Regresa todos los numeros enteros en una lista dividida por un numero\n",
" \n",
......@@ -138,9 +137,10 @@
" >>> divide(b,num)\n",
" [5.0, 10.0]\n",
" '''\n",
" return list( map(lambda z: z/numero, filter(lambda x: x%numero == 0, b)) )\n",
" return list( map(lambda z: z/numero, filter(lambda x: x%numero == 0, lista)) )\n",
"\n",
" \n",
"b = [10, 20, 30, 40, 50]\n",
"num = 4 \n",
"divide(b,num)\n",
"\n",
"import doctest\n",
......@@ -193,7 +193,6 @@
}
],
"source": [
"num = 5\n",
"def factorial(n):\n",
" '''La función Encuentra el factorial de un numero usando recursion\n",
" \n",
......@@ -205,11 +204,9 @@
" >>> factorial(num)\n",
" 120\n",
" '''\n",
" if n == 1:\n",
" return n\n",
" else:\n",
" return n*factorial(n-1)\n",
" \n",
" return n if n == 1 else n*factorial(n-1)\n",
"\n",
"num = 6 \n",
"factorial(num)\n",
"\n",
"import doctest\n",
......@@ -266,8 +263,6 @@
}
],
"source": [
"minimo = 10\n",
"maximo = 300\n",
"def palindromo_impar(minimo, maximo):\n",
" '''La función Encuentra todos los numeros que sean impares y palindromos en un rango dado.\n",
" \n",
......@@ -281,13 +276,13 @@
" >>> 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",
" palindromo = filter(lambda k: list(str(k)) == list(reversed(str(k))), range(minimo,maximo))\n",
" \n",
" return [i for i in palindromo if i %2 != 0]\n",
"\n",
"minimo = 10\n",
"maximo = 400\n",
"palindromo_impar(minimo,maximo)\n",
"\n",
"import doctest\n",
......@@ -304,44 +299,52 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Introduce una palabra:mocoso\n",
"Introduce una palabra:manopla\n",
"Introduce numero de intentos:3\n",
"_ _ _ _ _ _\n",
"_ _ _ _ _ _ _\n",
"\n",
"Dame una letra: s\n",
"Dame una letra: o\n",
"\n",
"_ _ _ _ s _\n",
"_ _ _ o _ _ _\n",
"\n",
"Dame una letra: o\n",
"Dame una letra: a\n",
"\n",
"_ a _ o _ _ a\n",
"\n",
"Dame una letra: n\n",
"\n",
"_ o _ o s o\n",
"_ a n o _ _ a\n",
"\n",
"Dame una letra: w\n",
"Dame una letra: p\n",
"\n",
"Error, te quedan 2 intentos\n",
"_ a n o p _ a\n",
"\n",
"_ o _ o s o\n",
"Dame una letra: q\n",
"\n",
"Error!!, te quedan 2 intentos\n",
"\n",
"_ a n o p _ a\n",
"\n",
"Dame una letra: m\n",
"\n",
"m o _ o s o\n",
"m a n o p _ a\n",
"\n",
"Dame una letra: c\n",
"Dame una letra: l\n",
"\n",
"Ganaste, la palabra es \" mocoso \"\n",
"Ganaste!!, la palabra es \" manopla \"\n",
"Trying:\n",
" ahorcado(palabra, intentos)\n",
"Expecting:\n",
" Introduce una palabra:mocoso\n",
" Introduce una palabra:manopla\n",
" Introduce numero de intentos:3\n",
" _ _ _ _ _ _\n",
" _ _ _ _ _ _ _\n",
"**********************************************************************\n",
"File \"__main__\", line 9, in __main__.ahorcado\n",
"Failed example:\n",
......@@ -369,14 +372,14 @@
"TestResults(failed=1, attempted=1)"
]
},
"execution_count": 1,
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"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",
" '''La función recibe una palabra y un numero e interactua con el usuario para jugar *ahorcado*.\n",
" \n",
" Args:\n",
" palabra(str): La palabra a adivinar\n",
......@@ -384,50 +387,38 @@
" \n",
" Ejemplo:\n",
" >>> ahorcado(palabra, intentos)\n",
" Introduce una palabra:mocoso\n",
" Introduce una palabra:manopla\n",
" Introduce numero de intentos:3\n",
" _ _ _ _ _ _\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",
" PalabraAdiv = list(palabra)\n",
" PalabraMost = ['_' for item in PalabraAdiv] \n",
"\n",
" for item in listaPalabraAdiv:\n",
" listaPalabraMost.append('_')\n",
" while intentos > 0:\n",
"\n",
" while True:\n",
"\n",
" print(' '.join(listaPalabraMost)) # Imprime los guiones\n",
" print(' '.join(PalabraMost)) # 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",
" if letra not in PalabraAdiv: #Intento Fallido\n",
" intentos -= 1\n",
" print('Error, te quedan', intentos, 'intentos\\n')\n",
" print('Error!!, te quedan', intentos, 'intentos\\n')\n",
" else:\n",
" for key, value in enumerate(listaPalabraAdiv): #Acertó en la palabra\n",
" for key, value in enumerate(PalabraAdiv): #Acertó en la letra\n",
" if value == letra:\n",
" listaPalabraMost[key] = value\n",
" PalabraMost[key] = value\n",
"\n",
" if intentos == 0:\n",
" print('Perdiste, la palabra es \"',''.join(listaPalabraAdiv),'\"')\n",
" print('Perdiste!!, la palabra es \"',''.join(PalabraAdiv),'\"')\n",
" break\n",
" elif listaPalabraAdiv == listaPalabraMost: \n",
" print('Ganaste, la palabra es \"',''.join(listaPalabraAdiv),'\"')\n",
" if PalabraAdiv == PalabraMost: \n",
" print('Ganaste!!, la palabra es \"',''.join(PalabraAdiv),'\"')\n",
" break\n",
" \n",
"palabra = input('Introduce una palabra:')\n",
"numero = input('Introduce numero de intentos:')\n",
"numero = int( input('Introduce numero de intentos:') )\n",
"ahorcado(palabra, numero)\n",
"\n",
"import doctest\n",
......@@ -452,13 +443,13 @@
"output_type": "stream",
"text": [
"Trying:\n",
" texto = 'zayra prueba el ejercicio, prueba un perfume y prueba el pastel'\n",
" frase = 'zayra prueba el ejercicio prueba un perfume prueba el pastel'\n",
"Expecting nothing\n",
"ok\n",
"Trying:\n",
" cuenta_palabras(texto)\n",
" cuenta_palabras(frase)\n",
"Expecting:\n",
" [('ejercicio,', 1), ('el', 2), ('pastel', 1), ('perfume', 1), ('prueba', 3), ('un', 1), ('y', 1), ('zayra', 1)]\n",
" [('ejercicio', 1), ('el', 2), ('pastel', 1), ('perfume', 1), ('prueba', 3), ('un', 1), ('zayra', 1)]\n",
"ok\n",
"1 items had no tests:\n",
" __main__\n",
......@@ -481,34 +472,29 @@
}
],
"source": [
"def cuenta_palabras(str):\n",
"def cuenta_palabras(texto):\n",
" '''La función recibe un texto y regrese un diccionario ordenado con el numero de occurencias de cada palabra en el texto\n",
" \n",
" Args:\n",
" str(str): La cadena de texto a evaluar\n",
" texto(str): La cadena de texto a evaluar\n",
" \n",
" Ejemplo:\n",
" >>> texto = 'zayra prueba el ejercicio, prueba un perfume y prueba el pastel'\n",
" >>> cuenta_palabras(texto)\n",
" [('ejercicio,', 1), ('el', 2), ('pastel', 1), ('perfume', 1), ('prueba', 3), ('un', 1), ('y', 1), ('zayra', 1)]\n",
" >>> frase = 'zayra prueba el ejercicio prueba un perfume prueba el pastel'\n",
" >>> cuenta_palabras(frase)\n",
" [('ejercicio', 1), ('el', 2), ('pastel', 1), ('perfume', 1), ('prueba', 3), ('un', 1), ('zayra', 1)]\n",
" '''\n",
" cuenta = dict()\n",
" texto = str.split()\n",
"\n",
" for palabra in texto:\n",
" cuenta = {}\n",
" \n",
" for palabra in texto.split():\n",
" if palabra in cuenta:\n",
" cuenta[palabra] += 1\n",
" else:\n",
" cuenta[palabra] = 1\n",
" \n",
" return sorted(cuenta.items(), key=lambda x: x[0])\n",
" \n",
" #cuenta = list(cuenta).sort(key=lambda x: x[1])\n",
"\n",
"texto = 'zayra prueba el ejercicio, prueba un perfume y prueba el pastel'\n",
"\n",
"#Al llamar al diccionario da implicito un ordenamiento por \"key\", no es necesario meter la funcion para ordenar nuevamente\n",
"cuenta_palabras(texto)\n",
"frase = 'zayra prueba el ejercicio prueba un perfume prueba el pastel'\n",
"cuenta_palabras(frase)\n",
"\n",
"import doctest\n",
"doctest.testmod(verbose=True)"
......@@ -575,13 +561,9 @@
" romano_convertido = ''\n",
" i = 0\n",
" while numero > 0:\n",
" #print(\"entra a while, i=\",i)\n",
" #print(\"div=\",numero // enteros[i])\n",
" for _ in range(numero // enteros[i]):\n",
" romano_convertido += romanos[i]\n",
" #print(\"rc=\",romano_convertido)\n",
" numero -= enteros[i]\n",
" #print(\"nuevo=\",numero)\n",
" i += 1\n",
" return romano_convertido\n",
"\n",
......
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