Advertisement
CodingComputing

Numeric Concentric Squares Pattern

May 18th, 2024 (edited)
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.61 KB | None | 0 0
  1. # Numeric concentric squares pattern by @CodingComputing
  2.  
  3. max_num = 4  # given parameter
  4.  
  5. # Construct the central row
  6. inc_nums = list(range(1, max_num+1))
  7. centre_row = inc_nums[:0:-1] + inc_nums
  8. size = len(centre_row)  # infer the size
  9.  
  10. for num in centre_row:
  11.     row = centre_row.copy()  # Each row is derived from centre row
  12.     # Replace a middle slice by repeated num
  13.     slice_start = max_num - num
  14.     slice_stop = size - slice_start
  15.     row[slice_start:slice_stop] = [num]*(num*2-1)
  16.     print(' '.join([str(item) for item in row]))  # format and print
  17.  
  18. # Follow @CodingComputing for more on Python!
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement