Dado un número entero N, la tarea es imprimir un número de diamante de tamaño N en estilo rangoli, donde N significa hasta el número N desde el número ‘1’.
Ejemplos:
Input : 2 Output : --2-- 2-1-2 --2-- Input : 3 Output : ----3---- --3-2-3-- 3-2-1-2-3 --3-2-3-- ----3---- Input : 4 Output : ------4------ ----4-3-4---- --4-3-2-3-4-- 4-3-2-1-2-3-4 --4-3-2-3-4-- ----4-3-4---- ------4------
Enfoque:
en primer lugar, imprima desde la primera fila hasta la fila intermedia, y luego imprima desde la fila tras la fila intermedia hasta la última fila. El número total de posiciones en cada fila será 4(N-1) + 1
. El número de filas será 2(N-1) + 1
. La complejidad del tiempo es O(N^2)
.
A continuación se muestra la implementación.
def print_diamond(size): # print the first triangle # (the upper half) for i in range (size): # print from first row till # middle row rownum = i + 1 num_alphabet = 2 * rownum - 1 space_in_between_alphabets = num_alphabet - 1 total_spots = (2 * size - 1) * 2 - 1 total_space = total_spots - num_alphabet space_leading_trailing = total_space - space_in_between_alphabets lead_space = int(space_leading_trailing / 2) trail_space = int(space_leading_trailing / 2) # print the leading spaces for j in range(lead_space): print('-', end ='') # determine the middle character mid_char = (1 + size - 1) - int(num_alphabet / 2) # start with the last character k = 1 + size - 1 is_alphabet_printed = False mid_char_reached = False # print the numbers alternated by '-' for j in range(num_alphabet + space_in_between_alphabets): if not is_alphabet_printed: print(str(k), end ='') is_alphabet_printed = True if k == mid_char: mid_char_reached = True if mid_char_reached == True: k += 1 else: k -= 1 else: print('-', end ='') is_alphabet_printed = False # print the trailing spaces for j in range(trail_space): print('-', end ='') # go to the next line print('') # print the rows after middle row # till last row (the second triangle # which is inverted, i.e., the lower half) for i in range(size + 1, 2 * size): rownum = i num_alphabet = 2 * (2 * size - rownum) - 1 space_in_between_alphabets = num_alphabet - 1 total_spots = (2 * size - 1) * 2 - 1 total_space = total_spots - num_alphabet space_leading_trailing = total_space - space_in_between_alphabets lead_space = int(space_leading_trailing / 2) trail_space = int(space_leading_trailing / 2) # print the leading spaces for j in range(lead_space): print('-', end ='') mid_char = (1 + size - 1) - int(num_alphabet / 2) # start with the last char k = 1 + size - 1 is_alphabet_printed = False mid_char_reached = False # print the numbers alternated by '-' for j in range(num_alphabet + space_in_between_alphabets): if not is_alphabet_printed: print(str(k), end ='') is_alphabet_printed = True if k == mid_char: mid_char_reached = True if mid_char_reached == True: k += 1 else: k -= 1 else: print('-', end ='') is_alphabet_printed = False # print the trailing spaces for j in range(trail_space): print('-', end ='') # go to the next line print('') # Driver Code if __name__ == '__main__': n = 5 print_diamond(n)
Producción:
--------5-------- ------5-4-5------ ----5-4-3-4-5---- --5-4-3-2-3-4-5-- 5-4-3-2-1-2-3-4-5 --5-4-3-2-3-4-5-- ----5-4-3-4-5---- ------5-4-5------ --------5--------