Temperatures puzzle discussion

without more info, I’d say you did a mistake when reproducing the test case.

What does the console show on CodinGame?

https://pasteboard.co/HIHpv4Z.png

this is my code:

int main()
{
    int n; // the number of temperatures to analyse
    cin >> n; cin.ignore();
    
    int tA = 0;// temperatura koja je najbliza 0
    int tL = -273;// negativna temperatura najbliza 0
    int tH = 5526;// pozitivna temperatura najbliza 0
    int iL = 0;// pomak prema najblizoj negativnoj temperaturi
    int iH = 0;// pomak prema najblizoj pozitivnoj temperaturi
    int nL = 0;// broj pomaka u negativnoj temperaturi
    int nH = 0;// broj pomaka u pozitivnoj temperaturi
    
    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 (t == 0)
        {
            tA = t;
        }else
        
         if (t > 0 && t <= tH)
        {
            tA = t;
            tH = t;
        }else
        
        if (t < 0 && t >= tL)
        {
            tA = t;
            tL = t;
        }
        
        if (tH != iH)
        {
         iH++;
         nH++;
        }
        
        if (tL != iL)
        {
        iL--;
        nL++;
        }
        
        
        
        if (nL != nH && nL < nH)
        {
        tA = tL;
        }else
        
        if (nL != nH && nH < nL)
        {
        tA = tH;
        }
        
       /*else
        
        if (nL == nH)
        {
        tA = tH;
        }*/
        
       
        

    }

    // Write an action using cout. DON'T FORGET THE "<< endl"
    // To debug: cerr << "Debug messages..." << endl;

    cout << tA << endl;

note that if I uncoment the last part the whole code stops working while it works perfectly in MVS 2017

I have one first global programming advice:

When something breaks, define what you mean by “stops working”.

Apart from that, I tested your code and uncommenting the commented part only made me pass some tests that were failing before and fail others that were passing before.

It’s hard to see what your code doing wrong without seeing the outputs. However, I would first advise you to simplify. Rather than counting shifts and doing whatever the iL and iH variables are doing, why not just compare the best positive and negative results and give your output based on that?

Also: https://www.programiz.com/cpp-programming/library-function/cstdlib/abs

I think you should add one more test case.

  • A case contains “two or more than two negative closest data” (i.e. 10, 13, -5, -5, -8).

I wrote a code which returns a positive closest data when there are multiple closest data in the input. This code passed all test cases but it should not. The code I wrote will return 5 if the above case is tested (which is wrong).

All of the tests in the IDE have been successfully passed, but after submission the code failes test nr. 2 (Error: -273 alone) and nr. 3 (Error: 5526). I would apreciate if someone could tell me what is the problem (not the solution). Thanks!
So here is my C# script:

class Solution
{
    static void Main(string[] args)
    {
        int n = int.Parse(Console.ReadLine()); // the number of temperatures to analyse
        string[] inputs = Console.ReadLine().Split(' ');
    
        int maxPass = 5526;
        int minPass = -273;
        int max = maxPass;
        int min = minPass;
        int x = 0;  // final temperature
    
        for (int i = 0; i < n; i++)
        {
            int t = int.Parse(inputs[i]); //temp of input[i]
        
            if (t > 0) {
                if (t < max) {
                    max = t;
                }
            }
            else if (t < 0) {
                if (t > min) {
                    min = t;
                }
            }
        }
        if (max >= maxPass && min <= minPass) {
            x = 0;
        }
        else if (max >= maxPass) {
            x = min;
        }
        else if (min <= minPass) {
            x = max;
        }
        else if (Math.Abs(max) < Math.Abs(min)) {
            x = max;
        }
        else if (Math.Abs(max) > Math.Abs(min)) {
            x = min;
        }
        else if (Math.Abs(max) == Math.Abs(min)) {
            x = max;
        }
        Console.Error.WriteLine("min " + min);
        Console.Error.WriteLine("max " + max);
        Console.WriteLine(x);
    }
}
1 Like

