+ 1

[Solved] Sololearn "The Spy Life" Code Coach Exercise

The code I wrote as a solution for the exercise below successfully reverses the input, but only removes some of the non-letters and non-spaces, and leaves in some digits remaining in it's output.. especially when there are two consecutive characters that need to be removed from the input.. my code removes one and leaves the other to character to remain as part of the output. Can you pinpoint why? Please find below: - the exercise - my code solution - a case where my solution did not work Code Coach Exercise: " You are a secret agent, and you receive an encrypted message that needs to be decoded. The code that is being used flips the message backwards and inserts non-alphabetic characters in the message to make it hard to decipher. Task: Create a program that will take the encoded message, flip it around, remove any characters that are not a letter or a space, and output the hidden message. Input Format: A string of characters that represent the encoded message. Output Format: A string of character tha

23rd Apr 2023, 2:54 PM
Waseem Haidar
Waseem Haidar - avatar
6 Antworten
+ 7
Waseem Haidar , the issue of your code is that you remove elements from the *characters list* during a for loop. this creates confusion with the indexes that are used internally during iteration. a possible solution is *not to delete* any elements during iteration, but creating a new output string during the iteration.
23rd Apr 2023, 3:29 PM
Lothar
Lothar - avatar
+ 7
Waseem Haidar , > you can create a new list with all valid characters and finally convert the list to a string for output or > you can create a new string directly with all valid characters for output
23rd Apr 2023, 3:39 PM
Lothar
Lothar - avatar
+ 5
Please share your code in separate file on code playground to read it properly
23rd Apr 2023, 2:56 PM
Hasnain [ACTIVE CHALLENGER]
Hasnain [ACTIVE CHALLENGER] - avatar
23rd Apr 2023, 3:12 PM
Waseem Haidar
Waseem Haidar - avatar
+ 1
Lothar so ideally, creating a code that copies the required elements into a new list should solve the issue? I will try that now
23rd Apr 2023, 3:35 PM
Waseem Haidar
Waseem Haidar - avatar
23rd Apr 2023, 4:47 PM
Waseem Haidar
Waseem Haidar - avatar