Advertisement
CodingComputing

Untitled

Mar 31st, 2024 (edited)
522
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.06 KB | None | 0 0
  1. # *Letters and Number Triangle Pattern* by @CodingComputing
  2. from string import ascii_uppercase as alphas  # Alphabets
  3. from string import digits  # String digits 0...9
  4. natural_nums = digits[1:]  # String digits 1...9, because pattern starts from 1
  5.  
  6. height = 5  # height of the pattern
  7. max_width = 2*height + (2*height-1)
  8. # `height` number of alphabets, plus `height` number of digits,
  9. # plus `2*height-1` number of spaces between characters
  10.  
  11. for row_idx in range(height):
  12.     # Slice starting from row_idx, of (row_idx+1) items ahead
  13.     row_slice = slice(row_idx, 2*row_idx+1)
  14.     row_alphas = alphas[row_slice]  # slice out from alphas
  15.     row_digits = natural_nums[row_slice]  # slice out from digits
  16.     row_chars = row_alphas + row_digits  # characters to be printed in current row
  17.     row_content = ' '.join(list(row_chars))  # insert spaces between chars
  18.     print(f"{row_content:^{max_width}}")  # format to centre at max_width
  19.  
  20. # Output:
  21. #         A 1        
  22. #       B C 2 3      
  23. #     C D E 3 4 5    
  24. #   D E F G 4 5 6 7  
  25. # E F G H I 5 6 7 8 9
  26.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement