Temperatures puzzle discussion


#607

Bonjour, je suis encore débutant en C# et je ne comprend pourquoi mon code me renvoie 0 alors qu’il devrait me renvoyer 1 pour le premier test. Si quelqu’un pouvait m’éclairer s’il vous plait, ce serait très gentil. Merci

if (t<0)
{
Tableau[i]= -1*t;
}
else
{
Tableau[i]= t;
}

        Array.Sort(Tableau);

Console.WriteLine(Tableau[0]);

En fait ici j’essaie de changer les nombres négatif en positif et du coup avoir un tableau que d’entiers positif.
Je sais que ce n’est pas la bonne voie pour résoudre cette exercice mais je deviens fou a ne pas comprendre pourquoi il continue a me retourner 0 alors que si je fais juste:

Tableau[i]=t;
Array.Sort(Tableau);
Console.WriteLine(Tableau[0]);

il me renvoie bien -8, pour le premier test.

Help please


#608

@adrienheral @RileyJin
Use the standard debug to analyse your errors. Print variables at different stages of your algorithm and I’m sure you’ll find your mistake in no time


#609

Hi _CG_Thibaud, I’d love to use the standard debug system but i didn’t find it. I tried using the “Console.Error.WriteLine(“Debug messages…”);” but, as i suspected, it just pops the message “Debug messages” up.
Can you tel me how to use the debug system on codingame please, i’ve seek online with no sucess.

Anyway i’m still stuck here with a code that do something it is not suppose to (return 0 instead of 1).

It seems like evrytime i try to transform a negative into a positive number, it returns 0. Why the hell?!

Here is my code:

for (int i = 0; i < n; i++)
{
int t = int.Parse(inputs[i]);

        if (t<0)
        {
       
            Tableau[i]= -1*t;
        }
        else
        {
            Tableau[i]= t;
        }
       
        Array.Sort(Tableau);

Console.WriteLine(Tableau[0]);

so with 1, -2, -8, 4, 5 as inputs it should return 1, right?


#610

I don’t code in C# but a quick (quicker than the time you took to write your message) google search gave me this:

Console.Error.WriteLine("Generating multiples of numbers from {0} to {1}", increment + 1, increment + 10);

So I’d try using something like that


#611

Well as i said in my last comment i’m new in C#, so i’m sorry but i don’t see how this is suppose to help me.
I’ve tried using it, but i think i miss something.

Thanks anyway for trying to help.


#612

I’m new in C# too, you know.

Ok, I went and try this

int t = int.Parse(inputs[i]); // a temperature expressed as an integer ranging from -273 to 5526
Console.Error.WriteLine("temp {0}", t);

and here’s what I got in the console:

temp 1
temp -2
temp -8
temp 4
temp 5
result

How does this sound?


#613

Ok, so this shows me all the value of “t” during the loop. It could be usefull if i had no idea what values were input in the code.

Thanks for that tips, it’ll definitly be usefull on other circumstances. But here its not helping.

Just to be clear, i’m not here to get the solution of the exercise (i found it online). But to understand why my code keeps returning me 0.

Thanks again for trying :wink:


#614

I think it can be useful here :wink:

Now that you know how to use the standard error, why don’t you check the values in your array “tableau” before and after sorting?
It could help to understand why it returns 0.


#615

Thanks dude, it actually did help me figure out why it was returning 0.
The reason is that it was puting 0 as an input in my array “Tableau”.
So when i “Sort” it, the first value in Tableau is 0, ok that make sense.
But why the hell is there a 0 in my array.

Its not doing it with only negative numbers, its really confusing.
Any idea why?


#616

When you initialize an array with a specific length in C#, every field will have it’s standard value. So if you, for example, initialize a new Array like this:
int[] table = new int[4];
It will look like this:
{ 0, 0, 0, 0 }
If you go into your for-loop and table[0] = 1;
Then your array will be { 1, 0, 0, 0 }
If you sort this, it is going to be { 0, 0, 0, 1 }
If you output table[0], it will return 0, because your 1 will be at position table[3] :wink:


#617

Yes i get that, thank you.

That is why i used “n” for my array:

int[] Tableau = new int[n];

“n” is the number of temperatur that i will have in my array

So evry single case in the array should have their own value of the input, none of them is 0.

And as i said its doing it only with positive numbers.

For exemple:
i have 5 temperatures, so it should look like this {1, 5, -8, -2, -5}
but here it is adding a 0 {1, 5, -8, -2, -5, 0} and that doesn’t make any sense for me


#618

Try printing in debug all the content of your “Tableau” at every step of your algorithm.
You’ll see your mistake.


#619

Finally found why!

It is the Array.Sort(Tableau) that was inside the loop, when i put it out of the loop it stopped adding 0 in the Array.
I don’t know why but its fixed now.

Thank you for your help guys :wink:


#620

Well if you sort the array every time you add a value in it, you might overwrite values. Ex:

adding temp -1
[-1, 0, 0]
sorting
[0,0,-1]
adding temp -2
[0,-2, -1]
sorting
[0, -1, -2]
adding temps -3
[0, -1, -3]
sorting
[0,-1,-3]


#621

Hi,

I am doing this puzzle in python3. I can’t seem to understand where the input for temperatures is.

I tried to print i and t, both output “5”. I tried printing i and t in a loop, but to no avail. Where is the value fo rthe temperatures?


#622

Bonjour,
Pour ce puzzle, tous les tests passent mais je n’ai qu’un score de 63%.
Quand je soumets mon code, 3 nouveaux tests apparaissent en rouge mais n’apparaissaient pas à l’origine.
Est-ce normal ?
Merci,
David.


#623

DURING the same loop as you read the temperatures (t), you may either store the values in a list for later processing, or process the values as you read them.

If you do nothing during the reading loop and try to print out the variables AFTERWARDS, you only get the last values of the variables of i and t because they will be kept overwritten by the newer values reached by i / assigned to t.


#624

Hello David,

c’est expliqué juste au dessus des tests en rouge:

Les validateurs suivants diffèrent de ceux présents dans l’IDE pour éviter les solutions codées en dur. C’est pour cette raison que certains d’entre eux peuvent échouer même si la totalité des tests de l’IDE passaient.


#625

Merci Thibaud,

Du coup, pour avoir 100%, il faut que mon code soit conforme aux 3 tests en rouge ?
Genre, je dois coder pour que ces 3 tests passent ?


#626

Il faut et il suffit que tous les tests des validateurs passent.