Temperatures puzzle discussion

I found where the problem comes from, in fact for tests 3 and 4 $minPos gives me 24 instead of 5 for some reason. So it is obvious that he does not fit the condition. But then why he does that, good question.

for(int j = 0; j < n; j++){

        if      ( abs(temp[j]) == abs(result) && result < abs(temp[j])){

            result = abs(temp[j]);

        }

        else if ( abs(temp[j]) < abs(result)){

            result = temp[j];

        }

    }

    printf("%d\n", result);

What is wrong with my code? Why doesn’t it produce a 100% result?
Thank you in advance for your help.

It looks like your first if-condition should be amended, e.g. check what your code will output if the input numbers are -5, -6, -5.

How to pass the last test in js? I am new to this, i don understand “No temperature”, tried
if (t > 0 && t < 5527) {

  -----

}

else if (t >= -273  && t < 0) {


}

else {

result= 0

}
nothing seem to work

In the last test, you aren’t given any temperatures. Your code should be able to detect such a situation and output 0 directly.

Hi, all my testcases work apart from the 5th where there is no temp value at all. I’ve used Nan/unidentified and null, but nothing is working for me. Any suggestions? Thanks,
Craig

const n = parseInt(readline()); // the number of temperatures to analyse
var inputs = readline().split(' ');
var number = 0;
var lowestnumber = 5766;
for (let i = 0; i < n; i++) {
    const t = parseInt(inputs[i]);// a temperature expressed as an integer ranging from -273 to 5526
    number = t;
    if (number === NaN) { // if input empty return zero
        lowestnumber = 0;
    } else { // otherwise return diff of abs value 
    if (Math.abs(number) < Math.abs(lowestnumber)) {
        lowestnumber = number;
    }
    else if (Math.abs(number) === Math.abs(lowestnumber)){ // if abs value same return the positive number
        lowestnumber = Math.abs(number);
    }
} // end else
} // end for

console.log(lowestnumber);

Just check the number of temperatures (first input). If it is zero, output 0 and do not proceed any more.

Bonjour,
j’ai essayé mon code sur Pycharm et il fonctionne parfaitement, mais ne fonctionne pas sur l’ide de la plateforme.
voici le code ( en Phyton) :

n = input()
N = n.split()

if not N:
print(“0”)
else:
n_min = -275
for i in range(len(N)):
if abs(int(N[i])) < abs(int(n_min)):
n_min = N[i]
print(n_min)

Next time please use the </> in the formatting toolbar so that your code can be formatted properly :slight_smile:

Turning to your code: It isn’t able to handle
(a) the case where 2 numbers have the same absolute value but different signs, e.g. -4 and 4,
(b) the case where all numbers provided are positive and greater than or equal to 275, e.g. 700, 800 and 900.
Also:
(c) the actual numbers that you need to compare is in a second line, so you need another input() to read those before splitting.

1 Like

Hello Everyone,

I don’t understand why my code for the Puzzle Températures isn’t working… Could you help me please ?

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
print(n)
temp_plus_proche_zero = -273
for i in input().split():
    print(i)
    ecart = abs(temp_plus_proche_zero - 0)
    # t: a temperature expressed as an integer ranging from -273 to 5526
    t = int(i)
    ecart_test = abs(t - 0)
    print("ecart_test",ecart_test)
    if ecart_test < ecart :
        temp_plus_proche_zero = t
    elif ecart_test > ecart :
        temp_plus_proche_zero = temp_plus_proche_zero
    elif ecart_test == ecart and t > 0:
        temp_plus_proche_zero = t
    else :
        temp_plus_proche_zero = temp_plus_proche_zero
    
    print("temp_plus_proche_zero",temp_plus_proche_zero)
  
# Write an answer using print
# To debug: print("Debug messages...", file=sys.stderr, flush=True)
print("temp_finale",temp_plus_proche_zero)

Thank you in advance !

Léa from France

I haven’t checked the logic of your code, but the most obvious issue of your code is that you have printed stuff in addition to the required answer. You are not supposed to do that. For debug print, please refer to the proper syntax which you can see in the comments in your code:

# To debug: print("Debug messages...", file=sys.stderr, flush=True)

Hello, thank you for your reply. I printed stuff to better understand where my code isn’t working… At the end, my final print said the good value but the game doesn’t work. I really don’t understand…

You can print, but as I said, all prints other than the final answer should be done using the debug syntax.

Okay, I didn’t understand. So I wrote this line :

print("temp_final",temp_plus_proche_zero, file=sys.stderr, flush=True)

And it said that my lower temperature is 1. This is actually the lower temperature but the game doesn’t work, it said 5 as the lower temperature (5 is also my last i)

It works !!! Thank you for your help

1 Like

A post was merged into an existing topic: [FAQ] (Really) Frequently Asked Questions

Salut tout le monde !

Je suis étudiant et je débute sur CodinGame, j’ai besoin d’un coup de mains sur mon programme : je ne parviens pas à repérer le problème …

import sys

import math

n = int(input()) # the number of temperatures to analyse
L=[]
for i in input().split():
t = int(i)
L.append(t)

mini=0

for j in range(1,len(L)) :
mini=L[0]
if abs(L[j])<abs(L[j-1]) : mini=L[j]

print(mini)

