Skip to content

Commit 164db45

Browse files
authored
Merge pull request mouredev#7326 from Jesusway69/main
mouredev#48 - Python
2 parents 5f91b82 + 7439760 commit 164db45

File tree

1 file changed

+196
-0
lines changed

1 file changed

+196
-0
lines changed
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
import os, platform, random
2+
3+
if (platform.platform().startswith("macOS") or platform.platform().startswith("Linux")):
4+
os.system('clear')
5+
else:
6+
os.system('cls')
7+
8+
9+
""" * EJERCICIO:
10+
* ¡Ha comenzado diciembre! Es hora de montar nuestro
11+
* árbol de Navidad...
12+
*
13+
* Desarrolla un programa que cree un árbol de Navidad
14+
* con una altura dinámica definida por el usuario por terminal.
15+
*
16+
* Ejemplo de árbol de altura 5 (el tronco siempre será igual):
17+
*
18+
* *
19+
* ***
20+
* *****
21+
* *******
22+
* *********
23+
* |||
24+
* |||
25+
*
26+
* El usuario podrá seleccionar las siguientes acciones:
27+
*
28+
* - Añadir o eliminar la estrella en la copa del árbol (@)
29+
* - Añadir o eliminar bolas de dos en dos (o) aleatoriamente
30+
* - Añadir o eliminar luces de tres en tres (X) aleatoriamente
31+
* - Apagar (*) o encender (+) las luces (conservando su posición)
32+
* - Una luz y una bola no pueden estar en el mismo sitio
33+
*
34+
* Sólo puedes añadir una estrella, y tantas luces o bolas
35+
* como tengan cabida en el árbol. El programa debe notificar
36+
* cada una de las acciones (o por el contrario, cuando no
37+
* se pueda realizar alguna)."""
38+
39+
def create_tree(hight:int)->list:
40+
if hight <= 3:
41+
print("No se puede crear un árbol de menos de 4 alturas")
42+
return
43+
tree = []
44+
base = hight * 2 - 1
45+
branch = 1
46+
for i in range(hight):
47+
tree.append([])
48+
[tree[i].append(' ') for j in range(base // 2)]
49+
[tree[i].append('*') for k in range(branch)]
50+
[tree[i].append(' ') for l in range(base // 2)]
51+
base -= 2
52+
branch += 2
53+
for m in range (1, 3):
54+
tree.append([])
55+
[tree[i + m].append(' ') for n in range(hight - 2)]
56+
[tree[i + m].append('|') for o in range(3)]
57+
[tree[i + m].append(' ') for p in range(hight - 2)]
58+
59+
return tree
60+
61+
def show_tree(tree:list):
62+
if tree == None:
63+
return
64+
print()
65+
for row in tree:
66+
for column in row:
67+
print(column, end='')
68+
print()
69+
70+
def top_star(tree:list, switch:bool)->list:
71+
if tree == None:
72+
print("ERROR: Hay que crear un árbol antes de poder modificarlo (opción 1)")
73+
return
74+
if switch:
75+
star = ['@' if x == '*' else x for x in tree[0]]
76+
print("\n--- ESTRELLA AÑADIDA ---")
77+
else:
78+
star = ['*' if x == '@' else x for x in tree[0]]
79+
print("\n--- ESTRELLA ELIMINADA ---")
80+
tree[0] = star
81+
return tree
82+
83+
def add_balls(tree:list)->list:
84+
if tree == None:
85+
print("ERROR: Hay que crear un árbol antes de poder modificarlo (opción 1)")
86+
return
87+
i=0
88+
while i !=2:
89+
branch = random.randint(1, len(tree)-3)
90+
ball = random.randint(0, len(tree[branch]))
91+
if tree[branch][ball-1] != '*':
92+
continue
93+
else:
94+
tree[branch][ball-1] = 'o'
95+
i+=1
96+
print("\n--- SE AÑADEN 2 BOLAS DE ADORNO ---")
97+
return tree
98+
99+
def remove_balls(tree:list):
100+
if tree == None:
101+
print("ERROR: Hay que crear un árbol antes de poder modificarlo (opción 1)")
102+
return
103+
for row in range(1, len(tree)-2):
104+
for column in range(len(tree[row])):
105+
if tree[row][column] == 'o':
106+
tree[row][column] = '*'
107+
print("\n--- BOLAS DE ADORNO ELIMINADAS ---")
108+
return tree
109+
110+
def add_ligths(tree:list)->tuple:
111+
if tree == None:
112+
print("ERROR: Hay que crear un árbol antes de poder modificarlo (opción 1)")
113+
return None, None
114+
i=0
115+
lights_position = []
116+
while i !=3:
117+
branch = random.randint(1, len(tree)-3)
118+
light = random.randint(0, len(tree[branch]))
119+
if tree[branch][light-1] != '*':
120+
continue
121+
else:
122+
tree[branch][light-1] = 'X'
123+
lights_position.append(branch)
124+
lights_position.append(light-1)
125+
i+=1
126+
print("\n--- AÑADIDAS 3 LUCES ENCENDIDAS AL ÁRBOL ---")
127+
return tree, lights_position
128+
129+
def turn_on_lights(tree:list, coordinates:list)->list:
130+
if tree == None:
131+
print("ERROR: Hay que crear un árbol antes de poder modificarlo (opción 1)")
132+
return
133+
for pos in coordinates:
134+
tree[pos[0]][pos[1]], tree[pos[2]][pos[3]], tree[pos[4]][pos[5]] = 'X', 'X', 'X'
135+
print("\n--- TODAS LAS LUCES ENCENDIDAS ---")
136+
return tree
137+
138+
def turn_off_lights(tree:list)->list:
139+
if tree == None:
140+
print("ERROR: Hay que crear un árbol antes de poder modificarlo (opción 1)")
141+
return
142+
for row in range(1, len(tree)-2):
143+
for column in range(len(tree[row])):
144+
if tree[row][column] == 'X':
145+
tree[row][column] = '*'
146+
print("\n--- TODAS LAS LUCES APAGADAS ---")
147+
return tree
148+
149+
coordinates = []
150+
tree = None
151+
while True:
152+
print("""
153+
154+
1- Crear árbol
155+
2- Añadir estrella
156+
3- Eliminar estrella
157+
4- Añadir 2 bolas aleatoriamente
158+
5- Quitar todas las bolas
159+
6- Añadir 3 luces aleatoriamente
160+
7- Encender las luces
161+
8- Apagar las luces
162+
163+
""")
164+
option = input("Selecciona una opción del 1 al 6 (enter para salir): ")
165+
166+
match option:
167+
case "1":
168+
hight = input("Indroduzca la altura del árbol a crear: ")
169+
if not hight.isdigit():
170+
print("ERROR: la altura del árbol debe ser un valor numérico")
171+
continue
172+
tree = create_tree(int(hight))
173+
print(f"--- ARBOL DE {hight} ALTURAS CREADO ---")
174+
show_tree(tree)
175+
case "2":
176+
show_tree(top_star(tree, True))
177+
case "3":
178+
show_tree(top_star(tree, False))
179+
case "4":
180+
show_tree(add_balls(tree))
181+
case "5":
182+
show_tree(remove_balls(tree))
183+
case "6":
184+
tree, position = add_ligths(tree)
185+
coordinates.append(position)
186+
show_tree(tree)
187+
case "7":
188+
show_tree(turn_on_lights(tree, coordinates))
189+
case "8":
190+
show_tree(turn_off_lights(tree))
191+
case "":
192+
break
193+
case _:
194+
print("ERROR: sólo se pueden introducir números del 1 al 8 o enter para salir")
195+
196+

0 commit comments

Comments
 (0)