From 2a49cebe74e73a04604a3eba74403cd1997edc13 Mon Sep 17 00:00:00 2001 From: gabisosa1 <139979518+gabisosa1@users.noreply.github.com> Date: Thu, 16 Nov 2023 14:16:02 -0300 Subject: [PATCH] Comments added and new file for map_coloring --- Chapter1/hanoi.py | 31 ++++++++++--------------------- Chapter3/map_coloring.py | 17 ++++------------- Chapter3/matplolib.py | 30 ++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 34 deletions(-) create mode 100644 Chapter3/matplolib.py diff --git a/Chapter1/hanoi.py b/Chapter1/hanoi.py index 48ab600..d644eb6 100644 --- a/Chapter1/hanoi.py +++ b/Chapter1/hanoi.py @@ -1,28 +1,15 @@ -# hanoi.py -# From Classic Computer Science Problems in Python Chapter 1 -# Copyright 2018 David Kopec -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. + from typing import TypeVar, Generic, List -T = TypeVar('T') +T = TypeVar('T') #Variable introducida a partir de la version python 3.5 class Stack(Generic[T]): - def __init__(self) -> None: + def __init__(self) -> None: #constructor de la clase, define la lista con el nombre container de tipo List[T] self._container: List[T] = [] - def push(self, item: T) -> None: + #método que agrega "adjunta a la lista" + def push(self, item: T) -> None: self._container.append(item) def pop(self) -> T: @@ -36,15 +23,17 @@ def __repr__(self) -> str: tower_a: Stack[int] = Stack() tower_b: Stack[int] = Stack() tower_c: Stack[int] = Stack() -for i in range(1, num_discs + 1): + +for i in range(1, num_discs + 1): #Carga los discos a la torre_a tower_a.push(i) def hanoi(begin: Stack[int], end: Stack[int], temp: Stack[int], n: int) -> None: if n == 1: - end.push(begin.pop()) + end.push(begin.pop()) #Primero saca del inicio (begin) y lo que saco lo pone en el fin (end) + else: - hanoi(begin, temp, end, n - 1) + hanoi(begin, temp, end, n - 1) #Itera sin generar impacto hasta llegar a 1, cuando n llega a 1, hace la magia de end.push(begin.pop()) hanoi(begin, end, temp, 1) hanoi(temp, end, begin, n - 1) diff --git a/Chapter3/map_coloring.py b/Chapter3/map_coloring.py index 4332f11..7576a07 100644 --- a/Chapter3/map_coloring.py +++ b/Chapter3/map_coloring.py @@ -1,18 +1,9 @@ # map_coloring.py # From Classic Computer Science Problems in Python Chapter 3 -# Copyright 2018 David Kopec -# -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. +"""El objetivo principal del problema es encontrar una manera de asignar colores +a las regiones de un mapa, de modo que regiones adyacentes (aquellas que comparten una frontera) +no tengan el mismo color.""" + from csp import Constraint, CSP from typing import Dict, List, Optional diff --git a/Chapter3/matplolib.py b/Chapter3/matplolib.py new file mode 100644 index 0000000..b4cf498 --- /dev/null +++ b/Chapter3/matplolib.py @@ -0,0 +1,30 @@ +import matplotlib.pyplot as plt + +def mostrar_mapa(solucion): + # Datos ficticios de regiones y sus colores + regiones = list(solucion.keys()) + colores = list(solucion.values()) + + # Dibujar el mapa con colores asignados a cada región + plt.figure(figsize=(8, 6)) + plt.barh(regiones, [1] * len(regiones), color=colores) + plt.xlabel('Colores') + plt.title('Asignación de colores a regiones') + plt.yticks(fontsize=10) + plt.grid(axis='x', linestyle='--', alpha=0.7) + plt.tight_layout() + + plt.show() + +# Suponiendo que 'solution' contiene la solución obtenida del CSP +solution = { + 'Western Australia': 'red', + 'Northern Territory': 'green', + 'South Australia': 'blue', + 'Queensland': 'red', + 'New South Wales': 'green', + 'Victoria': 'red', + 'Tasmania': 'blue' +} + +mostrar_mapa(solution)