+ 1
Would anyone describe this code logic ?
Would anyone describe this code logic ? https://code.sololearn.com/c6laWgy2dTBm/#py
1 Antwort
+ 6
If you didn't code this program, you are required by SoloLearn to provide a link to where it comes from (or Sololearn profile name) or keep it private.
1 character_set = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 "
2 secret_key = "Dd18Abz2EqNPW hYTOjBvtVlpXaH6msFUICg4o0KZwJeryQx3f9kSinRu5L7cGM"
3 encrypted_message = ""
4 for character in my_string:
5 index = character_set.find(character)
6 encrypted_message = encrypted_message + secret_key[index]
Line 1 lists all characters that have a mapping.
Line 2 lists the mapped characters so all 'a' become 'D'.
Line 3 initializes the result string to empty.
Line 4 loops once for each character in the argument my_string
Line 5 returns the index of the character in the character_set string
Line 6 will crash when a character not listed in character_set is processed as -1 isn't a valid index, otherwise it finds the mapped character in secret_key and adds it to the current encrypted_message string