Temperatures puzzle discussion


#517

Welcome to Codingame!

The puzzle is asking for the number with the lowest absolute value. You are returning the absolute value of the number with the lowest absolute value, rather than the number itself. As you’ve noted, that will fail when the correct number is negative.


#518

Hi, I’m new here and I’m also new to c++. I’m having some issues on where to really start with this puzzle. I understand most of the terms of c++, but thanks to a crappy teacher for the first part of my c++ classes I have little to no understanding of how to actually write code. If someone could at lest point me in the right direction I would very grateful.


#519

Well do you need a concept or advise on actual C code? Well i started in lua not long ago. So i took a look in the official lua users wiki and tutorials about the concepts you need for this problem.
On the overview page it even tells you which concepts are important. Namely: Loops, Array, Conditions. I would also look into string manipulation.

Then look at the problem and go step by step. First you get a string and the number of temperatures. So find the nth temperature in the string (loop, string manipulation). Make sure it is number by now. Look at its delta from 0. Find a way to save that (variables) and compare it to the next case… and so on.
Just break a problem down into small steps. Also it helped me to print alot of my variables and steps in between to control the returned values…Hope this helps a little bit


#520

Greetings,
I am also having trouble with this puzzle like larryjoe.
I have noticed that the input stream is not an array but one long string. Thus I cannot access or analyze the numbers. I have tried to take each character of the string and put it into an array, which gives me everything as one item in the array including spaces and ‘-’ signs. Still though, from there I cannot append just the numbers from that array to a new Int array because the first array is still an array of strings (just short strings of one character).

My thinking thus far has been this:
read output string
divide the output string into an array.
remove only the numbers and append them to an Int array
check for the largest number
output the largest number

What am I doing wrong?


#521

You are on the right track. Try to find how you can do the following in your language: “convert string to int” and “split string”.


#522

Thank you, chr-m. That helped a lot.


#523

Hello,

I must say, I am bit confused. I am given temperatures as a string. But is each temperature somehow divided?
I mean is temps just string of numbers and “-”?
How I am suppose to know, when one temperature ends and anather starts?
Trying to do it with Python.

Best Regards


#524

There is whitespace between the numbers. Look at the example in the problem description, you can take that literally. You can also look at the in/output of all the other testcases.


#525

Thank you very much. That helps a lot.


#526

well it DID feel weird, but since I was so sure I was correct… :slight_smile:

Anyway, big thanks for the answer, I got it now.
Rather easy once you solve the problem hehe


#527

Hi the requirements say:
“[…]if the temperatures are -5 and 5, then display 5[…]”
That is why -2 is wrong.


#528

Hi! I am a beginner in coding. How long does it take to solve this game?


#529

Welcome to Codingame!

Depends on the puzzle and the coder. For a novice who only knows a little bit about coding, the Easy ones can take a few hours, especially if it calls for a method that you don’t know. Someone who already knows how to code can go through them much faster. If you’ve done them before and are just using them to learn a new language, they can be done in 5 minutes or less.

The temperatures puzzle has a number of little details that often trip up beginners. Be sure to read the whole problem statement thoroughly, and don’t forget you can use error outputs to help in debugging. How you do that is language-dependent but it’s explained in the comments of the default code.


#530

Bonjour,

Je code en C ici et j’arrive à passer tous les tests sauf le numéro 7 avec en entrée {-10 -10} mais je ne comprend pas pourquoi, logiquement mon programme affiche -10 pour ce cas, pourriez-vous m’éclairer svp? merci par avance.

// Write an action using printf(). DON'T FORGET THE TRAILING \n
// To debug: fprintf(stderr, "Debug messages...\n");
//vérification si aucune température
if (n==0) {
    printf("%d\n", 0);
    return 0;
}
//remplissage de la matrice t avec les températures une par une
for (int i = 0; i < n; i++) {
    for (int k = 0; k < n; k++) {
        for (int j = 0; j < 6; j++) {
              if (temps[i] != ' ') {
                  t[k][j] = temps[i];    
              }
              else {
                  t[k][j] = '\0';
                  j = 6;
              }
              i++;
        }
    }
}

int closest;
//recherche de la température la plus basse et stockage dans closest
for (int k = 0; k < n; k++) {
    if (k == 0) {
        closest = atoi(t[k]);
    }
    else if (fabs(atoi(t[k])) < fabs(closest)) {
        closest = atoi(t[k]);
    }
    else if ((fabs(atoi(t[k])) == fabs(closest)) && (atoi(t[k]) > closest)) {
            closest = atoi(t[k]);
    }
}

printf("%d\n", closest);
return 0;

}


#532

Hello,

I hope someone can help me. I started learning Python 3 a few weeks ago and i managed to get 90% on this test.
The only case my code cannot pass is the one when a negative and a positive temperature have the same distance to 0.
I have to admit that i do not do any comparison of the temperatures in my code so if someone has an idea how i can fiy the one missing case in my code it would be great.

if n == 0:
    print (0)
else:
    numbers = [int(el) for el in temps.split(' ')]
    output = {}
    
    for i in range(len(numbers)):
        f = abs(numbers[i])
        output[f] = numbers[i]
    
    x = [value for (key, value) in sorted(output.items(), reverse=False)]
    print(x[0])

#533

hi Wigo, don’t limit yourself to python or any specific language.
Assume there is a function f(a,b) that takes two inputs and return one output, the function can be designed as:

if abs(a) equals abs(b) then return abs(a)


#534

ok found a failure in my code.
i cannnot have 2 keys named the same. I should have known it.
i am on it to fix my code :slight_smile:


#537

Dears, can you evaluate my code:
in principle,my algorithm should work as follows:
I check every value T if it is negative,
if yes, then convert to positive
all original and converted negatives (now also positives) are checked, if they are lower that Tmin. if they are lower, they should be returned (printed).

can’t make it work somehow,

Please advise,

var n = parseInt(readline()); // the number of temperatures to analyse
var temps = readline().split(' ');
// the n temperatures expressed as integers ranging from -273 to 5526
var Tmin = 5526;
if (n=0) {print (0);}
else {
   for (var i = 1; n; i++) {
        if (temps < 0) {
            z = temps * (-1);
        }
       else {
          z = temps;
       }
       if  (Tmin > z) {
          Tmin = z;
       }
   }
   print(Tmin);
}

#538

temps is an array of string.

Problems:


#539

Hi, _CG_SaiksyApo, really appreciate yoru support.
I probably understood the task itself in a bit wrong way - my code is designed to return only with positive numbers, which is not really what was requested - I will fixed that.

I’m still struggling, though, to access the array elements. I changed temps to temps[i], but during the testing doesn’t work.

if you still have patience to check/comment/fix - I would appreciate.

var temps = [];
var n = parseInt(readline()); // the number of temperatures to analyse
var temps = readline().split(’ ');
// the n temperatures expressed as integers ranging from -273 to 5526
var Tmin = 5526;
if (n=0) {print (0);}
else {
for (var i = 0; n; i++) {
if (temps[i] < 0) {
z = temps[i] * (-1);}
else {z = temps[i];}
if (Tmin > z)
{
Tmin = z;}
}
print(Tmin);
}