Temperatures puzzle discussion

Merci de vos réponse bonne journée a vous :wink:

Bonjour,

J’ai également un problème avec ce puzzle, en Java cette fois, sur le dernier test (n°6) car j’utilise une lambda qui visiblement n’est pas bien géré par la version de Java 8 installée chez CodinGame (très en retard : https://www.codingame.com/faq Java Oracle Java 1.8.0_72 !!!

Your puzzle’s description starts with “This puzzle is can also be”. It might be better to say “This puzzle is…” or “This puzzle can be…”

1 Like

Can someone help this dont work at all. From where I see only last case shouldn’t be working
int[] tempsInteger = new int[n];
for(int c=0; c < n; c++)
{
tempsInteger[c] = Convert.ToInt32(temps[c].ToString());
}
int resultPositive = 5526;
int resultNegative = -273;
for (int c = 0; c < n; c++)
{
if(temps[c] >= 0 && resultPositive > temps[c])
{
resultPositive = temps[c];
}
else if(temps[c] < 0 && resultNegative < temps[c])
{
resultNegative = temps[c];
}
}
int absoluteNegative = Math.Abs(resultNegative);
if (resultPositive == 0)
Console.WriteLine(resultPositive);
else if(absoluteNegative < resultPositive)
Console.WriteLine(resultNegative);
else if (absoluteNegative >= resultPositive)
Console.WriteLine(resultPositive);

Hi Kal-El,
What exact issue do you have?
May be the problem in that you are trying to convert variable temps from string to int and save it into variable tempsInteger that actually isn’t used, you are still working in if statements with temps.
For example, if you will read the input as follow
int[] temps = Array.ConvertAll(Console.ReadLine().Split(null), int.Parse);
all test cases except last one will pass.

[JAVA]
Bonsoir :slight_smile: Je n’arrive pas à comprendre cette ligne (N°16):

String temps = in.nextLine(); // the n temperatures expressed as integers ranging from -273 to 5526

les températures sont des entiers, donc pourquoi on nous les envoie en String, qui ne pourront pas être comparé numériquement ?

A toi de les splitter et de les tranformer en entier.

Can someone give the inputs and the outputs of test cases “-273 alone” and “5526 alone”.

-273 and 5526

my solution didn’t work with those two as well. I had an “off by one” error in my code, the last temperature was not evaluated.
Sadly the only testcase where this fails, is at the “-273” and “5526” because in no other test the last temperature is the nearest to zero.

c# I don’t know what going wrong here.
Test 2 output is -1
Test 3 and 5 output is -

class Solution
{
 static void Main(string[] args)
  {
    int n = int.Parse(Console.ReadLine()); // the number of temperatures to analyse
    string temps = Console.ReadLine(); // the n temperatures expressed as integers ranging from -273 to 5526

int[] aTemps = temps.Split().Select(int.Parse).ToArray();
int what, diff;
int best = 5526;
for (int i = 0; i < n; i++)
    {what = aTemps[i];
     diff = Math.Abs(what - 0);
     Console.Error.WriteLine(what);
  
  if (diff < best)
        {best=what;
        }  
      
  if (diff == best )
  { if(what> 0)
      {best= what;
      }
  }
        
    }

    Console.WriteLine(best);
}

}

What happens when best is -5 and what is 5?

Also, what happens if n is 0 ?

The output being - is just the validator doing its thing… it doesn’t show you your whole output, just your output up to the point where it failed. The first character of the answer is not - so it stopped there.

I was skipping n = 0 for the moment.

Didn’t realize I was having a problem with - and + of same number. Later I realized, I was having a problem of comparing something to a negative number that I didn’t do a Math.Abs function to.

I believe I have learned more now about reading through non working code. Specially with Error.WriteLine after spending way too long on this.

Thanks, I solved the problem, and this is my solution.
Admin Edit: Don’t share your solutions pls.

Hey i’m having trouble with this one in C :frowning: Can someone help ?

for (int i = 0; temps[i] != ‘\0’; i++)
{
if ( temps[i] != ’ ’ )
{
if ( atoi(temps[i]) > 0 && atoi(temps[i]) < max )
{
max = atoi(temps[i]);
}
else if (atoi(temps[i]) < 0 && atoi(temps[i]) > min )
{
min = atoi(temps[i]);
}
}
}

Won’t compile …
(Im beginner)

Awesome! Those writelines are so valuable… use them liberally.

the first thing I do now in every new problem I start is to write me a short say function (groovy):
def say = {System.err.println(it)}

so that if I need debug output I can just write ‘say(variable or anytext)’ anywhere in my code :slight_smile:

thank you but i just used split() in python

Hello I´m doing the temperatures puzzle in C++ and I´m programming the following sub-algorithm just to take one by one each number:

int getInt( string s){
//Returns the first int of the string, then
//removes it from the string(this part hasn´t been done yet)
int n=0;
if( s[0]==’-’){
for(int i=1;s[i]!=’ ';i++){
n=n10+s[i];
}
return -n;
}
else{
for(int i=0;s[i]!=’ ';i++){
n=n
10+s[i];
}
return n;
}
}

But the problem is that in the first part, when i hace the following string: {1 5 2 6 7}
It returns me 49 and i don´t know what´s wrong.

I have been doing some Debug just whatching the value of s[i] and n just before the operation and I don´t know why it sums 49

Thanks for the help

s[0] is a char equal to ‘1’ . When a char converts directly to int, the number you get is its ascii value. The ascii value of ‘1’ is 49. Not sure what the rest of your code does, but it seems like this is the root of your problem.

For a single character, you can subtract the ascii value of 0 (or do s[i] - ‘0’) to get its numeric value. For multiple characters you need a string conversion function like atoi.

2 Likes

Wow thank you so much I don´t know why i didn´t think about that.

Thank you again :smiley: