Advertisement
CodingComputing

Square Wave Pattern

Mar 31st, 2024 (edited)
863
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. # *Square Wave Pattern* by @CodingComputing
  2.  
  3. # Parameters
  4. num_cycles = 3  # How many repeating cycles are present?
  5. high_width = 2  # How long is the wave high in each cycle?
  6. low_before_width = 1  # Before the wave moves up to high, how long is it low?
  7. low_after_width = 1  # After the wave moves down from high, how long is it low?
  8. height = 4  # How high does the wave go?
  9.  
  10. # Develop Column-wise representation
  11. # L for Low, H for High, T for Transition
  12. single_cycle_col_repr = 'L'*low_before_width + 'T' + 'H'*high_width + 'T' + 'L'*low_after_width
  13. full_col_repr = single_cycle_col_repr * num_cycles
  14.  
  15. # Form each row and draw!
  16. for row_idx in range(height):
  17.     if row_idx == 0:  # Top row. '*' comes for H or T type columns
  18.         row_items = [(' ' if c=='L' else '*') for c in full_col_repr]
  19.     elif row_idx == height-1:  # Bottom row. '*' comes for L or T type columns
  20.         row_items = [(' ' if c=='H' else '*') for c in full_col_repr]
  21.     else:  # Middle row. '*' comes only T type columns
  22.         row_items = [('*' if c=='T' else ' ') for c in full_col_repr]
  23.     print(''.join(row_items))  # Output the row
  24.  
  25. # Output:
  26. #  ****  ****  ****
  27. #  *  *  *  *  *  *
  28. #  *  *  *  *  *  *
  29. # **  ****  ****  **
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement