Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # *Asterisk Pattern* solution by @CodingComputing
- size = 7 # given parameter, expected to be ODD
- center_idx = size // 2 # get the index of the central row/column
- for row_idx in range(size): # to construct each row and print
- if row_idx == center_idx: # if the row is the central row...
- row_chars = ["*"] * size # print all *s for the central row
- else: # for a non-centre row...
- row_chars = [" "] * size # initialization, we'll fill * where needed
- for col_idx in range(size): # for columns within the current row...
- # check if we are on any of the diagnoals
- is_on_descending_diagonal = col_idx==row_idx
- is_on_ascending_diagonal = col_idx==(size-1-row_idx)
- # -1 is needed because row_idx and col_idx are zero indexed
- is_on_centre_col = col_idx==center_idx # check if on the central column
- if (
- is_on_ascending_diagonal or
- is_on_descending_diagonal or
- is_on_centre_col
- ): # Any of these conditions
- row_chars[col_idx] = '*' # change the initialized space to *
- print(" ".join(row_chars)) # join on a space to get separation
- # Output:
- #
- # * * *
- # * * *
- # * * *
- # * * * * * * *
- # * * *
- # * * *
- # * * *
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement