Created
April 2, 2024 13:54
-
-
Save andersonbosa/820671dab800fee8cda7545afbee4028 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
# 5 seconds | |
- return the smallest number you can get by reduction | |
# CONSTRAINS | |
# RULES | |
1. The method is: Only the letters a, b, and c will be given | |
in str and you must take two different adjacent characters | |
and replace it with the third | |
2. RETURNS: the length of the resulting string is to be outputted. | |
''' | |
def StringReduction(s): | |
# Converte a string em uma lista de caracteres | |
s = list(s) | |
# Define um conjunto de caracteres possíveis | |
char_set = {'a', 'b', 'c'} | |
# Variável para controlar se a redução da string é repetida | |
repeat = True | |
# Loop principal para reduzir a string até que não seja mais possível | |
while repeat: | |
i = 0 | |
repeat = False | |
# Loop para percorrer a string | |
while i < len(s) - 1: | |
# Verifica se dois caracteres consecutivos são diferentes | |
if s[i] != s[i + 1]: | |
# Substitui os dois caracteres por um terceiro caractere ausente na sequência | |
char1 = s[i] | |
char2 = s[i + 1] | |
charNew = list(char_set - {char1, char2}) | |
s[i:i + 2] = charNew | |
print(">> {s}", char1, char2, charNew) | |
repeat = True | |
else: | |
i += 1 | |
# Retorna o comprimento da string resultante | |
return len(s) | |
x = "abcabc" | |
print( | |
f"{x}: {StringReduction(x)}" | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment