t | import random | t | import random |
| | | |
| def rnd(a, b=None): | | def rnd(a, b=None): |
| match (a, b): | | match (a, b): |
| case [int(), int()]: | | case [int(), int()]: |
| return random.randint(a, b) | | return random.randint(a, b) |
| case [int(), None]: | | case [int(), None]: |
| return random.randint(0, a) | | return random.randint(0, a) |
| case [float() | int() as x, float() | int() as y]: | | case [float() | int() as x, float() | int() as y]: |
| return (y - x) * random.random() + x | | return (y - x) * random.random() + x |
| case [str() as s, str() as sep]: | | case [str() as s, str() as sep]: |
| parts = s.split(sep) | | parts = s.split(sep) |
| return random.choice(parts) | | return random.choice(parts) |
| case [str() as s, int() as length]: | | case [str() as s, int() as length]: |
| start = random.randint(0, len(s) - length) | | start = random.randint(0, len(s) - length) |
| return s[start:start + length] | | return s[start:start + length] |
| case [str() as s, None]: | | case [str() as s, None]: |
| parts = s.split() | | parts = s.split() |
| return random.choice(parts) | | return random.choice(parts) |
| case [a, int() as cnt] if hasattr(a, '__iter__'): | | case [a, int() as cnt] if hasattr(a, '__iter__'): |
| lst = list(a) | | lst = list(a) |
| return random.choices(lst, k=cnt) | | return random.choices(lst, k=cnt) |
| case [a, None] if hasattr(a, '__iter__'): | | case [a, None] if hasattr(a, '__iter__'): |
| lst = list(a) | | lst = list(a) |
| return random.choice(lst) | | return random.choice(lst) |
| case _: | | case _: |
| raise ValueError | | raise ValueError |