Examen 1 vpacheco

parent 229a40fd
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 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": [
{
"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": "markdown",
"metadata": {},
"source": [
"## 4.2 Regresa todos los numeros enteros en una lista dividida por un numero."
]
},
{
"cell_type": "code",
"execution_count": 196,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1.0\n",
"2.0\n"
]
}
],
"source": [
"'''4.2 Regresa todos los numeros enteros en una lista dividida por un numero.'''\n",
"lista2=[2,4,3,6,8,10,13]\n",
"def ejercicio2(lista, numero):\n",
" for i in lista2:\n",
" if(i%numero==0):\n",
" j=i/numero\n",
" print (j)\n",
" pass\n",
"ejercicio2(lista2,3)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4.3 Encuentra el factorial de un numero usando recursion.\n"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"120\n"
]
}
],
"source": [
"'''4.3 Encuentra el factorial de un numero usando recursion.'''\n",
"numero=5\n",
"def ejercicio3(numero):\n",
" resultado=0\n",
" inicio=numero\n",
" for i in range(numero-1):\n",
" inicio=inicio-1\n",
" if(numero==inicio+1):\n",
" resultado=inicio*(inicio+1)\n",
" else:\n",
" resultado=resultado*(inicio)\n",
" print(resultado)\n",
" pass\n",
"ejercicio3(numero)"
]
},
{
"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": 72,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"3\n",
"5\n",
"7\n",
"9\n",
"11\n",
"33\n",
"55\n",
"77\n",
"99\n"
]
}
],
"source": [
"'''4.4 Encuentra todos los numeros que sean impares y palindromos en un rango dado.'''\n",
"minimo=0\n",
"maximo=100\n",
"listoriginal=[123,'abc','def']\n",
"listoriginal2=[123,'abc','def']\n",
"listswitch=listoriginal[::-1]\n",
"if(listoriginal==listswitch):\n",
" print(\"son iguales\")\n",
"def ejercicio4(minimo, maximo):\n",
" for i in range(minimo,maximo+1):\n",
" a=[x for x in str(i)]\n",
" b=a[::-1]\n",
" if(i%2!=0 and a==b):\n",
" print(i)\n",
" pass\n",
"ejercicio4(minimo,maximo)"
]
},
{
"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": 109,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Que palabra deseas que juegue? leon\n",
"Intentos?6\n",
"Comienza el juego...\n",
"Bienvenido a ahorcado\n",
"Intento número: 1 / 6\n",
"Que letra deseas?l\n",
"aciertos= ['l']\n",
"Acertaste, sigue asi\n",
"Intento número: 2 / 6\n",
"Que letra deseas?e\n",
"aciertos= ['l', 'e']\n",
"Acertaste, sigue asi\n",
"Intento número: 3 / 6\n",
"Que letra deseas?o\n",
"aciertos= ['l', 'e', 'o']\n",
"Acertaste, sigue asi\n",
"Intento número: 4 / 6\n",
"Que letra deseas?n\n",
"aciertos= ['l', 'e', 'o', 'n']\n",
"Acertaste, sigue asi\n",
"Intento número: 5 / 6\n",
"Que letra deseas?h\n",
"Letra incorrecta, intenta nuevamente\n",
"Intento número: 6 / 6\n",
"Que letra deseas?y\n",
"Letra incorrecta, intenta nuevamente\n",
"Lo lograste!\n",
"listpal= ['l', 'e', 'o', 'n']\n",
"listres= ['l', 'e', 'o', 'n']\n"
]
}
],
"source": [
"#supercalifragilisticoespialidoso\n",
"palabra = str(input(\"Que palabra deseas que juegue? \"))\n",
"numintentos = int(input(\"Intentos?\"))\n",
"print(\"Comienza el juego...\")\n",
"def ejercicio5(palabra, numintentos):\n",
" #print('la palabra es:',palabra,'los intentos son:',intentos)\n",
" print('Bienvenido a ahorcado')\n",
" listaciertos=[]\n",
" listpal=[x for x in str(palabra)]\n",
" listres=[]\n",
" banderaj=0\n",
" banderam=0\n",
" for i in range(numintentos):\n",
" print(\"Intento número:\",i+1,\"/\",numintentos)\n",
" intento=str(input(\"Que letra deseas?\"))\n",
" for j in listpal:\n",
" if(j==intento):\n",
" for m in listaciertos:\n",
" if(intento==m):\n",
" banderam=1\n",
" if(banderam==1):\n",
" print(\"Letra usada anteriormente (pierdes un turno), intenta otra\")\n",
" else:\n",
" listaciertos.append(intento)\n",
" print('aciertos=',listaciertos)\n",
" banderaj=1\n",
" if(banderaj==1):\n",
" print(\"Acertaste, sigue asi\")\n",
" else:\n",
" print(\"Letra incorrecta, intenta nuevamente\")\n",
" banderaj=0\n",
" banderam=0\n",
" #print(i)\n",
" if(len(listpal)==len(listaciertos)):\n",
" print(\"Lo lograste!\")\n",
" else:\n",
" print(\"No pudiste conseguirlo :/\")\n",
" print(\"listpal=\",listpal)\n",
" print(\"listres=\",listaciertos)\n",
" pass\n",
"ejercicio5(palabra,numintentos)"
]
},
{
"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": 175,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"a : 1\n",
"el : 3\n",
"gato : 2\n",
"negro : 1\n",
"zacarias : 1\n"
]
}
],
"source": [
"texto=\"el gato gato zacarias negro el el a\"\n",
"#texto='Lorem Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.'\n",
"texto2=texto.split()\n",
"def ejercicio6(texto):\n",
" val=0\n",
" usadas=[]\n",
" bandera=0\n",
" for i in texto:\n",
" #print(usadas)\n",
" bandera=0\n",
" for k in usadas:\n",
" #print(k)\n",
" if(i==k[0] and bandera==0):\n",
" bandera=1\n",
" #usadas[1]+=1\n",
" for m in usadas:\n",
" if(m[0]==k[0]):\n",
" m[1]+=1\n",
" if(bandera==0):\n",
" usadas.append([i,1])\n",
" used=sorted(usadas) \n",
" #print(used)\n",
" for i in used:\n",
" print(i[0],\":\",i[1])\n",
" \n",
" pass\n",
"\n",
"def alphaonly(text):\n",
" resultado=\"\"\n",
" uso=[x for x in str(text)]\n",
" for char in uso:\n",
" if(char.isalpha()):\n",
" resultado=resultado+char\n",
" return resultado\n",
" pass\n",
"\n",
"texto3=[]\n",
"for i in texto2:\n",
" j=alphaonly(i)\n",
" #print(j)\n",
" texto3.append(j)\n",
"#print(texto3)\n",
"ejercicio6(texto3)\n",
"#res = list(set(lista) & set(lista2)) para intersecciones de conjuntos en listados"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4.7 Escribe una clase que convierta un numero entero a numero romano."
]
},
{
"cell_type": "code",
"execution_count": 219,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ingresa el número:10\n",
"10:\n",
"x"
]
},
{
"data": {
"text/plain": [
"<__main__.ejercicio7 at 0x7f509826a8d0>"
]
},
"execution_count": 219,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class ejercicio7:\n",
" numdec=[9000,5000,4000,1000,900,500,400,100,90,50,40,10,9,5,4,1]\n",
" rom=[\"IX'\",\"V'\",\"IV'\",\"m\",\"cm\",\"d\",\"cd\",\"c\",\"cx\",\"i\",\"xi\",\"x\",\"ix\",\"v\",\"iv\",\"i\",]\n",
" def __init__(self,numinsertado):\n",
" \n",
" if(numinsertado==0):\n",
" print(\"No existe el cero en romanos\")\n",
" print (str(numinsertado)+\":\")\n",
"\n",
" i=0\n",
" while(numinsertado>0):\n",
" if(numinsertado>=numdec[i]):\n",
" print(str(rom[i]),end=\"\")\n",
" numinsertado=numinsertado-numdec[i]\n",
" else:\n",
" i=i+1\n",
" pass\n",
"\n",
"numinsertado=int(input(\"Ingresa el número:\"))\n",
"ejercicio7(numinsertado)\n",
"\n"
]
},
{
"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": 14,
"metadata": {},
"outputs": [],
"source": [
"def ejercicio8(n):\n",
" pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"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.6.7"
}
},
"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