Necesito hacer un patrón de triángulo de triángulo de * dependiendo de la entrada de entero.
Por ejemplo:
n = 2 * *** * * * *********n = 3 * *** ***** * * * *** *** *** *************** * * * * * *** *** *** *** *** *************************Ya descubrí el código para un solo triángulo, pero no sé cómo duplicarlos para que aparezcan como un triángulo de triángulos.
Aquí está mi código para un triángulo:
rows = int(input()) for i in range(rows): for j in range(i, rows): print(" ", end="") for j in range(i): print("*", end="") for j in range(i + 1): print("*", end="") print()Usando una función auxiliar para construir los sub-triángulos:
def tri(n): r = [(s:=(' '*(((2*n-1)-(2*i-1))//2)))+('*'*(2*i-1))+s for i in range(1, n+1)] return r def triangle(n): v = [''.join(j) for i in range(n+1) for j in zip(*[tri(n) for _ in range(2*i-1)])] return '\n'.join((s:=' '*((len(v[-1]) - len(i))//2))+i+s for i in v) for i in range(1, 4): print(triangle(i)) print('-'*25) * ------------------------- * *** * * * ********* ------------------------- * *** ***** * * * *** *** *** *************** * * * * * *** *** *** *** *** ************************* -------------------------Solo otra alternativa con una función para dibujar el triángulo interior y una función principal para imprimir el resultado final.
import sys n = int(sys.argv[1]) def drawtriangle(num_lines): # prepares the inner triagle in a list and return it together with its width (size). size = (2*num_lines)-1 triangle = [] for i in range(num_lines): white_side = num_lines - i - 1 asterisks = 2*i + 1 triangle.append(" "*white_side + "*"*asterisks + " "*white_side) return triangle, size def main(num_lines): tr, tr_size = drawtriangle(num_lines) for j in range(num_lines): for line in tr: white_triangles = n - j - 1 white_size = tr_size * white_triangles line_repeat = (2*j) + 1 print(" "*white_size + line*line_repeat + " "*white_size) main(n)Producción:
* * *** * * * ********* * *** ***** * * * *** *** *** *************** * * * * * *** *** *** *** *** ************************* * *** ***** ******* * * * *** *** *** ***** ***** ***** ********************* * * * * * *** *** *** *** *** ***** ***** ***** ***** ***** *********************************** * * * * * * * *** *** *** *** *** *** *** ***** ***** ***** ***** ***** ***** ***** *************************************************Ya hay muchas respuestas interesantes, pero pensé en agregar una que permita a Python manejar el centrado de cadenas.
def print_fractal(n, char='*'): # Width of single triangle base = 2*n - 1 # Width of overall figure width = base**2 # Lines containing single triangle padded to rectangle of width `base` lines = [f'{(2*line + 1)*char:^{base}}' for line in range(n)] for row in range(n): # Print (2*row + 1) triangle blocks next to each other for line in lines: print(f'{(2*row + 1)*line:^{width}}') >>> print_fractal(3) * *** ***** * * * *** *** *** *************** * * * * * *** *** *** *** *** *************************También se sugiere una solución recursiva, gracias a la inspiración de la respuesta de @Lynn :
def make_fractal(n, depth, block=['*']): if not depth: return block width = (2*n - 1)*max(map(len, block)) lines = [] for row in range(n): for line in block: lines.append(f'{(2*row + 1)*line:^{width}}') return make_fractal(n, depth - 1, lines) >>> for line in make_fractal(3, 2): print(line) * *** ***** * * * *** *** *** *************** * * * * * *** *** *** *** *** ************************* >>> for line in make_fractal(2, 3): print(line) * *** * * * ********* * * * *** *** *** * * * * * * * * * *************************** >>> for line in make_fractal(2, 2, [' . ', '---']): print(line) . --- . . . --------- . . . --- --- --- . . . . . . . . . ---------------------------