py_array_rotation_detector

Código comentado línea a línea y paso a paso:

Entregas: py_array_rotation_detector.py con array_rotation_detector(arr1, arr2). Rank 04 · nivel 1.

Qué pide (en palabras normales)

Subject (enunciado original)
Write a Python function that takes two lists (arrays) as parameters and determines if the second list is a rotation of the first list (left or right).

A rotation means that the elements are shifted circularly. For example, shifting [1, 2, 3] to the right by one position results in [3, 1, 2].

The function must return True if arr2 is a rotation of arr1, and False otherwise.
If the arrays have different lengths, they cannot be rotations of each other.
Two empty arrays are considered rotations of each other.

Expected file: py_array_rotation_detector.py
Function signature:
def array_rotation_detector(arr1: list, arr2: list) -> bool:
Forbidden: collections.deque.rotate()

Examples:
array_rotation_detector([1, 2, 3, 4, 5], [4, 5, 1, 2, 3])
=> True
array_rotation_detector([1, 2, 3, 4, 5], [5, 1, 2, 3, 4])
=> True
array_rotation_detector([1, 2, 3], [3, 2, 1])
=> False
array_rotation_detector([1, 2], [1, 2, 3])
=> False
array_rotation_detector([], [])
=> True

Solución: probar todos los cortes

for shift in range(len(arr1)):
    if arr1[shift:] + arr1[:shift] == arr2:
        return True
return not arr1

Corto arr1 por cada posición y pego las dos partes al revés: eso son todas las rotaciones posibles. Izquierda y derecha dan el mismo conjunto, así que un sentido basta. O(n²) en el peor caso; para listas de examen sobra.

El truco de una línea

return len(arr1) == len(arr2) and (not arr1 or arr2 in [arr1[i:] + arr1[:i] for i in range(len(arr1))])

Lo mismo comprimido. Y para strings existe el clásico s2 in s1 + s1; con listas no hay in de sublista, de ahí el bucle.

Trampas