"Las comprensiones de python proveen de una forma consisa de crear listas, diccionarios y conjuntos. Su nombre proviene de teria de conjuntos en donde la *notación contructiva de conjuntos* o comprensión se define como:\n",
"print(\"\\nComo funciona la funcion reduce:\")\n",
"Image.open(ima)\n",
"\n"
]
},
{
...
...
@@ -436,13 +502,28 @@
},
{
"cell_type": "code",
"execution_count":78,
"execution_count": 46,
"metadata": {},
"outputs":[],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[35, 45, 55, 65]\n"
]
}
],
"source": [
"a = [5, 10, 15, 20]\n",
" \n",
"b = [30,35,40,45]"
"b = [30, 35, 40, 45]\n",
"\n",
"def mapLambdaSuma(l1,l2):\n",
" suma=list(map(lambda x,y: x+y, l1,l2))\n",
" return suma\n",
" \n",
"sumi=mapLambdaSuma(a,b)\n",
"print(sumi)\n"
]
},
{
...
...
@@ -455,6 +536,55 @@
"[Wikipedia](https://es.wikipedia.org/wiki/Terna_pitag%C3%B3rica): Una terna pitagórica es un conjunto ordenado de tres números enteros positivos a, b, c, y son solución de la ecuación diofantina cuadrática $a^{2}+b^{2}=c^{2}$.\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[(4, 3, 5), (5, 12, 13), (6, 8, 10)]\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def tripletaPita(valor):\n",
" for i in range(4, valor + 1):\n",
" if i % 2:\n",
" #Impar\n",
" valores = i**2 // 2\n",
" yield i, valores, valores + 1\n",
" else:\n",
" #par\n",
" valores = (i // 2)**2\n",
" yield i, valores - 1, valores + 1\n",
"\n",
"\n",
"print (list(tripletaPita(6)))\n",
"\n",
"#a,b,c\n",
"#4,-7,9\n",
"(4**2)//2\n",
"\n",
"#a,b,c\n",
"#4,3,5\n",
"(5//2)**2\n",
"\n",
"bool(1%2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
...
...
@@ -465,11 +595,38 @@
},
{
"cell_type": "code",
"execution_count":80,
"execution_count": 115,
"metadata": {},
"outputs":[],
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n"
]
}
],
"source": [
"a = [\"a\",\"b\",\"c\"]"
"def recursive_generator(some_list):\n",
" #some_list.reverse()\n",
" if len(some_list)>1:\n",
" for i in recursive_generator(some_list[1:]):\n",
"Python **Docstring** es el texto de documentación que puede aprecer en la definición de una clase, módulo, función o método, y se escribe como la primera declaración. Se puede acceder a las cadenas de documenacion desde el atributo doc para cualquiera de los objetos de Python y también con la función incorporada **help()**."
"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: 𝑎𝑛𝑟=𝑛!𝑟!(𝑛−𝑟)!=(𝑛𝑟)"
"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()"
]
},
{
"cell_type": "code",
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"wrote tarea2_ejercicios1.html\n"
]
}
],
"source": [
"%%bash\n",
"cd miModulo\n",
"pydoc -w tarea2_ejercicios1"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n",
"'def matmult(a,b):\\n zip_b = zip(*b)\\n #uncomment next line if python 3 : \\n zip_b = list(zip_b)\\n return [[sum(ele_a*ele_b for ele_a, ele_b in zip(row_a, col_b)) \\n for col_b in zip_b] for row_a in a]\\n'"
]
},
"execution_count": 173,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def tamMatriz():\n",
" n=int(input(\"Ingrese las filas de la matriz: \\n\"))\n",
" m=int(input(\"Ingrese las columnas de la matriz: \\n\"))\n",
" return [n,m]\n",
"\n",
"def creaMatriz(nFila, nCol):\n",
" matriz = []\n",
" for i in range(nFila):\n",
" listaFila = []\n",
" for j in range(nCol):\n",
" val=int(input(\"Ingresa el valor del elemento\"+str(i)+\",\"+str(j)+\": \\n\"))\n",
" listaFila.append(val)\n",
" matriz.append(listaFila)\n",
"\n",
" return matriz\n",
"\n",
"def mmult1(m1, m2):\n",
" fila_A = len(m1)\n",
" cols_A = len(m1[0])\n",
" fila_B = len(m2)\n",
" cols_B = len(m2[0])\n",
"\n",
" if cols_A != fila_B:\n",
" print (\"Imposible resolver, la dimensionalidad de las matrices no cuadra.\")\n",
" return\n",
"\n",
" C = [[0 for fila in range(cols_B)] for col in range(fila_A)]\n",
" print (C)\n",
"\n",
" for i in range(fila_A):\n",
" for j in range(cols_B):\n",
" for k in range(cols_A):\n",
" C[i][j] += m1[i][k] * m2[k][j]\n",
" return C\n",
"\n",
"#Me encantaria que me podieran explicar esta ya que es extremadamente eficiente :O\n",
"\n",
"'''def matmult(a,b):\n",
" zip_b = zip(*b)\n",
" #uncomment next line if python 3 : \n",
" zip_b = list(zip_b)\n",
" return [[sum(ele_a*ele_b for ele_a, ele_b in zip(row_a, col_b)) \n",
" for col_b in zip_b] for row_a in a]\n",
"'''\n",
"\n",
"'''b=mat2\n",
"print(b)\n",
"zip_b = zip(*b)\n",
"for col_b in zip_b:\n",
" print(col_b)\n",
"'''\n",
"\n",
"### Muy PRO\n",
"m = np.array([[sum(row_a*col_b) for col_b in b.T] for row_a in a])\n",
"'def matmult(a,b):\\n zip_b = zip(*b)\\n #uncomment next line if python 3 : \\n zip_b = list(zip_b)\\n return [[sum(ele_a*ele_b for ele_a, ele_b in zip(row_a, col_b)) \\n for col_b in zip_b] for row_a in a]\\n'"
]
},
"execution_count": 173,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def tamMatriz():\n",
" n=int(input(\"Ingrese las filas de la matriz: \\n\"))\n",
" m=int(input(\"Ingrese las columnas de la matriz: \\n\"))\n",
" return [n,m]\n",
"\n",
"def creaMatriz(nFila, nCol):\n",
" matriz = []\n",
" for i in range(nFila):\n",
" listaFila = []\n",
" for j in range(nCol):\n",
" val=int(input(\"Ingresa el valor del elemento\"+str(i)+\",\"+str(j)+\": \\n\"))\n",
" listaFila.append(val)\n",
" matriz.append(listaFila)\n",
"\n",
" return matriz\n",
"\n",
"def mmult1(m1, m2):\n",
" fila_A = len(m1)\n",
" cols_A = len(m1[0])\n",
" fila_B = len(m2)\n",
" cols_B = len(m2[0])\n",
"\n",
" if cols_A != fila_B:\n",
" print (\"Imposible resolver, la dimensionalidad de las matrices no cuadra.\")\n",
" return\n",
"\n",
" C = [[0 for fila in range(cols_B)] for col in range(fila_A)]\n",
" print (C)\n",
"\n",
" for i in range(fila_A):\n",
" for j in range(cols_B):\n",
" for k in range(cols_A):\n",
" C[i][j] += m1[i][k] * m2[k][j]\n",
" return C\n",
"\n",
"#Me encantaria que me podieran explicar esta ya que es extremadamente eficiente :O\n",
"\n",
"'''def matmult(a,b):\n",
" zip_b = zip(*b)\n",
" #uncomment next line if python 3 : \n",
" zip_b = list(zip_b)\n",
" return [[sum(ele_a*ele_b for ele_a, ele_b in zip(row_a, col_b)) \n",
" for col_b in zip_b] for row_a in a]\n",
"'''\n",
"\n",
"'''b=mat2\n",
"print(b)\n",
"zip_b = zip(*b)\n",
"for col_b in zip_b:\n",
" print(col_b)\n",
"'''\n",
"\n",
"### Muy PRO\n",
"m = np.array([[sum(row_a*col_b) for col_b in b.T] for row_a in a])\n",