Generate a pattern of '@' and '$' characters. Args: lines: Number of lines to generate in the pattern Returns: A multiline string containing the specified pattern Raises: ValueError: If lines is negative Examples: >>> print(pattern(3)) @@@
(lines: int)
| 16 | |
| 17 | |
| 18 | def pattern(lines: int) -> str: |
| 19 | """ |
| 20 | Generate a pattern of '@' and '$' characters. |
| 21 | |
| 22 | Args: |
| 23 | lines: Number of lines to generate in the pattern |
| 24 | |
| 25 | Returns: |
| 26 | A multiline string containing the specified pattern |
| 27 | |
| 28 | Raises: |
| 29 | ValueError: If lines is negative |
| 30 | |
| 31 | Examples: |
| 32 | >>> print(pattern(3)) |
| 33 | @@@ $ |
| 34 | @@ $$ |
| 35 | @ $$$ |
| 36 | |
| 37 | >>> print(pattern(1)) |
| 38 | @ $ |
| 39 | |
| 40 | >>> print(pattern(0)) |
| 41 | <BLANKLINE> |
| 42 | |
| 43 | >>> pattern(-5) |
| 44 | Traceback (most recent call last): |
| 45 | ... |
| 46 | ValueError: Number of lines must be non-negative |
| 47 | """ |
| 48 | if lines < 0: |
| 49 | raise ValueError("Number of lines must be non-negative") |
| 50 | if lines == 0: |
| 51 | return "" |
| 52 | |
| 53 | pattern_lines = [] |
| 54 | for i in range(lines, 0, -1): |
| 55 | at_pattern = "@" * i |
| 56 | dollar_pattern = "$" * (lines - i + 1) |
| 57 | pattern_lines.append(f"{at_pattern} {dollar_pattern}") |
| 58 | |
| 59 | return "\n".join(pattern_lines) |
| 60 | |
| 61 | |
| 62 | if __name__ == "__main__": |