|
| 1 | +# #03 ESTRUCTURAS DE DATOS |
| 2 | +# Muestra ejemplos de creacion de todas las estructuras soportadas por defecto en tu lenguaje |
| 3 | +# Utiliza operaciones de inserccion, borrado, actualizacion y ordenacion. |
| 4 | + |
| 5 | +# Lista |
| 6 | +print("List") |
| 7 | +List = ["Python", "C", "JavaScript", "Ruby"] |
| 8 | +print(List) |
| 9 | + |
| 10 | +## Insercion |
| 11 | +print("New element to append: 'Go'") |
| 12 | +List.append("Go") |
| 13 | +print("New List", List) |
| 14 | + |
| 15 | +print("New element to insert: 'HTML' in position 1") |
| 16 | +List.insert(1, "HTML") |
| 17 | +print("New List", List) |
| 18 | + |
| 19 | +## Borrado |
| 20 | +print("Element to remove: 'Ruby'") |
| 21 | +List.remove("Ruby") |
| 22 | +print("New List", List) |
| 23 | + |
| 24 | +print("Element to pop: 'Python'") |
| 25 | +List.pop(0) |
| 26 | +print("New List", List) |
| 27 | + |
| 28 | +## Ordenamiento |
| 29 | +print("Ordered List") |
| 30 | +List.sort() |
| 31 | +print("New List", List) |
| 32 | + |
| 33 | +# Lista |
| 34 | +List = ["Python", "C", "JavaScript", "Ruby"] |
| 35 | +print(List) |
| 36 | + |
| 37 | +## Insercion |
| 38 | +print("New element to append: 'Go'") |
| 39 | +List.append("Go") |
| 40 | +print("New List", List) |
| 41 | + |
| 42 | +print("New element to insert: 'HTML' in position 1") |
| 43 | +List.insert(1, "HTML") |
| 44 | +print("New List", List) |
| 45 | + |
| 46 | +## Borrado |
| 47 | +print("Element to remove: 'Ruby'") |
| 48 | +List.remove("Ruby") |
| 49 | +print("New List", List) |
| 50 | + |
| 51 | +print("Element to pop: 'Python'") |
| 52 | +List.pop(0) |
| 53 | +print("New List", List) |
| 54 | + |
| 55 | +## Ordenamiento |
| 56 | +print("Ordered List") |
| 57 | +List.sort() |
| 58 | +print("New List", List) |
| 59 | +## Primer elemento de la lista |
| 60 | +print("First element of List") |
| 61 | +print(List[0]) |
| 62 | + |
| 63 | +## Ultimo elemento de la lista |
| 64 | +print("Last element of List") |
| 65 | +print(List[-1]) |
| 66 | + |
| 67 | +print() |
| 68 | + |
| 69 | +#---------------------------------------------------------- |
| 70 | +# Dictionary |
| 71 | +print("Dictionary") |
| 72 | +Dictionary = {0:"Python", 1:"C", 2:"JavaScript", 3:"Ruby"} |
| 73 | + |
| 74 | +## Accessar por medio de clave |
| 75 | +print("Accesing element using key") |
| 76 | +print(Dictionary[0]) |
| 77 | + |
| 78 | +## Accessar usando get |
| 79 | +print("Accesing element using get") |
| 80 | +print(Dictionary.get(1)) |
| 81 | + |
| 82 | +print() |
| 83 | +#---------------------------------------------------------- |
| 84 | +# Tuple |
| 85 | +print("Tuple") |
| 86 | +Tuple = ("Python", "C", "JavaScript", "Ruby") |
| 87 | +print(Tuple) |
| 88 | + |
| 89 | +## Primer elemento de la lista |
| 90 | +print("First element of Tuple") |
| 91 | +print(Tuple[0]) |
| 92 | + |
| 93 | +## Ultimo elemento de la lista |
| 94 | +print("Last element of Tuple") |
| 95 | +print(Tuple[-1]) |
| 96 | + |
| 97 | +print() |
| 98 | +#---------------------------------------------------------- |
| 99 | +# Dificultad Extra |
| 100 | +''' |
| 101 | + Crea una agenda de contactos por terminal. |
| 102 | + - Debes implementar funcionalidades de búsqueda, inserción, actualización y eliminación de contactos. |
| 103 | + - Cada contacto debe tener un nombre y un número de teléfono. |
| 104 | + - El programa solicita en primer lugar cuál es la operación que se quiere realizar, y a continuación |
| 105 | + los datos necesarios para llevarla a cabo. |
| 106 | + - El programa no puede dejar introducir números de teléfono no númericos y con más de 11 dígitos. |
| 107 | + (o el número de dígitos que quieras) |
| 108 | + - También se debe proponer una operación de finalización del programa. |
| 109 | +''' |
| 110 | + |
| 111 | +agenda = {} |
| 112 | + |
| 113 | +def search(data): |
| 114 | + if data.isdigit(): |
| 115 | + for key, value in agenda.items(): |
| 116 | + if value == int(data): |
| 117 | + print(f"{key}: {value}") |
| 118 | + else: |
| 119 | + if data in agenda: |
| 120 | + print(f"{data}: {agenda[data]}") |
| 121 | + |
| 122 | +def insert(name, number): |
| 123 | + if name not in agenda: |
| 124 | + agenda[name] = number |
| 125 | + else: |
| 126 | + print(f"{name} is in the agenda") |
| 127 | +def remove(name): |
| 128 | + print(f"{name}: {agenda[name]} was removed") |
| 129 | + del agenda[name] |
| 130 | + |
| 131 | +def show(): |
| 132 | + for item in agenda: |
| 133 | + print(f"{item}: {agenda[item]:>5}") |
| 134 | +def update(name, number): |
| 135 | + agenda[name] = number |
| 136 | + |
| 137 | +def main(): |
| 138 | + while True: |
| 139 | + print("Phone Agenda") |
| 140 | + option = input("1: Serach\n2: Insert\n3: Remove\n4: Show\n5: Update\nX: Exit\n\t Option: ") |
| 141 | + if option == '1': |
| 142 | + data = input("Enter a name or number: ") |
| 143 | + search(data) |
| 144 | + if option == '2': |
| 145 | + name = input("Enter a name: ") |
| 146 | + while True: |
| 147 | + number = input("Enter a number: ") |
| 148 | + if not number.isdigit() or len(number) > 10: |
| 149 | + print("Number is not valid, try again") |
| 150 | + else: |
| 151 | + break |
| 152 | + insert(name, number) |
| 153 | + if option == '3': |
| 154 | + name = input("Enter a name: ") |
| 155 | + remove(name) |
| 156 | + if option == '4': |
| 157 | + show() |
| 158 | + if option == '5': |
| 159 | + name = input("Enter a name: ") |
| 160 | + number = int(input("Enter a number: ")) |
| 161 | + update(name, number) |
| 162 | + if option == 'X': |
| 163 | + break |
| 164 | +if __name__ == '__main__': |
| 165 | + main() |
0 commit comments