| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| def make_tuple(x: int | tuple, n: int) -> tuple: |
| """Convert an integer or a tuple to an n-tuple. |
| |
| Parameters |
| ---------- |
| x : int or tuple |
| Input value which can be an integer or a tuple of n integers. |
| n : int |
| The length of the tuple to return. |
| |
| Returns |
| ------- |
| tuple |
| A tuple of n integers. |
| """ |
| if isinstance(x, tuple) or isinstance(x, list): |
| if len(x) != n: |
| raise ValueError(f"Expected a tuple of length {n}, got {len(x)}") |
| if not all(isinstance(i, int) for i in x): |
| raise ValueError("All elements in the tuple must be integers") |
| return tuple(x) |
|
|
| if not isinstance(x, int): |
| raise TypeError(f"Expected int, got {type(x)}") |
| return (x,) * n |
|
|
|
|
| def make_2tuple(x: int | tuple[int, int]) -> tuple[int, int]: |
| """Convert an integer or a tuple to a 2-tuple. |
| |
| Parameters |
| ---------- |
| x : int or tuple[int, int] |
| Input value which can be an integer or a tuple of two integers with two elements. |
| |
| Returns |
| ------- |
| tuple[int, int] |
| A tuple of two integers. |
| """ |
| return make_tuple(x, 2) |
|
|
|
|
| def make_3tuple(x: int | tuple[int, int, int]) -> tuple[int, int, int]: |
| """Convert an integer or a tuple to a 3-tuple. |
| |
| Parameters |
| ---------- |
| x : int or tuple[int, int, int] |
| Input value which can be an integer or a tuple of three integers with three elements. |
| |
| Returns |
| ------- |
| tuple[int, int, int] |
| A tuple of three integers. |
| """ |
| return make_tuple(x, 3) |
|
|