-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
1,211 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import asyncio | ||
|
||
global A, Amin | ||
|
||
def leer(): | ||
# Pedir al usuario el tamaño del arreglo | ||
n = int(input("Ingrese el tamaño del arreglo: ")) | ||
# Crear un arreglo vacío | ||
arreglo = [] | ||
# Pedir al usuario que ingrese los elementos del arreglo uno por uno | ||
print("Ingrese los elementos del arreglo, uno por uno:") | ||
for i in range(n): | ||
elemento = float(input(f"Elemento {i + 1}: ")) | ||
arreglo.append(elemento) | ||
return arreglo | ||
|
||
# Función que realiza la suma de dos elementos en un arreglo | ||
async def com(p1, p2): | ||
global A, Amin | ||
if A[p1-1] >= A[p2-1]: | ||
Amin[p1-1] = 1 | ||
else: | ||
Amin[p2-1] = 1 | ||
print(Amin) | ||
|
||
async def asign(i): | ||
global A, Amin | ||
if Amin[i-1] == 0: | ||
print(f'El menor es {A[i-1]} en la posición {i}') | ||
|
||
async def main(): | ||
global A, Amin | ||
A = leer() | ||
n2 = len(A) | ||
Amin = [0] * len(A) | ||
P = [(i, j) for i in range(1, n2+1) for j in range(i+1, n2+1)] | ||
await asyncio.gather(*[com(*pos) for pos in P]) | ||
P = [i for i in range(1, n2+1)] | ||
await asyncio.gather(*[asign(pos) for pos in P]) | ||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import asyncio | ||
import math | ||
import time | ||
|
||
global A, s | ||
|
||
|
||
def leer(): | ||
# Pedir al usuario el tamaño del arreglo | ||
n = int(input("Ingrese el tamaño del arreglo: ")) | ||
# Crear un arreglo vacío | ||
arreglo = [] | ||
# Pedir al usuario que ingrese los elementos del arreglo uno por uno | ||
print("Ingrese los elementos del arreglo, uno por uno:") | ||
for i in range(n): | ||
elemento = float(input(f"Elemento {i + 1}: ")) | ||
arreglo.append(elemento) | ||
return arreglo | ||
|
||
async def ordenar(p1, p2): | ||
global A | ||
if A[p1] == 'Infinito' and A[p2] == 'Infinito': | ||
pass | ||
elif A[p1] != 'Infinito': | ||
pass | ||
else: | ||
A[p1] = A[p2] | ||
if A[p1] != 'Infinito' and A[p2] != 'Infinito' and A[p2] >= A[p1]: | ||
A[p2] = 'Infinito' | ||
else: | ||
A[p1] = A[p2] | ||
A[p2] = 'Infinito' | ||
print(A) | ||
|
||
|
||
async def main(): | ||
global A, s | ||
p = 'No' | ||
A = leer() | ||
n = int(math.log2(len(A))) | ||
n2 = len(A) | ||
s = time.time() | ||
print(A) | ||
for i in range(1, n + 1): | ||
print('Paso nuevo ' + str(i)) | ||
k = pow(2, i) | ||
k0 = int(k / 2) | ||
m = int(n2 / k) | ||
P = [(n2 - j * k, n2 + k0 - j * k) for j in range(1, m + 1)] | ||
print(P) | ||
await asyncio.gather(*[ordenar(*pos) for pos in P]) | ||
print(f'Mínimo es {A[0]}') | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import numpy as np | ||
import asyncio | ||
import time | ||
|
||
async def print_arr(aux, p_i, l): | ||
print("\n [", end="") | ||
for i in range(l): | ||
p = i + p_i | ||
print(f"{aux[p]:.6f}, ", end="") | ||
print("]") | ||
|
||
async def odd_even_split(aux, odd, even, i): | ||
odd[i] = aux[2*i+1] | ||
even[i] = aux[2*i] | ||
print(f'split {2*i+1} y {2*i}') | ||
|
||
async def unir(aux, odd, even, i): | ||
aux[2*i+1] = odd[i] | ||
aux[2*i] = even[i] | ||
print(f'unir {2*i+1} y {2*i}') | ||
|
||
async def comparar(aux,i): | ||
print(f'comparar {2*i-1} y {2*i}') | ||
if aux[2*i-1] > aux[2*i]: | ||
aux[2*i-1], aux[2*i] = aux[2*i], aux[2*i-1] | ||
|
||
async def odd_even_merge(aux, l): | ||
print(f'odd-even-merge {aux}') | ||
if l == 2: | ||
if aux[0] > aux[1]: | ||
aux[0], aux[1] = aux[1], aux[0] | ||
else: | ||
m = l // 2 | ||
|
||
odd = np.zeros(m) | ||
even = np.zeros(m) | ||
tasks = [] | ||
for i in range(m): | ||
tasks.append(odd_even_split(aux, odd, even, i)) | ||
await asyncio.gather(*tasks) | ||
|
||
tasks = [] | ||
tasks.append(odd_even_merge(odd, m)) | ||
tasks.append(odd_even_merge(even, m)) | ||
await asyncio.gather(*tasks) | ||
|
||
tasks = [] | ||
for i in range(m): | ||
tasks.append(unir(aux, odd, even, i)) | ||
await asyncio.gather(*tasks) | ||
|
||
tasks = [] | ||
for i in range(1, m): | ||
tasks.append(comparar(aux, i)) | ||
await asyncio.gather(*tasks) | ||
|
||
async def odd_even_merge_sort(aux, l): | ||
if l > 1: | ||
print(f'odd-even-merge-sort {aux}') | ||
m = l // 2 | ||
tasks = [] | ||
tasks.append(odd_even_merge_sort(aux[:m], m)) | ||
tasks.append(odd_even_merge_sort(aux[m:], m)) | ||
await asyncio.gather(*tasks) | ||
await odd_even_merge(aux, l) | ||
|
||
async def main(): | ||
n1 = int(input("Ingrese la potencia k, para 2^k renglones de 1 a 10: ")) | ||
n2 = 2 ** n1 | ||
print(f"Ordenar vector de {n2}") | ||
L = np.zeros(n2) | ||
print("Ingresar V") | ||
for i in range(n2): | ||
L[i] = float(input(f"Ingresar V[{i+1}]: ")) | ||
start_time = time.time() | ||
await odd_even_merge_sort(L, n2) | ||
end_time = time.time() | ||
print("\nTiempo de ejecución:", end_time - start_time, "segundos") | ||
print("\nEl vector resultante:") | ||
await print_arr(L, 0, n2) | ||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import asyncio | ||
|
||
global A,Aord, A1 | ||
|
||
def leer(): | ||
# Pedir al usuario el tamaño del arreglo | ||
n = int(input("Ingrese el tamaño del arreglo: ")) | ||
# Crear un arreglo vacío | ||
arreglo = [] | ||
# Pedir al usuario que ingrese los elementos del arreglo uno por uno | ||
print("Ingrese los elementos del arreglo, uno por uno:") | ||
for i in range(n): | ||
elemento = float(input(f"Elemento {i + 1}: ")) | ||
arreglo.append(elemento) | ||
return arreglo | ||
|
||
# Función que realiza la suma de dos elementos en un arreglo | ||
async def com(p1, p2): | ||
|
||
global A,Aord | ||
|
||
if A[p1-1]>=A[p2-1]: | ||
Aord[p1-1] +=1 | ||
else: | ||
Aord[p2-1] +=1 | ||
print(Aord) | ||
|
||
async def asign(i): | ||
global A, A1 | ||
A1[Aord[i-1]]= A[i-1] | ||
print(A1) | ||
|
||
|
||
async def main(): | ||
|
||
global A,Aord, A1 | ||
|
||
A = leer() | ||
n2 = len(A) | ||
Aord = [0]*len(A) | ||
A1 = [0]*len(A) | ||
P = [(i,j) for i in range(1, n2+1) for j in range(i+1,n2+1)] | ||
await asyncio.gather(*[com(*pos) for pos in P]) | ||
|
||
P = [i for i in range(1,n2+1)] | ||
await asyncio.gather(*[asign(i) for i in P]) | ||
|
||
print(f'El arreglo ordenado {A1}') | ||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import asyncio | ||
import math | ||
import time | ||
|
||
global A, Aux, s, p | ||
|
||
|
||
def leer(): | ||
# Pedir al usuario el tamaño del arreglo | ||
n = int(input("Ingrese el tamaño del arreglo: ")) | ||
# Crear un arreglo vacío | ||
arreglo = [] | ||
# Pedir al usuario que ingrese los elementos del arreglo uno por uno | ||
print("Ingrese los elementos del arreglo, uno por uno:") | ||
for i in range(n): | ||
elemento = float(input(f"Elemento {i + 1}: ")) | ||
arreglo.append(elemento) | ||
return arreglo | ||
|
||
|
||
async def llenar(p1, p2): | ||
global Aux | ||
Aux[p2 - 1] = Aux[p1 - 1] | ||
|
||
|
||
# Función que realiza la suma de dos elementos en un arreglo | ||
async def cmp(p1): | ||
global A, Aux, p | ||
if (Aux[p1 - 1] != A[p1 - 1]): | ||
A[p1 - 1] = 'Infinito' | ||
else: | ||
p = p1 | ||
print(A) | ||
|
||
async def main(): | ||
global A, Aux, s, p | ||
p = 'No' | ||
A = leer() | ||
n = int(math.log2(len(A))) | ||
n2 = len(A) | ||
s = time.time() | ||
Aux = [0] * n2 | ||
Aux[0] = float(input(f"Ingrea el elemento a buscar: ")) | ||
print(Aux) | ||
for i in range(1, n + 1): | ||
print('Paso ' + str(i)) | ||
k = pow(2, i) | ||
k0 = int(k / 2) | ||
P = [(j, j + k0) for j in range(1, k0 + 1)] | ||
await asyncio.gather(*[llenar(*pos) for pos in P]) | ||
print(Aux) | ||
P = [j for j in range(1, n2 + 1)] | ||
print(P) | ||
await asyncio.gather(*[cmp(pos) for pos in P]) | ||
print(A) | ||
if p == 'No encontrado': | ||
print(p) | ||
else: | ||
print(f'encontrado en {p}') | ||
|
||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import asyncio | ||
import math | ||
|
||
global A, B, C | ||
|
||
def leer_matriz(f): | ||
# Pedir al usuario el número de filas y columnas de la matriz | ||
filas = f | ||
# Crear una matriz vacía | ||
matriz = [] | ||
# Pedir al usuario que ingrese los elementos de la matriz uno por uno | ||
print("Ingrese los elementos de la matriz, fila por fila:") | ||
for i in range(filas): | ||
fila = [] | ||
for j in range(filas): | ||
elemento = float(input(f"Elemento en la posición ({i + 1}, {j + 1}): ")) | ||
fila.append(elemento) | ||
matriz.append(fila) | ||
# Mostrar la matriz | ||
print("La matriz ingresada es:") | ||
for fila in matriz: | ||
print(fila) | ||
return matriz | ||
|
||
async def llenar(i, j, k): | ||
global A, B, C | ||
C[i][j][k] = A[i][k] * B[k][j] | ||
|
||
async def sumar(i, j, k, s): | ||
global C | ||
if (2 * k) % pow(2, s) == 0: | ||
C[i - 1][j - 1][2 * k - 1] += C[i - 1][j - 1][2 * k - int(pow(2, s - 1)) - 1] | ||
|
||
async def main(): | ||
global A, B, C | ||
f = int(input("Ingrese el número de filas/columnas de la matriz: ")) | ||
A = leer_matriz(f) | ||
B = leer_matriz(f) | ||
C = [[[0.0 for _ in range(f)] for _ in range(f)] for _ in range(f)] | ||
P = [(i, j, k) for i in range(0, f) for j in range(0, f) for k in range(0, f)] | ||
await asyncio.gather(*[llenar(*pos) for pos in P]) | ||
for s in range(0, int(math.log2(f))): | ||
P = [(i + 1, j + 1, k + 1, s + 1) for i in range(0, f) for j in range(0, f) for k in range(0, int(f / 2))] | ||
await asyncio.gather(*[sumar(*pos) for pos in P]) | ||
s = '' | ||
for m in range(0, f): | ||
s += '[' | ||
for n in range(0, f): | ||
s = s + str(C[m][n][f - 1]) + ', ' | ||
s += ']\n' | ||
print(f'\n La matriz A*B es: \n {s}') | ||
|
||
if __name__ == "__main__": | ||
asyncio.run(main()) |
Oops, something went wrong.