You don’t handle correctly the cases where there is only one temperature given. You can set up this custom test case in the IDE and test it yourself:
Input:
1
-273
Output
-273

Same issue with 5526 alone

Edit:
Got it. Test when n=1.
Check all logic < , > , ==, <=, >= etc…
In my case I had to use <= and >= instead just <, >.

1 Like

Output instructions
Display 0 (zero) if no temperatures are provided. Otherwise, display the temperature closest to 0.

codin1

Anyone got any ideas what I might do to get my last 10% or is the Display Zero case for a (inputs == null) conditional I’m using buggy? Action is setting my answer = 0.

Thanks for any ideas…

I would rather use if n == 0 instead of inputs == null

hello,

I am having a tough time understanding what is happening here.
When I sort the input array, 5 is apparently greater than 42. I used the javascript function “.sort()” to sort the array but I dont understand why this would happen? I have a picture of what is happening but since im new, i cant upload it.

Can you please help me?
~Sasha

seems you managed to solve the puzzle. What was your issue? (could help future readers)

Hi there,
I am using Python min() to find the minimum temperature, however it always returns the negative value instead of the positive, any clues or suggestion how can I make my program return the positive form of the number?

eg: [-1,2,3…] it returns -1 instead of 1.

Thanks for the help in advance.

https://www.tutorialspoint.com/python/number_abs.htm

oh, i see the problem now. Thanks for finding the bug!

I made some codes for Temperatures puzzle like below

const n = parseInt(readline()); // the number of temperatures to analyse
var inputs = readline().split(' ');
var closestt;
console.log(inputs)
closestt = parseInt(inputs[0])
console.log('inputs[0] : ' + closestt);

for (let i = 0; i < n; i++) {
const t = parseInt(inputs[i]); // a temperature expressed as an integer ranging from -273 to 5526

console.log('i = '+i);
console.log(Math.abs(parseInt(inputs[i])))

if(Math.abs(closestt) > Math.abs(parseInt(inputs[i]))){
    closestt = parseInt(inputs[i]);
}else if(Math.abs(closestt) == Math.abs(parseInt(inputs[i]))){
    if(closestt < parseInt(inputs[i])){
        closestt = parseInt(inputs[i])
    }
}
console.log(closestt);
console.log(typeof closestt);
}

// Write an action using print()
print(closestt);

but there is some error like

42,-5,12,21,5,24

inputs[0] : 42

i = 0

42

42

number

i = 1

5

-5

number

i = 2

12

-5

number

i = 3

21

-5

number

i = 4

5

5

number

i = 5

24

5

number

5

Failure

Found: “42…”

Expected: “5”

Somebody can help me for resolving this one??

???
That looks like you just have to comment out your debug outputs…

My code outputs the expected values (-273 or 5526) if those single values are input, likewise any other single value. All the test cases pass. But after submitting the solution, my code fails the “-273 alone” and “5526 alone” validators.

Is there something wrong with the validator or the emulator? I’m coding in PHP. Can you just add the “single value” validation cases as testable test cases in the game itself? It’s possible to override the input values and echo the correct result by fudging another test case, but the code isn’t being accepted when it’s submitted.

I have a section near the top of my code that bypasses the for loop if there’s only a single value input:

if ($inputs_count == 1) {
    if (!is_numeric($inputs[0]) ) {
        $solution = 0;
    } else {
        $solution = intval($inputs[0]);
    }
} else { ...execute the FOR loop

where $inputs_count = count($inputs) after the explode() is run.

1 Like

you can test it yourself actually. Check out the settings menu on the left panel in the IDE and activate the “expert” mode. Now you can add a custom test in the test case section.

1 Like

(PHP) Tous les tests passent dans la page de code à 100%. et seulement 85 % après soumission sur question 2 et 3.
(PHP) all code example passed in dev hud. but after submiting only 85 % . any clue ?