Temperatures puzzle discussion

I don’t understand why on Javascript I failed this…

Two negative temperatures that are equal: {-10 -10}

Yet passed everything else with a 90%. Not sure how to ask for help since I can’t post code. Yes I understand that the answer itself is -10. What I don’t understand is

} else if (Math.abs(t) === Math.abs(x)) {
    x = Math.abs(t);
}

Fails that?

If you use x as an answer, it will always be positive.
Your test works for {10 10} or {-10 10}, not for {-10 -10}.

2 Likes

Hi! Im doing this puzzle in C and was wondering if anyone could offer some advice. My code works fine except that it cant detect when no tempertures were given. I’ve tried checking for EOF and NULL but i’m not really sure what to do in this situation.

The puzzle gives you the number of temperatures you’re supposed to read as input in the variable n I think.

hello, i’m a beginner in python (literaly just started with python 3) and i can’t figure out how to code this puzzle. i tried implimenting a list to my temperatures but that turned out to be a terrible idea.

n = int(input()) 

my_temps=[]
for i in input().split():        
        t=int(i)
        my_temps.append(t)
        
        result=my_temps[0]
        if n==0:
            result=0
        else:
            for i in range(len(my_temps)-1):
                if abs(my_temps[i])<abs(result):
                    result=my_temps[i]
                    

print(result)

please any help?

At first, you don’t need “-1” in range() argument as long as upper bound not included in resulting sequence. Also, you can simply use “n” instead of len(my_temps)

And about logic - you need to read puzzle description carefully -> " If two numbers are equally close to zero, positive integer has to be considered closest to zero (for instance, if the temperatures are -5 and 5, then display 5)."

I tried using n as you said but it didn’t quite work for me, probably due to a mistake i made. If not, the logic as you say is quite simple to understand but coding it is not as easy.
thanks anyways for your help

Hello! I’ve used your code and modified it so it could pass all the validators after submitting

admin EDIT: please don’t share the solution of puzzles in the forum.

I’m 8 days late to the party but don’t forget that if n=0, there will be no inputs to read on the next line.
input().split becomes problematic at that point.

My JavaScript passes all tests except the complex test case. But when I test my code in the console in Chrome Dev tools and pass in the complex test case values, it returns the correct value.

Any ideas?

And… I figured it out. In my code, I needed to test for equality in absolute values before testing for larger absolute value.

1 Like

hi in testcase 6 .
How to find if no temparatures are given or not in java

aren’t you given the number of temperatures to read in input in variable N?

Why can’t I use Math.abs in c#?

You can but but it is not javascript …
In C# the method is Math.Abs()

1 Like

I think you should include test cases with few equal negative numbers.
I was able to pass all tests with wrong code

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

const n = parseInt(readline()); // the number of temperatures to analyse
if (!n) {
    console.log(0);
    return;
}

const inputs = readline().split(' ');

// My test case that is not covered
// const inputs = [-5, -5, 9, 8];
// console.error(inputs);

let result = 9999;

for (let i = 0; i < n; i++) {
    const t = parseInt(inputs[i]); // a temperature expressed as an integer ranging from -273 to 5526
    
    // Wrong code that works with all tests
    // if (Math.abs(result) === Math.abs(t)) {
    
    if (Math.abs(result) === Math.abs(t) && result !== t) {  // Right code that also works =)
        result = Math.abs(t);
    }

    if (Math.abs(result) > Math.abs(t)) {
        result = t;
    }
}

// Write an action using console.log()
// To debug: console.error('Debug messages...');

console.log(result);

Ok, so this is the solution I did in C++. It passed with 100% validation but I think there are some flaws in this, but if you’re too lazy just copy and paste it, lol.

If you didnt understand the “closest0”, it just holds the smallest value it’s given.

`for (int i = 0; i < n; i++) {
    int t; // a temperature expressed as an integer ranging from -273 to 5526
    cin >> t; cin.ignore();
    temp[i] = t;
    closest0 = temp[0];
}
for (int j = 0; j < n; j++)
{
    if(temp[j] >= 0 && temp[j] < closest0)
    {
        closest0 = temp[j];
    }
    else if(temp[j] < 0 && temp[j] > closest0)
    {
        closest0 = temp[j];
    }
    else if(closest0 < 0 && temp[j] >= 0)
    {
        closest0 *= -1;
    }
}`

i tried the below logic which is the same one as the one in this discussion forum but it is failing on the first test case
Code:
t = int(i)
if math.fabs(t) < math.fabs(result) :
result = t
elif ( math.fabs(t) == math.fabs(result) ) :
if t > result : # found same value, but positive
can someone please help me out

Bonjour,

J’essaie de passer ce challenge en JS.
Est-ce que l’interpréteur est buggé car sur codesandbox mes tests fonctionnent parfaitement

1 Like

I checked your code…
I just modified to take the input from CG and added a console.log to print the output
and it works for the tests (did not check validators)