2023-08-20 11:41:07 +00:00
|
|
|
from typing import Iterable, TypeVar
|
2023-08-18 22:34:02 +00:00
|
|
|
|
2023-08-20 11:41:07 +00:00
|
|
|
T = TypeVar("T")
|
2023-08-18 22:34:02 +00:00
|
|
|
|
2023-08-20 11:41:07 +00:00
|
|
|
|
|
|
|
def _rotate_r(__s: list[T]) -> list[T]:
|
|
|
|
return [__s[-1]] + __s[0:-1]
|
|
|
|
|
|
|
|
|
|
|
|
def _valid_rotate(__s: Iterable[str], __c: Iterable[str]) -> bool:
|
2023-08-18 22:34:02 +00:00
|
|
|
for sc, cc in zip(__s, __c):
|
|
|
|
# can never rotate a "1" onto an "x"
|
|
|
|
if sc == "x" and cc == "1":
|
|
|
|
return False
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2023-08-20 11:41:07 +00:00
|
|
|
def _fix_exes(__s: Iterable[str], __c: Iterable[str]) -> list[str]:
|
|
|
|
result = []
|
2023-08-18 22:34:02 +00:00
|
|
|
|
|
|
|
for sc, cc in zip(__s, __c):
|
|
|
|
if sc == "x":
|
2023-08-20 11:41:07 +00:00
|
|
|
result.append("x")
|
2023-08-18 22:34:02 +00:00
|
|
|
|
|
|
|
elif cc == "x":
|
2023-08-20 11:41:07 +00:00
|
|
|
result.append("0")
|
2023-08-18 22:34:02 +00:00
|
|
|
|
|
|
|
else:
|
2023-08-20 11:41:07 +00:00
|
|
|
result.append(cc)
|
2023-08-18 22:34:02 +00:00
|
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
2023-08-20 11:41:07 +00:00
|
|
|
def rotate_r(__s: list[str]) -> list[str]:
|
2023-08-18 22:34:02 +00:00
|
|
|
result = _rotate_r(__s)
|
|
|
|
|
|
|
|
while not _valid_rotate(__s, result):
|
|
|
|
result = _rotate_r(result)
|
|
|
|
|
|
|
|
return _fix_exes(__s, result)
|