py_array_rotation_detector · Python
▶ Aprender py_array_rotation_detector (Python) paso a paso · explicación de py_array_rotation_detector
Código línea a línea
Tres columnas: el código, el trozo del subject o del main que obliga a escribir esa línea, y el por qué.
py_array_rotation_detector.py
| Código | Subject | Por qué |
|---|---|---|
def array_rotation_detector(arr1: list, arr2: list) -> bool: | def array_rotation_detector(arr1: list, arr2: list) -> bool: | Firma del subject. Listas de cualquier cosa, no solo enteros. |
if len(arr1) != len(arr2): | If the arrays have different lengths, they cannot be rotations of each other. | Longitudes distintas: imposible. |
return False | No. | |
for shift in range(len(arr1)): | A rotation means that the elements are shifted circularly. | Pruebo todos los desplazamientos posibles, de 0 a len-1. Rotar a la izquierda s es rotar a la derecha len - s: con un sentido cubro los dos. |
if arr1[shift:] + arr1[:shift] == arr2: | determines if the second list is a rotation of the first list (left or right) | arr1 rotado shift a la izquierda. Comparo listas enteras con ==. |
return True | Encontrada. | |
return not arr1 | Two empty arrays are considered rotations of each other. | Ninguna rotación coincide: False. Salvo si arr1 está vacía (el bucle no entra): dos vacías son rotación → True. |