Temperatures puzzle discussion

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.

When I google “Validators” I get a lot of explanations of what to do to validate that a user has entered in the correct information, but not much beyond that.

Anyways, I put that function in there because I figured if there was a 1 or a 0 in it because those numbers are obviously close to 0 or 0 itself. I swapped them around to fix this, however, I am realizing now that I probably should only be looking for just 0, as my later function call would sniff out the 1 to begin with. If I remove the 1 section of the code it fails on the first on and the third on of the submission. I would need to further revise, but I would like to solve this 5526 problem first.

def check_if_result_is_0_or_1(number):
    result = 0
    if 0 in number:
        result = False
        return result 
    elif 1 in number:
        result = True
        return result
    else:
        result = 4
    return result

However, if there is a number greater then 0, then it should return a 4 which I used to determine that there is no 1 or 0 and therefore to continue on. The result should come out 5526 and does when I run it through an external IDE.

A validator on CodinGame is just a hidden test case for testing your code on submission.

I belive if 0 in number: result = False return result
should not work here.

Temperatures puzzle discussion - #942 by CraigVarley check this out :slight_smile:

I have it +90% complete except -10 -10 case. I am not sure how you understand “Two negative temperatures that are equal: {-10 -10}”= -10 -10 or any 2 negatives values in the array ?
Anyhow i tried both and it doesn’t validate :frowning:

In case of ties, just choose any one because it does not change your answer. For example, if the input is “-10 -10”, then output -10 as the answer; if the input is “-5 -5 -10 -10”, then output -5 as the answer.

I am not sure what inputs that validator provide, but your code should be general enough to cover any cases. An easy approach would be:

  1. initialise an answer as a starting point, e.g. 10000
  2. read a temperature
  3. if the newly read temperature is better than the existing answer, replace it
  4. go back to step 2

Except for the special case of no temperatures being provided, the above approach can deal with any number of temperatures and it does not matter they are positive or negative, or they are ties or not.

#include <iostream>

#include <string>

#include <vector>

#include <algorithm>

using namespace std;

/**

 * Auto-generated code below aims at helping you parse

 * the standard input according to the problem statement.

 **/

int main()

{

    int n; // the number of temperatures to analyse

    cin >> n; cin.ignore();

    int min=5600;

    int index=-1;

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

        int t; // a temperature expressed as an integer ranging from -273 to 5526

        cin >> t; cin.ignore();

        if(abs(t)<abs(min))

        {

            min=t;

            index=i;

            //cout<<min<<endl;

        }

    }

    // Write an answer using cout. DON'T FORGET THE "<< endl"

    // To debug: cerr << "Debug messages..." << endl;

    //cout << "result" << endl;

    cout<<index <<endl;

    cout<<min<<endl;

}

here is my code in codeblocks. it works well while negative in codinggame. i am not sure what’s wrong.

Your code does not work for cases when a negative minimum appears before a positive minimum of the same magnitude, e.g. for the case of -5, 5, your code will output -5 instead of the expected answer of 5.

Also, your code will output both index and min to the standard stream. Anything extra in the standard output will be treated as wrong. As can be seen in the comments near the end of your code, you should use cerr instead, e.g.

cerr << index << endl; // for debugging
cout << min << endl; // for answering

btw you can format your code properly by using the </> button on the formatting toolbar in this forum :wink:

Hello, another question about this weird “no temperature”… I’m on Java, I tried

System.out.println("");
System.out.println("0");
System.out.println(0);
System.out.println(n);

and I NEVER passed this test !
I give 0 ? “nothing” await. I give “” ? “0” await.
Please, I want my 100%…

Maybe the issue is that your code never reaches that line of code?