py_inter · Python

▶ Aprender py_inter (Python) paso a paso · explicación de py_inter

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_inter.py
CódigoSubjectPor qué
def inter(s1: str, s2: str) -> str:def inter(s1: str, s2: str) -> str:Firma del subject.
result = ""returns a stringAcumulo en un string.
for ch in s1:Characters are added in the order they appear in the first string.Recorro s1: así el orden de salida es el de s1.
if ch in s2 and ch not in result:appear in both strings, without repetitionsEstá en s2 (en ambos) y aún no la he añadido (sin repeticiones).
result += chLa añado.
return resultinter("abc", "xyz") => ""Si no hay comunes, queda "".