|
| 1 | +# --------- Strings |
| 2 | + |
| 3 | +my_string: str = "Soy un String" |
| 4 | + |
| 5 | +# I. Acceso |
| 6 | +print("Acceso:", my_string[5]) |
| 7 | + |
| 8 | +# II. Subcadenas |
| 9 | +my_sub_string = my_string[0:10] |
| 10 | +print("Subcadenas:", my_sub_string) |
| 11 | + |
| 12 | +# III. Longitud |
| 13 | +print("Longitud:", len(my_string)) |
| 14 | + |
| 15 | +# IV. Concatenación |
| 16 | +my_string = my_string + " :)" |
| 17 | +print("Concatenación:", my_string) |
| 18 | + |
| 19 | +# V. Repetición |
| 20 | +print("Repetición:", my_string * 2) |
| 21 | + |
| 22 | +# VI. Recorridio |
| 23 | +print("Recorrido (for each): ", end='') |
| 24 | +for letter in my_string: |
| 25 | + print(letter, end='') |
| 26 | +print() |
| 27 | + |
| 28 | + |
| 29 | +print("Recorrido (while): ", end='') |
| 30 | +counter = 0 |
| 31 | +while counter != len(my_string): |
| 32 | + print(my_string[counter], end='') |
| 33 | + counter = counter + 1 |
| 34 | +print() |
| 35 | + |
| 36 | +# VII. Mayusculas |
| 37 | +print("Conversión a mayusculas:", my_string.upper()) |
| 38 | + |
| 39 | +# VIII. Minisculas |
| 40 | +print("Conversión a minusculas:", my_string.lower()) |
| 41 | + |
| 42 | +# IX. Reemplazo |
| 43 | +my_string = my_string.replace("un String", "una Cadena") |
| 44 | +print("Reemplazo:", my_string) |
| 45 | + |
| 46 | +# X. División |
| 47 | +my_split_string = my_string.split() |
| 48 | +print("División:", my_split_string) |
| 49 | + |
| 50 | +# XI. Union |
| 51 | +my_join_string = " ".join(my_split_string) |
| 52 | +print("Union:", my_join_string) |
| 53 | + |
| 54 | +# XII. Interpolación |
| 55 | +print(f"Interpolación: {my_string}") |
| 56 | + |
| 57 | +# XIII. Verificación |
| 58 | +print("Verificación:") |
| 59 | +print("String" in my_string) |
| 60 | +print(my_string.isdigit()) |
| 61 | +print(my_string.islower()) |
| 62 | +print(my_string.startswith("Soy")) |
| 63 | +print(not my_string.isalpha()) |
| 64 | + |
| 65 | + |
| 66 | +# --------- Extra |
| 67 | +import re |
| 68 | + |
| 69 | + |
| 70 | +def only_letters(string): |
| 71 | + return re.sub(r"[^a-z]", '', string.lower()) |
| 72 | + |
| 73 | + |
| 74 | +def is_palindrome(string): |
| 75 | + return only_letters(string) == string[::-1] |
| 76 | + |
| 77 | + |
| 78 | +def is_anagram(string_1, string_2): |
| 79 | + return sorted(only_letters(string_1)) == sorted(only_letters(string_2)) |
| 80 | + |
| 81 | + |
| 82 | +def is_isogram(string): |
| 83 | + stack: dict = {} |
| 84 | + |
| 85 | + for letter in only_letters(string): |
| 86 | + if letter in stack: |
| 87 | + stack[letter] += 1 |
| 88 | + |
| 89 | + else: |
| 90 | + stack[letter] = 1 |
| 91 | + |
| 92 | + return len(set(stack.values())) == 1 |
| 93 | + |
| 94 | + |
| 95 | +print(is_palindrome("radar")) |
| 96 | +print(is_palindrome("coco")) |
| 97 | + |
| 98 | +print(is_anagram("cola", "loca")) |
| 99 | +print(is_anagram("nona", "nono")) |
| 100 | + |
| 101 | +print(is_isogram("carbon")) |
| 102 | +print(is_isogram("carbonara")) |
0 commit comments