From 6eee1a39118a5ab1ccb575086e0155e6b5ba0e70 Mon Sep 17 00:00:00 2001 From: pedro-avila <157546787+pedro-avila@users.noreply.github.com> Date: Tue, 9 Apr 2024 23:02:02 -0600 Subject: [PATCH 1/3] Add files via upload --- subarreglo_max.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 subarreglo_max.py diff --git a/subarreglo_max.py b/subarreglo_max.py new file mode 100644 index 0000000..2da1603 --- /dev/null +++ b/subarreglo_max.py @@ -0,0 +1,21 @@ +def max_subarray_sum(arr): + max_sum = arr[0] + current_sum = arr[0] + start_index = 0 + end_index = 0 + temp_start_index = 0 + + for i in range(1, len(arr)): + if arr[i] > current_sum + arr[i]: + current_sum = arr[i] + temp_start_index = i + else: + current_sum += arr[i] + + if current_sum > max_sum: + max_sum = current_sum + start_index = temp_start_index + end_index = i + + return max_sum, arr[start_index:end_index+1] + From bdb21e3be06f00ccd60419a78a2751baa3f002f7 Mon Sep 17 00:00:00 2001 From: pedro-avila <157546787+pedro-avila@users.noreply.github.com> Date: Tue, 9 Apr 2024 23:20:59 -0600 Subject: [PATCH 2/3] Delete subarreglo_max.py --- subarreglo_max.py | 21 --------------------- 1 file changed, 21 deletions(-) delete mode 100644 subarreglo_max.py diff --git a/subarreglo_max.py b/subarreglo_max.py deleted file mode 100644 index 2da1603..0000000 --- a/subarreglo_max.py +++ /dev/null @@ -1,21 +0,0 @@ -def max_subarray_sum(arr): - max_sum = arr[0] - current_sum = arr[0] - start_index = 0 - end_index = 0 - temp_start_index = 0 - - for i in range(1, len(arr)): - if arr[i] > current_sum + arr[i]: - current_sum = arr[i] - temp_start_index = i - else: - current_sum += arr[i] - - if current_sum > max_sum: - max_sum = current_sum - start_index = temp_start_index - end_index = i - - return max_sum, arr[start_index:end_index+1] - From 06f824d9de4dccd66a71005d9ca0a741de0586ac Mon Sep 17 00:00:00 2001 From: pedro-avila <157546787+pedro-avila@users.noreply.github.com> Date: Tue, 9 Apr 2024 23:27:54 -0600 Subject: [PATCH 3/3] Add files via upload --- arbol_binario.py | 96 ++++++++++++++--------------------------------- lista_enlazada.py | 51 +++++++++++++++++++++++++ subarreglo_max.py | 26 +++++++++++++ 3 files changed, 105 insertions(+), 68 deletions(-) create mode 100644 lista_enlazada.py create mode 100644 subarreglo_max.py diff --git a/arbol_binario.py b/arbol_binario.py index 2722955..721e48d 100644 --- a/arbol_binario.py +++ b/arbol_binario.py @@ -1,68 +1,28 @@ -class Nodo: - def __init__(self, valor): - self.valor = valor - self.izquierda = None - self.derecha = None - -class ArbolBinarioBusqueda: - def __init__(self): - self.raiz = None - - def insertar(self, valor): - if self.raiz is None: - self.raiz = Nodo(valor) - else: - self._insertar_recursivo(self.raiz, valor) - - def _insertar_recursivo(self, nodo, valor): - if valor < nodo.valor: - if nodo.izquierda is None: - nodo.izquierda = Nodo(valor) - else: - self._insertar_recursivo(nodo.izquierda, valor) - elif valor > nodo.valor: - if nodo.derecha is None: - nodo.derecha = Nodo(valor) - else: - self._insertar_recursivo(nodo.derecha, valor) - - def buscar(self, valor): - return self._buscar_recursivo(self.raiz, valor) - - def _buscar_recursivo(self, nodo, valor): - if nodo is None or nodo.valor == valor: - return nodo is not None - if valor < nodo.valor: - return self._buscar_recursivo(nodo.izquierda, valor) - else: - return self._buscar_recursivo(nodo.derecha, valor) - - def _imprimir_recursivo(self, nodo, espacio): - if nodo is not None: - espacio += 5 - self._imprimir_recursivo(nodo.derecha, espacio) - print() - for _ in range(5, espacio): - print(end=" ") - print(nodo.valor) - self._imprimir_recursivo(nodo.izquierda, espacio) - - def imprimir(self): - self._imprimir_recursivo(self.raiz, 0) - -# Ejemplo de uso -arbol = ArbolBinarioBusqueda() -arbol.insertar(10) -arbol.insertar(5) -arbol.insertar(15) -arbol.insertar(7) -arbol.insertar(3) -arbol.insertar(12) -arbol.insertar(14) -arbol.insertar(20) -arbol.insertar(18) -arbol.insertar(16) - -# Mostrar el árbol binario -print("Árbol binario:") -arbol.imprimir() +class Nodo: + """Representa un nodo en un árbol binario.""" + def __init__(self, valor): + """Inicializa un nodo con el valor dado.""" + self.valor = valor + self.izquierda = None + self.derecha = None + +class ArbolBinario: + """Representa un árbol binario.""" + def __init__(self): + """Inicializa un árbol binario vacío.""" + self.raiz = None + + def insertar(self, valor): + """Inserta un nuevo valor en el árbol.""" + if self.raiz is None: + self.raiz = Nodo(valor) + else: + self._insertar_recursivo(valor, self.raiz) + + def buscar(self, valor): + """Busca un valor en el árbol.""" + return self._buscar_recursivo(valor, self.raiz) + + def imprimir_inorden(self): + """Imprime los valores del árbol en orden (inorden).""" + self._imprimir_inorden_recursivo(self.raiz) diff --git a/lista_enlazada.py b/lista_enlazada.py new file mode 100644 index 0000000..1d3b7d7 --- /dev/null +++ b/lista_enlazada.py @@ -0,0 +1,51 @@ +import numpy as np +import matplotlib.pyplot as plt + +class LinkedList: + def __init__(self): + self.head = None + + def append(self, value): + if self.head is None: + self.head = np.array([value, -1]) # El segundo elemento es el índice del siguiente nodo (-1 si no hay más nodos) + else: + current = self.head + while current[1] != -1: + current_index = int(current[1]) + current = self.array[current_index] + new_node_index = len(self.array) + new_node = np.array([value, -1]) + current[1] = new_node_index + + def display(self): + current = self.head + while current is not None: + print(current[0], end=" -> ") + current_index = int(current[1]) + current = self.array[current_index] + +# Crear una lista enlazada +linked_list = LinkedList() +linked_list.append(1) +linked_list.append(2) +linked_list.append(3) +linked_list.append(4) + +# Mostrar la lista enlazada +linked_list.display() + +# Visualizar la lista enlazada utilizando matplotlib +values = [] +current = linked_list.head +while current is not None: + values.append(current[0]) + current_index = int(current[1]) + current = linked_list.array[current_index] + +plt.figure(figsize=(8, 6)) +plt.title("Linked List") +plt.plot(values, marker='o', linestyle='-') +plt.xlabel("Index") +plt.ylabel("Value") +plt.grid(True) +plt.show() diff --git a/subarreglo_max.py b/subarreglo_max.py new file mode 100644 index 0000000..521357c --- /dev/null +++ b/subarreglo_max.py @@ -0,0 +1,26 @@ +def max_subarray_sum(arr): + max_sum = arr[0] + current_sum = arr[0] + start_index = 0 + end_index = 0 + temp_start_index = 0 + + for i in range(1, len(arr)): + if arr[i] > current_sum + arr[i]: + current_sum = arr[i] + temp_start_index = i + else: + current_sum += arr[i] + + if current_sum > max_sum: + max_sum = current_sum + start_index = temp_start_index + end_index = i + + return max_sum, arr[start_index:end_index+1] + +#funcionalidad +arr = [-2, 1, -3, 4, -1, 2, 1, -5, 4] +max_sum, max_subarray = max_subarray_sum(arr) +print("La suma máxima del subarray contiguo es:", max_sum) +print("El subarray correspondiente es:", max_subarray) \ No newline at end of file