Temperatures puzzle discussion

Which test? I couldn’t find any test with this input. Are you sure of the way you parse the inputs?

test case 02 “only negative numbers”

to correct myself, I do get the first one right, where I found
1
from
[1, -2, -8, 4, 5]

as well as the last one, where no temperature is given and I print a zero.

The other failures are:

02 Only negative numbers
-1
[-1, 2, -5]
Failure
Found: -1
Expected: -5

03 Choose the right temperature
1
[4, 2, -5, 1, 2, 2]
Failure
Found: 1
Expected: 5

04 Choose the right temperature 2
1
[4, 2, 5, 1, 2, 2]
Failure
Found: 1
Expected: 5

05 Complex test case
0
[-5, -4, -2, 1, 2, -4, 0, 4, 2, 1]
Failure
Found: 0
Expected: 2

Bug in the puzzle code, my code:

var n = parseInt(readline());
var temps = readline();
var temps = temps.split(" ");
var cnum = 5526;
var res = 9999;
var neg = 0;
for (i = 0; i < n; i++){
if (temps[i] < 0) {temps[i] *= -1; neg = 1;}
if (temps[i] == cnum) {if (neg === 0) {res = temps[i];}}
if (temps[i] < cnum) {cnum = temps[i]; if (neg == 1) {temps[i] *= -1; res = temps[i]; neg = 0;} else {res = temps[i];}}
if (neg == 1) {temps[i] *= -1; neg = 0;}
}
if (n === 0) res = 0;
printErr(temps);
print(res);

worked on normal javascript execute pages (demo on jsfiddle: https://jsfiddle.net/9fqtz5da/1/)
but the code doesn’t work on your IDE (the print result will be ‘-5’ instead of ‘5’, please fix this or tell me what’ve i done horribly wrong in my code that the print result is wrong.

indeed, you don’t read the inputs correctly:

Here is the input of test 2: [-12, -5, -137]
By the way, the test is called “only negative numbers”, didn’t if feel weird to have ‘2’ in input? :wink:

By clicking on the button on the top right of the test cases panel, you can see the test cases details.

quote of the 2 lines rules of the puzzle:

If two numbers are equally close to zero, positive integer has to be considered closest to zero

Here’s the edited version:

using System;
using System.Linq;
using System.IO;
using System.Text;
using System.Collections;
using System.Collections.Generic;

/**
 * Auto-generated code below aims at helping you parse
 * the standard input according to the problem statement.
 **/
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[] absoluteZero = new int[n];
        
       
        
        // Write an action using Console.WriteLine()
        // To debug: Console.Error.WriteLine("Debug messages...");
           for(int i = 0; i < absoluteZero.Length; i++)
           {
              if(i < -15 || i <= 0 || i < 15)
              {
                 n = absoluteZero[i];
              }
              else
              {
               throw new Exception("WRONG!! ERROR!!");   
              }
           }
        
        Console.WriteLine($"{temps}");
        
    }
}

You’d better tell us what is blocking you or what you don’t understand. Before writing any code, I suggest that you write in english the different steps you need to do to solve the pb.

Yes, i know, but the code keep return the negative number, idk why, but when i test it on another javascript environment, it’ll return the positive number (check the jsfiddle demo) => your javascript version IDE is bugged, fix this please :’(

I don’t think so. :grinning:

Who are you talking to ?, If me, what do you mean ?

I say that I think that there is no bug in the IDE but that there is a bug in your code.

Bonjour,

Cela fait plusieurs heures que j’essaie de trouver une solution à ce problème mais rien n’y fait.

En gros à la toute fin de mon code, il y a un test pour savoir si c’est la valeur positive ou la valeur négative qui est la plus proche de zéro, la résultat de ce test donne le nom de la variable qui servira de valeur print.

Ce cas concerne le testcase numéro deux, ou seule des valeurs négatives sont donnée.

Perso je n’arrive pas à comprendre, si mon calcul est juste : 0 - 5 = -5, donc une valeurs négative. Je n’arrive pas à comprendre pourquoi la condition du if passe en True…

J’ai mis mon code en dessous (attention les yeux, ça pique^^) pour les experts qui voudrait bien aider un autodidacte dans le désarrois…

Merci d’avance!

PS : c’est du Python 3

 import sys
 import math

 # Auto-generated code below aims at helping you parse
 # the standard input according to the problem statement.

 n = int(input())  # the number of temperatures to analyse
 temps = input()  # the n temperatures expressed as integers ranging from -273 to 5526
 mini=int(-9999)
 maxi=int(9999)
 closest=int(0)
 test=int(0)
 nbtest=int(0)



 # Write an action using print
 # To debug: print("Debug messages...", file=sys.stderr)

 #print("n",n,"\n")
 other = temps.split()
 #print("other",other,"\n")
 for test in other:
     
     nbtest+=1
     #print("nbtest",nbtest)
     #print("test",test)
     #print("temps",temps)
     test=int(test)
    
     if test>0 and test<maxi:
         maxi=test
       #  print("maxi",maxi,"\n")

       
     if test<0 and test>mini:
         mini=test
        # print("mini",mini,"\n")

 if maxi==9999:
     maxi=0
 if mini==-9999:
     mini=0
 #print("mini",mini)
 #print("maxi",maxi)
 if maxi-mini>=0:
     closest=maxi
    # print("check")
 else:
     closest=mini



 print(closest)

Pourquoi borner tes valeurs entre 9999 et −9999 ?
Connais-tu la valeur absolue ?

Si j’annonce les variable maxi et mini à zéro, le calcul ne se fait pas (parce que le calcul se sert de cette variable comme base, en comparant les valeurs, s’il voit la variable à zéro il gardera toujours cette valeur vu que c’est le plus proche… de zéro).

J’ai rajouté à la fin du code un moyen de le neutraliser si le calcul ne s’est pas fait du tout en cas de valeur donnée uniquement positive ou négative.

Finalement en me levant ce matin, la solution m’est apparue :
En ajoutant une condition

and maxi!=0:

sur le dernier if, je valide tout les testcases avec 100% de point.

Je suis pas sur de comprendre pourquoi ça plantait que dans le cas de valeur négative mais ça passe.

C’est un peu chiant quand ton code marche mais tu sais pas trop pourquoi…

I advise you to check out the published solutions of this puzzle. You’ll discover the use of absolute value.

If there are only negative temps, then your last test if maxi- mini>=0 is always true. Because maxi = 0 so -mini is positive (since mini is negative)

Hello,

thanks for your answer.

I searched for an published solution but as a newbie i was unable to find it. Please can you help and show me where i can find it?

There is a tab named “solutions” on the details page. If you solved a puzzle 100% with a language, you should be able to see solutions in that language.

Effectively, i didn’t know the abs function. This make everything simple… too simple maybe :stuck_out_tongue:

Thanks for the info.

1 Like

Hi there, after several read ups i got a code in lua that is finally working…kinda.

In my IDE (ZeroBraneStudio) it solves it without any problems, but here on the website IDE i fail the test with only negative numbers and i can’t see why. Anyone got an idea?

Mod note: Don’t post the full code, please.

      else if n > 0 then
        for i = 1,n do
          local num = math.abs(findnth(temps,i))
          if num < i_temp then
            i_temp = num
          end
        end
      end
    end

    print(i_temp)

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.

1 Like