This level is moderate. CoCo is buggy sometimes. The thing is, CoCo mentioned that find() and split() are valid while they actually are not :\
Fix: forget about those methods as this is game python, not real. You want to manually find the colon and split the string by yourself by slicing.
def decode(message):
alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
result = ""
colonIndex = 0
for i in range(len(message)):
if message[i] == ":":
colonIndex = i
break
encrypted = message[:colonIndex]
shiftChar = message[colonIndex + 1]
shift = int(shiftChar)
# Rest of implementation not shown
Since find() don’t work either, you will need a different logic. The code idea is:
Loops through each character of the encrypted part:
If it’s a space, it just adds the space to the result.
If it’s a letter, it finds that letter in the alphabet string.
It subtracts the shift from the letter’s position to decode it.
If the shift goes below index 0 (like shifting 'A' by 3), it wraps around to the end of the alphabet using the mod.
The decoded letter is added to the final result.
Using a while-loop is pretty simple and can just bruteforce the problem, even though it is not the most efficient, but it will get the job done.
index = 0
# Loop through each character in the encrypted message
while index < len(encrypted):
char = encrypted[index] # Get the current character
if char == " ":
result += " " # If it's a space, just add it as-is
else:
i = 0
# Loop through the alphabet to find the position of the current letter
while i < len(alphabet):
if alphabet[i] == char:
# Calculate the new index after shifting left
newIndex = (i - shift) % 26 # % 26 wraps around if it goes below 0
result += alphabet[newIndex] # Add the decoded letter to the result
break # Exit the inner loop once the match is found
i += 1
index += 1 # Move to the next character in the encrypted message
# Rest of code implementation not shown
It should be working now, but do note that CoCo has bugs, so you would need to submit multiple times to get a good seed. Sometimes it doesn’t work (even though it would).