[Community Puzzle] Insert to String

Coding Games and Programming Challenges to Code Better

Send your feedback or ask for help here!

Created by @Flafy,validated by @FredericLocquet,@pluieciel and @gupascal.
If you have any issues, feel free to ping them.

Test case 3 contains a space char at the end of an input line.
I do not think it is a good practice in puzzle data inputs.

2 Likes

Here’s my code in C#

class Solution
{
    static void Main(string[] args)
    {
        string s = Console.ReadLine();
        int line=0, col=0; string addedStr ="";
        int changeCount = int.Parse(Console.ReadLine());
        string finalStr="";
        for (int i = 0; i < changeCount; i++)
        {
            string[] rawChange = Console.ReadLine().Split('|');
            line = int.Parse(rawChange[0]);
            col = int.Parse(rawChange[1]);
            addedStr = rawChange[2];
            for(int j = 0; j < s.Length - 1; j++){
                if(j==col && line==0){
                    finalStr = s.Insert(j, addedStr);  
                }
            }
        }
        Console.WriteLine(finalStr);
    }
}

I know it’s incomplete and the if statement is not “fit” for all the cases (number of lines dosen’t equal to 0), in my output i have this: Hello w world.
Any tip please? I couldn’t even find my mistake looping through my string

1 Like

You may add some debug prints to visualise what may go wrong:

        for (int i = 0; i < changeCount; i++)
        {
            Console.Error.WriteLine(i); // this line
            string[] rawChange = Console.ReadLine().Split('|');
            line = int.Parse(rawChange[0]);
            col = int.Parse(rawChange[1]);
            addedStr = rawChange[2];
            for(int j = 0; j < s.Length - 1; j++){
                if(j==col && line==0){
                    finalStr = s.Insert(j, addedStr);
                    Console.Error.WriteLine(finalStr); // this line
                }
            }
        }

The console will show the following:

0
1
Hello,\n world
2
Hello w world
3
Hello w world

What’s gone wrong? Let’s check one by one:
0: Nothing was output. Why? s.Length is 11 but col is also 11. What will happen?
1: Looks fine, ignoring the above omission
2 & 3: Why has the insertion made in 1 disappeared? Is the code modifying the original string or the edited string?

Hope the above gives you some hints to debug further.

2 Likes

Sure it will, I will do my best to exploit every hint you pointed out, thank’s

@Flafy @FredericLocquet @pluieciel @gupascal
hey guys. i don’t understand the question & need some help clarifying

1: what are suppose to be columns? every letter or every word?
also some hint would be awesome.

thank you :slight_smile:

“Each change includes a line number, a column number, and a string to add at that location.”
Ex : “0|11|!” (first test) you have to put at first string, index 11 the character “!”.

1 Like

here is my code so far. i got the line number, index & new string to add to the main string splited into new vars. all inside the for loop to be cleared out with every count and redeclared with new vals

    let index = Number(rawChange.split("|")[1]) 
    let newStr = rawChange.split("|")[2]
    if(s.length <= index) {
        finalStr = s.concat(newStr)
    } else {
        finalStr = s.slice(0, index).concat(newStr) + s.slice(index)
    }

now here are the parts i don’t know how to go about:
1- how to look for the second line in the “s”
2- in the above code it don’t seem to be recognizing the “\n” as new line but a string and added it to the “s”

if anyone could maybe give me some hints to go forward it be great. thank you in advanced

Assuming you are using JavaScript, does your code split the original string at each “\n” like this? If yes, that may help with both parts in your question.

Code

const s = readline().split("\\n");

1 Like

I use C#
When I do
var Ray = aString.Split(’\n’)
…that will give me an array that splits aString using the character \n

But when I try to do that with s (which is the input variable), it doesn’t work.

Is this a Codingame thing?

The puzzle says ("\n" should be replaced with a newline.)… but I can’t figure out how to do that.

(Sorry for the dumb question and thanks for any help)

when I try to do what with s, it doesn’t work.

you may have to escape the backslash (or even double escape because it’s regex?) if it is one of the chars in String s

Hey. i have some Questions that confuse me so i appreciate anything that helps me clarify them. BTW i’m using JS lang.
1- in the first test we have 4 lines of changes which the first one is to add “!” to the end of the string. but in the answers it doesn’t expects it. here is the list of changes:
Hello world
4
0|11|!
0|5|,\n
0|7| w
0|10|\n

but it’s answers as the test answer says should be this:
Hello,
w worl
d!

2- for the same test second change split the string in 5th char and keeps the first half. but third change answer shows that it split the string and add the rawChange-string to the beginning of the second half and logs the second half. it is very confusing to me which part should we keep?

3- in the second test i saw that the rawChange-string should be added in the middle of the original string and keep both side of it. what is the conditioner i should use to let the code know which half or the whole part should be kept?

4- in the third test on the first change there are “()” added in the end of the answer which was not on the rawChange. where did that come from?

5- on the last test again i don’t see how on the first, second and third char a string is added but the comas before that is not in the beginning of the output?

6- and last how to replace “\n” with a new line in the rawChange-string? should i add the rest of the string as an element to the splitted array?

thank you in advanced and sorry if my questions are dumb but i’m working on this challenge for the past two days and could not figure these out.

Image1

Using the example result, the exercise asks you to apply all 4 changes to the given text at the same time.
All the given indices refer to the indices before any editing.
Annoying. Isn’t it?

1 Like

acually yes it is really annoying and confusing but makes me more interested :smile:

but what i don’t get is how it happens at the same time when the changes are gonna be read with every count of the loop. i think maybe i need more guidance if possible???

My hint is to do it in reverse index order.
Add something to index 11. It extends the length but will not affect your content in index 10.
Do you like this idea?

2 Likes

the idea was pretty nice. thanks. but i believe some there were parts that needed a higher level on understanding programming to implement this. i was not able to see parts even after you mentioned the reverse index order for a day before i started to do it.
i’m a noob yet but i guess the challenge either need more explanations or without it it should be rated medium. but me as a new learner this level of understanding should not be expected.
plus maybe sorting can be labeled on the skills to learn from this challenge too.
what do you think ?

Just tell my opinions:

“Easy” is a suitable class for this puzzle. Many “Medium” puzzles are 10X more difficult than this one. But do not care too much about the difficulty setting. Often it is a subjective label.

The joy of solving puzzles is to overcome challenges. One of the challenges is to discover the untold truth or facts. It should not and need not tell people how to solve it step-by-step.

The list of skill set is often misunderstood. It just serves as some tags for rough classification and a search helper. It is never meant to be comprehensive or authoritative. Players are encouraged to invent alternative approaches to solve problems. Never got yourself constrained by these tags. No need to blame the tags missing your innovative algorithm.

You said you are interested in this puzzle because it is annoyingly difficult. That’s a good attitude to gain advancement.

2 Likes

make sense thanks. for all the help and hints as well.