04-Test1_updated.ipynb

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