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ódigo | Subject | Por qué |
|---|---|---|
def inter(s1: str, s2: str) -> str: | def inter(s1: str, s2: str) -> str: | Firma del subject. |
result = "" | returns a string | Acumulo 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 repetitions | Está en s2 (en ambos) y aún no la he añadido (sin repeticiones). |
result += ch | La añado. | |
return result | inter("abc", "xyz") => "" | Si no hay comunes, queda "". |