The issue is that the value of mini is overwritten by L[0] and, if the absolution value comparison condition is satisfied, it is overwritten by L[j] as well. For example, if the inputs are 4 3 7 6:
When j = 1, mini = L[0] = 4, abs(L[1]) = 3, abs(L[0]) = 4 => 3 < 4 => mini = L[1] = 3
When j = 2, mini = L[0] = 4, abs(L[2]) = 7, abs(L[1]) = 3 => 7 > 3 => mini still = 4
When j = 3, mini = L[0] = 4, abs(L[3]) = 6, abs(L[2]) = 7 => 6 < 7 => mini = L[3] = 6

You need to write your code so that mini is overwritten only when necessary, and also the comparison should be with mini (the closest number to 0 found so far) instead of with the previous number.

Running through your code (like what I did above) would help you understand the logic of your code, and this helps in your debugging process.

P.S. Please format your code with “</>” button in the formatting toolbar, otherwise we do not know how your code is indented. Indentation makes a great difference to how the code is executed in python.

Ok, I am stuck.

My code for this puzzle, works in all the test cases. But when I submit it, it doesn’t work with the test case 3, which is the single number 5526. I copied the code to PyCharm and ran it, and it works perfectly in Pycharm. So I don’t know what to do other then post in this forum in hopes that someone can help me figure this out.

Here is what I have. I am a beginner programmer so my code probably has a lot of bad practices in it, so I am open to any suggestions on improvement as well as finding the problem in my code of why the SUBMIT thing doesn’t work.

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
# print(f"{i}, {t}, {n}", file=sys.stderr, flush=True)
def check_if_result_is_0_or_1(number):
    result = 0
    if 1 in number:
        result = True
        return result 
    elif 0 in number:
        result = False
        return result
    else:
        result = 4
    return result

def check_if_there_are_numbers(numbers):
    are_there_numbers = True
    print(f"{numbers}", file=sys.stderr, flush=True)
    try: 
        number = numbers[0]
    except IndexError:
        are_there_numbers = False
    return are_there_numbers

def final_check(t, positive_numbers, negative_numbers):
    no_pos_number = False
    no_negative_number = False
    pos_number = 0
    negative_number = -1
    result=0
    final_check_executed = 9999999
    print(f"{final_check_executed}", file=sys.stderr, flush=True)
    print(f"{positive_numbers}", file=sys.stderr, flush=True)
    # print(f"{positive_numbers[0]}", file=sys.stderr, flush=True)
    # To avoid the index error do this:
    try: 
        pos_number = positive_numbers[0]
    except IndexError:
        no_pos_number = True
    try:
        negative_number = abs(negative_numbers[0])
    except IndexError:
        no_negative_number = True
    print(f"{pos_number}", file=sys.stderr, flush=True)
    print(f"{negative_numbers[0]}", file=sys.stderr, flush=True)
    print(f"{negative_number}", file=sys.stderr, flush=True)
    if not no_pos_number and not no_negative_number:
        if negative_number>pos_number:
            result=pos_number
            print(f"{pos_number}", file=sys.stderr, flush=True)
        elif negative_number<pos_number: 
            result=negative_numbers[0]
            print(f"{negative_numbers[0]}", file=sys.stderr, flush=True)
        elif negative_number == pos_number:
            result=pos_number
        return result
    elif no_pos_number and not no_negative_number:
        return negative_numbers[0]
    elif not no_pos_number and no_negative_number:
        return positive_numbers[0]

negative_numbers=[]
positive_numbers=[]
all_numbers=[]
temperature=0
negative_number=0
result=0
for i in input().split():
    # t: a temperature expressed as an integer ranging from -273 to 5526
    t = int(i)
    # print(f"{negative_numbers}", file=sys.stderr, flush=True)
    # print(f"{positive_numbers}", file=sys.stderr, flush=True)
    print(f"{t}, {n}", file=sys.stderr, flush=True)
    # print (type(i), type(t), type(n), type(negative_numbers), type(positive_numbers))
    all_numbers.append(t)
    if t<0:
        negative_numbers.append(t)
    elif t>0:
        positive_numbers.append(t)
negative_numbers.sort(reverse=True)
positive_numbers.sort()
   
# Write an answer using print
# To debug: print("Debug messages...", file=sys.stderr, flush=True)
print(f"{negative_numbers}", file=sys.stderr, flush=True)
print(f"{positive_numbers}", file=sys.stderr, flush=True)
print(f"{all_numbers}", file=sys.stderr, flush=True)
all_numbers.sort()
print(f"{all_numbers}", file=sys.stderr, flush=True)
# all_numbers_sorted=all_numbers.sort()
# print(f"{all_numbers_sorted}", file=sys.stderr, flush=True)
check = check_if_result_is_0_or_1(all_numbers)
print(f"{check}", file=sys.stderr, flush=True)
if check == True:
    result = 1
    print (result)
elif check == False:
    result = 0
    print(result)
else:
    check = check_if_there_are_numbers(all_numbers)
    if check == True:
        result=final_check(temperature, positive_numbers, negative_numbers)
    else:
        result = 0
    print (result)

Please note that after you submit your code, your code is tested using validators, which are different from the visible test cases. That is why it is possible that your code passes all test cases but is not general enough to pass the validators too. In that case, you have to revise your code.

And about your code, I do not understand the logic of “check_if_result_is_0_or_1”. Your code will return 1 if the given numbers include both 0 and 1, but the answer in such a case should be 0.