Ejercicios realizados

parent 79a57786
......@@ -16,7 +16,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 253,
"metadata": {},
"outputs": [
{
......@@ -25,7 +25,7 @@
"[2, 2, 4, 6, 8]"
]
},
"execution_count": 31,
"execution_count": 253,
"metadata": {},
"output_type": "execute_result"
}
......@@ -41,7 +41,7 @@
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 256,
"metadata": {},
"outputs": [
{
......@@ -50,7 +50,7 @@
"[2, 2, 4, 6, 8]"
]
},
"execution_count": 40,
"execution_count": 256,
"metadata": {},
"output_type": "execute_result"
}
......@@ -63,7 +63,7 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": 188,
"metadata": {},
"outputs": [
{
......@@ -81,7 +81,7 @@
" 9: 387420489}"
]
},
"execution_count": 38,
"execution_count": 188,
"metadata": {},
"output_type": "execute_result"
}
......@@ -93,7 +93,7 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 348,
"metadata": {},
"outputs": [
{
......@@ -102,14 +102,15 @@
"[[1, 0, 0], [0, 1, 0], [0, 0, 1]]"
]
},
"execution_count": 37,
"execution_count": 348,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Comprensión anidada\n",
"[ [ 1 if item_idx == row_idx else 0 for item_idx in range(0, 3) ] for row_idx in range(0, 3) ]"
"k = [ [ 1 if item_idx == row_idx else 0 for item_idx in range(0, 3) ] for row_idx in range(0, 3) ]\n",
"k"
]
},
{
......@@ -151,7 +152,7 @@
},
{
"cell_type": "code",
"execution_count": 77,
"execution_count": 38,
"metadata": {},
"outputs": [
{
......@@ -160,7 +161,7 @@
"function"
]
},
"execution_count": 77,
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
......@@ -192,29 +193,25 @@
},
{
"cell_type": "code",
"execution_count": 47,
"execution_count": 186,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(13, -3), (4, 1), (1, 2), (9, 10)]"
"name": "stdout",
"output_type": "stream",
"text": [
"[(13, -3), (4, 1), (1, 2), (9, 10)]\n"
]
},
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = [(1, 2), (4, 1), (9, 10), (13, -3)]\n",
"a.sort(key=lambda x: x[1])\n",
"a"
"a.sort(key=lambda x: x[1])"
]
},
{
"cell_type": "code",
"execution_count": 51,
"execution_count": 69,
"metadata": {},
"outputs": [
{
......@@ -241,14 +238,20 @@
},
{
"cell_type": "code",
"execution_count": 55,
"execution_count": 161,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"499999500000\n"
"0\n",
"1\n",
"2\n",
"3\n",
"4\n",
"5\n",
"15\n"
]
}
],
......@@ -257,9 +260,10 @@
" num = 0\n",
" while num < n:\n",
" yield num\n",
" print(num)\n",
" num += 1\n",
"\n",
"sum_of_first_n = sum(firstn(1000000))\n",
"sum_of_first_n = sum(firstn(6))\n",
"print(sum_of_first_n)"
]
},
......@@ -272,7 +276,7 @@
},
{
"cell_type": "code",
"execution_count": 66,
"execution_count": 110,
"metadata": {},
"outputs": [
{
......@@ -303,16 +307,38 @@
"source": [
"## 2.4 Ejercicios\n",
"### 2.4.1\n",
"Dada una lista de enteros **a**, utilisando una comprensión, Imprimir una nueva lista que contenga \"par\" o \"impar\" en cada uno de sus elementos, dependiendo del valor del elemento en la lista orginal."
"Dada una lista de enteros **a**, utilizando una comprensión, Imprimir una nueva lista que contenga \"par\" o \"impar\" en cada uno de sus elementos, dependiendo del valor del elemento en la lista orginal."
]
},
{
"cell_type": "code",
"execution_count": 67,
"execution_count": 374,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[(0, 'par'),\n",
" (1, 'impar'),\n",
" (2, 'par'),\n",
" (3, 'impar'),\n",
" (4, 'par'),\n",
" (5, 'impar'),\n",
" (6, 'par'),\n",
" (7, 'impar'),\n",
" (8, 'par'),\n",
" (9, 'impar')]"
]
},
"execution_count": 374,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]"
"a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
"\n",
"[ (x, 'par') if x%2==0 else (x, 'impar') for x in a ]"
]
},
{
......@@ -325,11 +351,24 @@
},
{
"cell_type": "code",
"execution_count": 69,
"execution_count": 342,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[(0, 'apple'), (1, 'banana'), (2, 'grapes'), (3, 'pear')]"
]
},
"execution_count": 342,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"my_list = ['apple', 'banana', 'grapes', 'pear']\n"
"my_list = ['apple', 'banana', 'grapes', 'pear']\n",
"\n",
"[ (k, my_list[k]) for k in range(len(my_list)) ]\n"
]
},
{
......@@ -342,12 +381,66 @@
},
{
"cell_type": "code",
"execution_count": 72,
"execution_count": 349,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[(0, 'a'),\n",
" (0, 'b'),\n",
" (0, 'c'),\n",
" (0, 'd'),\n",
" (0, 'e'),\n",
" (0, 'f'),\n",
" (1, 'a'),\n",
" (1, 'b'),\n",
" (1, 'c'),\n",
" (1, 'd'),\n",
" (1, 'e'),\n",
" (1, 'f'),\n",
" (2, 'a'),\n",
" (2, 'b'),\n",
" (2, 'c'),\n",
" (2, 'd'),\n",
" (2, 'e'),\n",
" (2, 'f'),\n",
" (3, 'a'),\n",
" (3, 'b'),\n",
" (3, 'c'),\n",
" (3, 'd'),\n",
" (3, 'e'),\n",
" (3, 'f'),\n",
" (4, 'a'),\n",
" (4, 'b'),\n",
" (4, 'c'),\n",
" (4, 'd'),\n",
" (4, 'e'),\n",
" (4, 'f'),\n",
" (5, 'a'),\n",
" (5, 'b'),\n",
" (5, 'c'),\n",
" (5, 'd'),\n",
" (5, 'e'),\n",
" (5, 'f'),\n",
" (6, 'a'),\n",
" (6, 'b'),\n",
" (6, 'c'),\n",
" (6, 'd'),\n",
" (6, 'e'),\n",
" (6, 'f')]"
]
},
"execution_count": 349,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = [0,1,2,3,4,5,6]\n",
"b = [\"a\",\"b\",\"c\",\"d\",\"e\",\"f\"]"
"b = [\"a\",\"b\",\"c\",\"d\",\"e\",\"f\"]\n",
"\n",
"[ (num, char) for num in a for char in b ]"
]
},
{
......@@ -360,11 +453,25 @@
},
{
"cell_type": "code",
"execution_count": 74,
"execution_count": 354,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[5, 7, 97, 77, 23, 73, 61]"
]
},
"execution_count": 354,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a=[5, 7, 22, 97, 54, 62, 77, 23, 73, 61]"
"a=[5, 7, 22, 97, 54, 62, 77, 23, 73, 61]\n",
"\n",
"b = filter(lambda y: y%2==1, a)\n",
"list(b)"
]
},
{
......@@ -377,11 +484,26 @@
},
{
"cell_type": "code",
"execution_count": 76,
"execution_count": 376,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"193"
]
},
"execution_count": 376,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = [5, 8, 10, 20, 50, 100] "
"from functools import reduce\n",
"\n",
"a = [5, 8, 10, 20, 50, 100]\n",
"\n",
"reduce(lambda n, m: n+m, a)"
]
},
{
......@@ -394,13 +516,27 @@
},
{
"cell_type": "code",
"execution_count": 78,
"execution_count": 103,
"metadata": {},
"outputs": [],
"outputs": [
{
"data": {
"text/plain": [
"[35, 45, 55, 65]"
]
},
"execution_count": 103,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = [5, 10, 15, 20]\n",
" \n",
"b = [30, 35, 40, 45]"
"b = [30, 35, 40, 45]\n",
"\n",
"c = map( (lambda x, y: x+y), a, b )\n",
"list(c)"
]
},
{
......@@ -413,6 +549,39 @@
"[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": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[[(3, 4, 5),\n",
" (5, 12, 13),\n",
" (6, 8, 10),\n",
" (7, 24, 25),\n",
" (8, 15, 17),\n",
" (9, 12, 15),\n",
" (9, 40, 41),\n",
" (10, 24, 26),\n",
" (12, 16, 20),\n",
" (12, 35, 37)]]"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def Terna(n):\n",
" TP = [(a, b, c) for a in range(1, 50) for b in range(a + 1, 50) for c in range(b + 1, 50) if a ** 2 + b ** 2 == c ** 2 ]\n",
" yield TP[0:n]\n",
" \n",
"list(Terna(10))"
]
},
{
"cell_type": "markdown",
"metadata": {},
......@@ -423,11 +592,54 @@
},
{
"cell_type": "code",
"execution_count": 80,
"execution_count": 27,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[['a', 'b', 'c'],\n",
" ['a', 'c', 'b'],\n",
" ['b', 'a', 'c'],\n",
" ['b', 'c', 'a'],\n",
" ['c', 'a', 'b'],\n",
" ['c', 'b', 'a']]"
]
},
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"a = [\"a\", \"b\", \"c\"]\n",
"\n",
"def Permuta(string, prefix=\"\"):\n",
" if len(string) == 1:\n",
" yield prefix + string\n",
" else:\n",
" for i in range(len(string)):\n",
" yield from getPermutations(string[:i]+string[i+1:], prefix+string[i])\n",
"\n",
"[list(perm) for perm in Permuta(''.join(a))]"
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
"a = [\"a\", \"b\", \"c\"]"
"'''no tomar en cuenta, es una prueba, el ejercicio final esta arriba''' \n",
"def permuta (vec,k,storage):\n",
" if k < len(vec):\n",
" for i in range(k,len(vec)):\n",
" vec[k], vec[i] = vec[i], vec[k]\n",
" permuta (vec, k+1, storage)\n",
" vec[k], vec[i] = vec[i], vec[k]\n",
" else:\n",
" storage.append(vec)\n",
" print(vec)\n"
]
},
{
......@@ -447,20 +659,28 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 87,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"hello\n"
"data": {
"text/plain": [
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]"
]
},
"execution_count": 87,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from miModulo import helloWorld\n",
"helloWorld.hello()"
"#from miModulo import helloWorld\n",
"#helloWorld.hello()\n",
"\n",
"lista=[]\n",
"for i in range(20):\n",
" lista.append(i)\n",
"lista"
]
},
{
......
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