Temperatures puzzle discussion


#668

Ii wrote a code that works in Microsoft Visual Studio but doesn’t work on CodeinGame, why?

When i check the test case it writes that I’m right and writes correct output but console output show failure?


#669

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

What does the console show on CodinGame?


#670

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


#671

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.


#672

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


#673

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).


#676

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);
    }
}

#677

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


#679

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 <, >.


#680

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

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…


#681

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


#682

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


#683

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


#684

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.


#685

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


#686

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


#687

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??


#688

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