Locker code cipher
EasyOnline Assessment
Scramble a locker-room message with a keyword cipher that treats letters and digits differently.
The cipher walks through text one character at a time:
- A letter (
a-zorA-Z) moves forward in the alphabet, wrapping fromztoa, and keeps its case. The distance is set bykeyword(all lowercase): the first letter of the text uses the value ofkeyword[0], the second letter useskeyword[1], and so on, starting again atkeyword[0]after the last one. The value of a keyword letter is its alphabet position (a= 1,b= 2, ...,z= 26). - A digit moves forward through
0-9, wrapping from9to0. It moves by the distance of the most recent letter before it in the text (that letter's value, taken modulo 10). If no letter has appeared yet, it moves bystart_shift. - Any other character (spaces, punctuation) is copied unchanged.
Only letters use up keyword positions: digits and other characters never advance the keyword.
Input: text, the message; keyword, a non-empty string of lowercase letters; start_shift, the digit shift used before the first letter.
Output: the scrambled string.
Example 1
- Input:
- ["Ab1","bc",4]
- Output:
- "Ce4"
- Explanation
Ais the first letter, so it moves byb= 2 and becomesC.bis the second letter and moves byc= 3, becominge. The digit1followsb, so it moves by 3 and becomes4. ResultCe4.
Example 2
- Input:
- ["7-x9 Q","d",5]
- Output:
- "2-b3 U"
- Explanation
- The
7comes before any letter, so it moves bystart_shift= 5 and becomes2. The-is unchanged.xmoves byd= 4 and becomesb; the keyword has one letter, so every letter uses it. The9followsxand moves by 4, wrapping to3. The space is unchanged andQmoves by 4 toU. Result2-b3 U.
Example 3
- Input:
- ["Zz 99, a8!","yz",2]
- Output:
- "Yz 55, z3!"
- Explanation
Zusesy= 25 and becomesY; the second letterzusesz= 26, a full lap, and staysz. The two 9s follow thatz, so each moves by 26 mod 10 = 6 and becomes5. The space and comma are unchanged. The third letterauses the keyword from the start again (y= 25) and becomesz. The8moves by 25 mod 10 = 5 and wraps to3. ResultYz 55, z3!.
Constraints
0 <= text.length <= 2001 <= keyword.length <= 26, lowercase letters only0 <= start_shift <= 9textcontains only printable ASCII characters