Ето моя код: https://pastebin.com/3Wcu5HjE
Loading...
+ Нов въпрос
LilianaVelvheva avatar LilianaVelvheva 17 Точки

Къде ми е грешката в задача Password reset и защо judge ми дава 75 точки?

Ето моя код: https://pastebin.com/3Wcu5HjE

Това е условието на задачата:

Programming Fundamentals Final Exam 04.04.2020

Problem 1. Password Reset

Yet again you have forgotten your password... Naturally it`s not the first time this has happened. Actually you got so tired of it that you decided to help yourself with a smart solution.

Write a password reset program that performs a series of commands upon a predefined string. First, you will receive a string and afterwards, until the command "Done" is given, you will be receiving strings with commands split by a single space. The commands will be the following:

  • TakeOdd
    •  Takes only the characters at odd indices and concatenates them together to
      obtain the new raw password and then prints it.
  • Cut {index} {length}
    • Gets the substring with the given length starting from the given index from the password and removes its first occurrence of it, then prints the password on the console.
    • The given index and length will always be valid.
  • Substitute {substring} {substitute}
    • If the raw password contains the given substring, replaces all of its
      occurrences with the substitute text given and prints the result.
    • If it doesn’t, prints "Nothing to replace!"

Input

  • You will be receiving strings until the "Done" command is given.

Output

  • After the "Done" command is received, print:
    • "Your password is: {password}"

Constraints

  • The indexes from the "Cut {index} {length}" command will always be valid.

Examples

Input

Output

Siiceercaroetavm!:?:ahsott.:i:nstupmomceqr

TakeOdd

Cut 15 3

Substitute :: -

Substitute | ^

Done

icecream::hot::summer

icecream::hot::mer

icecream-hot-mer

Nothing to replace!

Your password is: icecream-hot-mer

Comments

TakeOdd

Siiceercaroetavm!:?:ahsott.:i:nstupmomceqr -> icecream::hot::summer

We only take the chars at odd indices 1, 3, 5 etc.

Cut 15  3 -> icecream::hot::summer -> sum

icecream::hot::mer

We cut a substring starting at index 15 with length 3,

remove it from the raw password and print it.

Then, on a new line we print the resulting new raw password.

Substitute :: - -> icecream::hot::summer -> icream-hot-summer

We replace "::" with "-".

Substitute | ^ -> Nothing to replace!

"|" is not found anywhere in the raw password.

Finally, after receiving the "Done" command, we print the resulting password in the proper format.

Input

Output

up8rgoyg3r1atmlmpiunagt!-irs7!1fgulnnnqy

TakeOdd

Cut 18 2

Substitute ! ***

Substitute ? .!.

Done

programming!is!funny

programming!is!fun

programming***is***fun

Nothing to replace!

Your password is: programming***is***fun

JS Input / Output

Input

Output

(["Siiceercaroetavm!:?:ahsott.:i:nstupmomceqr",

"TakeOdd",

"Cut 15 3",

"Substitute :: -",

"Substitute | ^",

"Done"])

icecream::hot::summer

icecream::hot::mer

icecream-hot-mer

Nothing to replace!

Your password is: icecream-hot-mer

Comments

TakeOdd

Siiceercaroetavm!:?:ahsott.:i:nstupmomceqr -> icecream::hot::summer

We only take the chars at odd indices 1, 3, 5 etc.

Cut 15  3 -> icecream::hot::summer -> sum

icecream::hot::mer

We cut a substring starting at index 15 with length 3,

remove it from the raw password and print it.

Then, on a new line we print the resulting new raw password.

Substitute :: - -> icecream::hot::summer -> icream-hot-summer

We replace "::" with "-".

Substitute | ^ -> Nothing to replace!

"|" is not found anywhere in the raw password.

Finally, after receiving the "Done" command, we print the resulting password in the proper format.

Input

Output

(["up8rgoyg3r1atmlmpiunagt!-irs7!1fgulnnnqy",

"TakeOdd",

"Cut 18 2",

"Substitute ! ***",

"Substitute ? .!.",

"Done"])

programming!is!funny

programming!is!fun

programming***is***fun

Nothing to replace!

Your password is: programming***is***fun

 

 

Тагове:
0
Fundamentals Module
Axiomatik avatar Axiomatik 1626 Точки

Most likely issue comes form Substitue operation which requires as per instructions: 

"If the raw password contains the given substring, replaces all of its occurrences with the substitute text given and prints the result."

Without a global regex set in the replace function, replace will only modify the given string at the first occurence of the given substring.

MDN: String.replace() => Note that the function will be invoked multiple times for each full match to be replaced if the regular expression in the first parameter is global.

Test with following input/output to see where problem comes from:

password((['up8rgoyg3r1atmlmpiunagt!-irs7!1fgulnnnqy',
    'TakeOdd',
    'Cut 18 2',
    'Substitute ! ***',
    'Substitute ? .!.',
    'Done',])
);

Output
programming!is!funny
programming!is!fun
programming***is***fun
Nothing to replace!
Your password is: programming***is***fun

 

Demo:

function solve(input) {
    let password = input.shift();

    let line = input.shift();

    while (line !== 'Done') {
        let [command, ...rest] = line.split(' ');

        switch (command) {
            case 'TakeOdd':
                let newPassword = '';

                for (let i = 0; i < password.length; i++) {
                    if (i % 2 !== 0) {
                        newPassword += password[i];
                    }
                }
                password = newPassword;
                console.log(password);
                break;
            case 'Cut':
                let index = Number(rest[0]);
                let length = Number(rest[1]);

                let string = password.substring(index, index + length);
                password = password.replace(string, '');
                console.log(password);
                break;
            case 'Substitute':
                let stringTarget = rest[0];
                let replace = rest[1];
                if (password.includes(stringTarget)) {
                    while (password.includes(stringTarget)) {
                        password = password.replace(stringTarget, replace);
                    }
                    console.log(password);
                } else {
                    console.log('Nothing to replace!');
                }

                break;
        }

        line = input.shift();
    }

    console.log(`Your password is: ${password}`);
}

 

1
svephoto avatar svephoto 1114 Точки

Здравей, LilianaVelvheva,

 

Проверката за това дали паролата съдържа подадената от отвън част не може да се направи така, както ти си я направила. Трябва да бъде

if (!passwords.includes(elementToReplace))

Колегата Axiomatik вече е дал решение на проблема, а аз мога да предложа алтернатива за процеса на заместването със следния израз, който също работи:

passwords = passwords.split(elementToReplace).join(elSubstitute);

1
04/08/2021 13:39:31
LilianaVelvheva avatar LilianaVelvheva 17 Точки

Благодаря за бързите отговори. Помогнахте ми много.

1
svephoto avatar svephoto 1114 Точки

Моля, радвам се. yes

0