I have a string of 72 characters where the program validates if it is valid or not. It is valid when the last character is an "S" or an "E", also when 3 "N" are not repeated consecutively.
For example.
Note: The court, but it is understood that it must indeed have 72 characters.
WNEWSWNNNSNEWSWSEWSNSNNNNWENW
That for example is not valid, because it has 3 "N" repeated consecutively and 2 times. And it doesn't end in "S" or "E". The letter N does not necessarily have to be repeated 2 times repeated in 3. It can be more or less, the issue is that the N cannot be repeated 3 times consecutively.
So far, I managed to first validate that it is 72 characters long, and then validate that it ends in "S" or "E". But I don't know how to validate that the letter "N" should not be repeated consecutively 3 times.
robot = str(input("Ingrese una secuencia de movimiento del robot [Único intento]: "))
if(len(robot) == 72):
robot_1 = len(robot)
last_robot = robot[robot_1-1]
if(last_robot == "S" or last_robot == "E"):
secuencia_valida = 1
print("")
print(secuencia_valida)
else:
print("")
secuencia_invalida = 0
print(secuencia_invalida)
else:
print("")
print("Error. Saliendo en 5 segundos...")
time.sleep(5)
That, I really appreciate the help of this community. Stay tuned.
It's much easier than it seems if you just want to discard repetitions of the character
"N"
, just use the membership operator in :"NNN" not in s
will returnTrue
only and only if"NNN"
it is not a substring ofs
. This approximation would not work for you if repetitions of more than 3 "N" were valid, but I deduce that this is not the case. By usingin
"...WNNNNS"
it would also be invalid.Note that Python allows negative indexing,
-1
the last element of the array,-2
the penultimate, and so on.a[-1]
is the last character, you don't have to juggle itlen
to calculate its positive index.