The Imitation Game - 50\100
Здравейте,
На първа задача от 01. Programming Fundamentals Final Exam Retake, Judge ми дава Runtime Error на 2 и 5 тест, а 6тия не минава. Направил съм проверка за валидни индекси и за празен стринг от конзолата, но уловката явно е другаде.
Ето моето решение: https://pastebin.com/2m6c5F8n
Линк към Judge: https://judge.softuni.bg/Contests/Practice/Index/2525#0
Условие:
You are a mathematician during world war 2, who has joined the cryptography team to decipher the enemy's enigma code. Your job is to create a program to crack the codes.
On the first line of the input you will receive the encrypted message. After that, until the "Decode" command is given, you will be receiving strings with instructions for different operations that need to be performed upon the concealed message to interpret it and reveal its true content. There are several types of instructions, split by '|'
- Move {number of letters}
- Moves the first n letters to the back of the string.
- Insert {index} {value}
- Inserts the given value before the given index in the string.
- ChangeAll {substring} {replacement}
- Changes all occurrences of the given substring with the replacement text.
Input / Constraints
- On the first line, you will receive a string with message.
- On the next lines, you will be receiving commands, split by '|' .
Output
- After the "Decode" command is received, print this message:
"The decrypted message is: {message}"
Examples
Input |
Output |
zzHe ChangeAll|z|l Insert|2|o Move|3 Decode |
The decrypted message is: Hello |
Comments |
|
ChangeAll|z|l zzHe → llHe (We replace all occurrences of 'z' with 'l') Insert|2|o llHe → lloHe (We add an 'o' before the character on index 2) Move|3 lloHe → Hello (We take the first three characters and move them to the end of the string) Finally, after receiving the "Decode" command, we print the resulting message. |
|
Input |
Output |
owyouh Move|2 Move|3 Insert|3|are Insert|9|? Decode |
The decrypted message is: howareyou? |
Здрасти,
Благодаря за коментарите, доста по-изчистен код се получи. Оказа се, че и моя код минава с 100/100 ако просто променя replaceAll() метода на replace(). Не разбрах точно защо, но важното е, че работи :D
Ами супер, идеално, щом си открил проблема в твоя код и той е проработил коректно.
Иначе относно разликата между двата метода:
Both
replace()
andreplaceAll()
replace all occurrences in the String.replace()
Use
replace()
if you just want to replace somechar
with anotherchar
or someString
with anotherString
(actuallyCharSequence
).replaceAll()
Use
replaceAll()
if you want to use a regular expression pattern.Източник: https://stackoverflow.com/questions/10827872/difference-between-string-replace-and-replaceall
Аз се мъча с тази задача от час и при мен проблемът беше отчно същия. .replaceAll() на .replace() и даде 100/100. А аз се мъчих какво ли не.... :D mind-blowing е тая работа с тоя replaceAll.