Tarea 2 con ejercicios de la tarea 1

parent d4b1e5ae
...@@ -754,26 +754,26 @@ ...@@ -754,26 +754,26 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 66, "execution_count": 4,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
"name": "stdout", "name": "stdout",
"output_type": "stream", "output_type": "stream",
"text": [ "text": [
"no Python documentation found for 'helloWorld.py'\n" "wrote helloWorld.html\n"
] ]
} }
], ],
"source": [ "source": [
"%%bash\n", "%%bash\n",
"cd miModulo\n", "cd miModulo\n",
"pydoc -w helloWorld.py" "pydoc -w helloWorld"
] ]
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 26, "execution_count": 5,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -790,7 +790,7 @@ ...@@ -790,7 +790,7 @@
"<td valign=bottom>&nbsp;<br>\n", "<td valign=bottom>&nbsp;<br>\n",
"<font color=\"#ffffff\" face=\"helvetica, arial\">&nbsp;<br><big><big><strong>helloWorld</strong></big></big></font></td\n", "<font color=\"#ffffff\" face=\"helvetica, arial\">&nbsp;<br><big><big><strong>helloWorld</strong></big></big></font></td\n",
"><td align=right valign=bottom\n", "><td align=right valign=bottom\n",
"><font color=\"#ffffff\" face=\"helvetica, arial\"><a href=\".\">index</a><br><a href=\"file:/home/mchc/git/tap1012/miModulo/helloWorld.py\">/home/mchc/git/tap1012/miModulo/helloWorld.py</a></font></td></tr></table>\n", "><font color=\"#ffffff\" face=\"helvetica, arial\"><a href=\".\">index</a><br><a href=\"file:/home/carlosgarcia8/repos/tap1012/miModulo/helloWorld.py\">/home/carlosgarcia8/repos/tap1012/miModulo/helloWorld.py</a></font></td></tr></table>\n",
" <p><tt>This&nbsp;example&nbsp;module&nbsp;shows&nbsp;various&nbsp;types&nbsp;of&nbsp;documentation&nbsp;available&nbsp;for&nbsp;use<br>\n", " <p><tt>This&nbsp;example&nbsp;module&nbsp;shows&nbsp;various&nbsp;types&nbsp;of&nbsp;documentation&nbsp;available&nbsp;for&nbsp;use<br>\n",
"with&nbsp;pydoc.&nbsp;&nbsp;To&nbsp;generate&nbsp;HTML&nbsp;documentation&nbsp;for&nbsp;this&nbsp;module&nbsp;issue&nbsp;the<br>\n", "with&nbsp;pydoc.&nbsp;&nbsp;To&nbsp;generate&nbsp;HTML&nbsp;documentation&nbsp;for&nbsp;this&nbsp;module&nbsp;issue&nbsp;the<br>\n",
"command:<br>\n", "command:<br>\n",
...@@ -811,7 +811,7 @@ ...@@ -811,7 +811,7 @@
"<IPython.core.display.HTML object>" "<IPython.core.display.HTML object>"
] ]
}, },
"execution_count": 26, "execution_count": 5,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
...@@ -894,7 +894,7 @@ ...@@ -894,7 +894,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 6,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -913,7 +913,7 @@ ...@@ -913,7 +913,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 7,
"metadata": {}, "metadata": {},
"outputs": [ "outputs": [
{ {
...@@ -953,7 +953,7 @@ ...@@ -953,7 +953,7 @@
"<IPython.core.display.HTML object>" "<IPython.core.display.HTML object>"
] ]
}, },
"execution_count": 2, "execution_count": 7,
"metadata": {}, "metadata": {},
"output_type": "execute_result" "output_type": "execute_result"
} }
...@@ -962,6 +962,404 @@ ...@@ -962,6 +962,404 @@
"from IPython.display import HTML\n", "from IPython.display import HTML\n",
"HTML(filename=\"miModulo/tarea2.html\")" "HTML(filename=\"miModulo/tarea2.html\")"
] ]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Ejercicios de la tarea 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1.5.1 Imprimir todos los numeros pares entre 0 y 20 usando for o while.\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def numerPar():\n",
" lista=[x for x in range(0,21) if x%2==0]\n",
" return lista\n",
"\n",
"numerPar()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1.5.2 Imprimir todos los numeros mayores a 10 de la lista A"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[13, 21, 34, 55, 89]"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def numerMay(l1):\n",
" lista=[x for x in l1 if x>10]\n",
" return lista\n",
"\n",
"A = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]\n",
"numerMay(A)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1.5.3 Dadas dos listas A y B, obten una lista con sus elementos comunes (A∩B)."
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{1, 2, 3, 5, 8, 13}\n"
]
}
],
"source": [
"a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]\n",
"b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]\n",
"\n",
"def coincide(l1,l2):\n",
" coin=set([x for x in l1 if x in l2])\n",
" return coin\n",
"\n",
"coincide=coincide(a,b)\n",
"print(coincide)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1.5.4 Pregunta al usario cuantos numeros de la secuancia Fibonacci quiere calcular y escribe una funcion que calcule la secuencia e imprima el resultado."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Falta\n",
"def fibonacci(n):\n",
" if n == 0:\n",
" return [0]\n",
" elif n == 1:\n",
" return [0, 1]\n",
" else:\n",
" listaF = fibonacci(n-1)\n",
" listaF.append(listaF[-1] + listaF[-2])\n",
" return listaF\n",
" \n",
"tamaño=int(input(\"Ingresa los N elementos de la secuencia de Fibonacci?\\n: \"))\n",
"tamaño=tamaño-1\n",
"fibonacci(tamaño)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1.5.5 Escribe una funcion que sume todos los numeros en una lista.\n"
]
},
{
"cell_type": "code",
"execution_count": 33,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"20\n"
]
}
],
"source": [
"from functools import reduce\n",
"\n",
"a = [8, 2, 3, 0, 7]\n",
"\n",
"def sumar1(l1):\n",
" sumado=reduce((lambda x,y: x+y),l1)\n",
" return sumado\n",
"\n",
"sumar=sumar1(a)\n",
"print(sumar)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1.5.6 Escribe una funcion que tome una lista y regrese los elementos unicos en la lista."
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4, 5]\n",
"None\n"
]
}
],
"source": [
"a = [1,2,2,3,3,3,3,4,5,5]\n",
"\n",
"def unicos(l1):\n",
" norep=[]\n",
" [norep.append(x) for x in l1 if x not in norep]\n",
" print(norep)\n",
"\n",
"uniqu=unicos(a)\n",
"print(uniqu)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1.5.7 Escribe una funcion que indique si un numero es o no perfecto.\n",
"\n",
"Wikipedia: Un número perfecto es un número natural que es igual a la suma de sus divisores propios positivos. Dicho de otra forma, un número perfecto es aquel que es amigo de sí mismo. Así, 6 es un número perfecto porque sus divisores propios son 1, 2 y 3; y 6 = 1 + 2 + 3. Los siguientes números perfectos son 28, 496 y 8128.\n"
]
},
{
"cell_type": "code",
"execution_count": 93,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Ingresa tu numero\n",
": 496\n",
"True\n"
]
}
],
"source": [
"def perfect(n):\n",
" suma = 0\n",
" for x in range(1, n):\n",
" if n % x == 0:\n",
" suma += x\n",
" return suma == n\n",
"\n",
"def perfeto(n):\n",
" lam=[x for x in range(1,n) if n%x==0]\n",
" sumado=reduce((lambda x,y: x+y),lam)\n",
" return (sumado==n)\n",
" \n",
"numero = int(input(\"Ingresa tu numero\\n: \"))\n",
"prueba=perfeto(numero)\n",
"print(prueba)"
]
},
{
"cell_type": "code",
"execution_count": 73,
"metadata": {},
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (<ipython-input-73-e10a4c95830a>, line 2)",
"output_type": "error",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-73-e10a4c95830a>\"\u001b[0;36m, line \u001b[0;32m2\u001b[0m\n\u001b[0;31m lam=lambda x%n: x in range(n+1) x%n\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"n=6\n",
"lam=lambda x%n: x in range(n+1) x%n\n",
"lam"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1.5.8 Escribe una funcion que imprima las prieras n filas del triangulo de Pascal.\n",
"\n",
"Wolfram: El triángulo de Pascal es un triángulo numérico con números dispuestos en filas escalonadas de manera tal que: 𝑎𝑛𝑟=𝑛!𝑟!(𝑛−𝑟)!=(𝑛𝑟)"
]
},
{
"cell_type": "code",
"execution_count": 56,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Indica el numero de filas\n",
":2\n",
"[[1], [1, 1], [1, 2, 1]]\n"
]
}
],
"source": [
"#Falta\n",
"def pascal(n):\n",
" lista = [[1],[1,1]]\n",
" for i in range(1,n):\n",
" linea = [1]\n",
" for j in range(0,len(lista[i])-1):\n",
" #Suerte de Join\n",
" linea.extend([ lista[i][j] + lista[i][j+1] ])\n",
" linea += [1]\n",
" lista.append(linea) \n",
" print (lista)\n",
" \n",
"numero = int(input(\"Indica el numero de filas\\n:\"))\n",
"pascal(numero)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1.5.9 Escribe una funcion que indique si una frase es un panagrama.\n",
"\n",
"Wikipedia:Un pangrama (del griego: παν γραμμα, «todas las letras») o frase holoalfabética es un texto que usa todas las letras posibles del alfabeto de un idioma."
]
},
{
"cell_type": "code",
"execution_count": 143,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Escriba su frase aqui: \n",
"José compró una vieja zampona en Perú. Excusándose, Sofía tiró su whisky al desagüe de la banqueta\n",
"No soy un pangrama :(\n"
]
}
],
"source": [
"#!/usr/bin/env python\n",
"# -*- coding: utf-8 -*-\n",
"\n",
"def pangramo():\n",
" alfabeto = \"abcdefghijklmnñopqrstuvwxyz\"\n",
" frase=input(\"Escriba su frase aqui: \\n\")\n",
" frase=frase.lower()\n",
" frase=set(frase)\n",
" lista=[letr for letr in frase if letr in alfabeto]\n",
" if len(lista)==27:\n",
" print(\"Soy un pangrama :)\")\n",
" else:\n",
" print(\"No soy un pangrama :(\")\n",
" \n",
"pangramo()\n",
"#José compró una vieja zampona en Perú. Excusándose, Sofía tiró su whisky al desagüe de la banquetañ"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"1.5.10 Escribe un programa que imprima el siguiente un for anidado.\n",
"\n",
"1\n",
"\n",
"22\n",
"\n",
"333\n",
"\n",
"4444\n",
"\n",
"55555\n",
"\n",
"666666\n",
"\n",
"7777777\n",
"\n",
"88888888\n",
"\n",
"999999999\n"
]
},
{
"cell_type": "code",
"execution_count": 167,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"22\n",
"333\n",
"4444\n",
"55555\n",
"666666\n",
"7777777\n",
"88888888\n",
"999999999\n"
]
}
],
"source": [
"def piramide():\n",
" repeticion=[str(x)*x for x in range(1,10)]\n",
" for elemento in repeticion:\n",
" print(elemento)\n",
" \n",
"piramide()"
]
} }
], ],
"metadata": { "metadata": {
......
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