py_hidenp
Código comentado línea a línea y paso a paso:
Entregas: py_hidenp.py con hidenp(small, big). Rank 03 · nivel 3.
Qué pide (en palabras normales)
- ¿Aparecen las letras de
smalldentro debig, en orden, aunque con cosas en medio?"sing"en"subsequence testing"→ True. - Distingue mayúsculas.
smallvacío → True.
Subject (enunciado original)
Write a function that checks if the string 'small' is a subsequence
of 'big'. A subsequence means all characters of 'small' appear in 'big'
in the same order, but not necessarily consecutively.
Function is case-sensitive.
Expected file: py_hidenp.py
Function signature:
def hidenp(small: str, big: str) -> bool:
Examples:
hidenp("abc", "a1b2c3")
=> True
hidenp("ace", "abcde")
=> True
hidenp("aec", "abcde")
=> False
hidenp("", "abc")
=> True
hidenp("abc", "ab")
=> False
hidenp("aaaa", "aaa")
=> False
hidenp("sing","subsequence testing")
=> True
Solución: un puntero
i = 0
for ch in big:
if i < len(small) and ch == small[i]:
i += 1
return i == len(small)
Recorro big y cada vez que veo la letra que estoy esperando, paso a la siguiente. Si al final las esperé todas, es subsecuencia. Lineal, una pasada, sin recursión.
Trampas
i < len(small)antes desmall[i]: sin eso, cuando ya encontraste todo y sigue habiendo letras enbig,IndexError.- No uses
inpor letra (all(c in big for c in small)): ignora el orden y las repeticiones."aaaa"en"aaa"daría True y es False.