Temperatures puzzle discussion

yeah , i think it should …

At first glance I thought this was more complex than it was, and then once done I was grateful for the reminder on trying to find the less complex solution, thank you!

Hi! The following program is evidently wrong (i.e. try (
[10,10] for temperatures), even though it gets 100%. I suggest adding validation case [10,10].

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>

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

int main()
{
    // the number of temperatures to analyse
    int result=10000;
    int n;

    scanf("%d", &n);
    if(!n) result=0;
    for (int i = 0; i < n; i++) {
        // a temperature expressed as an integer ranging from -273 to 5526
        int t;
        scanf("%d", &t);
        if(abs(result)>abs(t)) result=t;
        else if (abs(result)==abs(t)) result=-result*t/abs(t);
    }
    // Write an answer using printf(). DON'T FORGET THE TRAILING \n
    // To debug: fprintf(stderr, "Debug messages...\n");
    printf("%d\n",result);
    return 0;
}

I can’t have the 100% because of “5526 Alone”. I don’t understand the problem…

Does your code work if the temperatures to compare consist of one or multiple instances of 5526 only?

Hello, I don’t understand the issue with “5526 alone” and “-273 alone”.
I’ve tested and these value should pass easily.

EDIT: Ok it doesn’t seem to depends on the second line input. It seems to be depending on the first line input.

Hi,

There is a misstake in the data with the “only negative numbers” condition, there is a positive 3 that bugs the tests. (expected 5 but got 3), see code below :

class Solution {

public static void main(String args[]) {
    Scanner in = new Scanner(System.in);

    int length = 0;
    int max = 5527;

    while (in.hasNext()) {
        int n = in.nextInt();
        System.err.println("n : " + n);
        int nAbs = Math.abs(n);
        System.err.println("nAbs : " + nAbs);
        if (nAbs < max) {
            max = nAbs;
        }
        length++;
    }
    System.out.println(max);
}

}

Output:
n : 3
nAbs : 3
n : -12
nAbs : 12
n : -5
nAbs : 5
n : -137
nAbs : 137
3 <=== (here result)

Failed

Found:
3
Expected:
-5

I also wonder if it is normal, for the test without temperatures, that “while (in.hasNext()) {}” condition is true?

Output :
n : 0
nAbs : 0
0

3 is the number of temperatures. Similarly, for the test without temperatures, 0 is the number of temperatures. That number should not be included in the comparison. Please read the Input section of the statement for what the inputs consist of.

By the way, you may format your code properly by using </> button. And normally, the full/core code should not be posted on the forum, except for some of the easiest puzzle like this one. :wink:

1 Like

Thanks for informations

Hello, could someone please check my solution, because I don’t understand why the tests don’t work with my solution. While when I test myself with the test values, everything works.

function abs(nb) {
    if(nb == 0) return 0;
    if(nb > 0) return nb;
    if(nb < 0) return -nb;
}

if ( n ==  0) {

    console.log(n);

} else {

    for(let i = 0; i < inputs.length; i++) {

        if(i == 0){
            min = inputs[i];
            continue;
        }

        if(inputs[i] == min) {
            continue;
        }
        
        if(abs(inputs[i]) < abs(min)) {
            min = inputs[i];
            continue;
        }
        
        if(abs(inputs[i]) == abs(min)) {
            min = abs(inputs[i]);
            continue;
        }
        
    }
    console.log(min);
} 

This line is wrong:

min = abs(inputs[i]);

Think about a case where you have the same negative number twice in a row.

By the way you can format your code properly by using the </> button in the formatting toolbar. Except for a few Easy puzzles (including this one), it is better not to post the full/core code on the forum.

humm unless I am mistaken, this line handles this specific case :thinking:

if(inputs[i] == min) {
            continue;
}

Oh sorry, I missed that line.

I suspect that the issue is actually that you’re not comparing numbers, you’re comparing strings.

In the default code, there’s a line:
const t = parseInt(inputs[i]);

But it seems that your code isn’t making use of t, and instead uses inputs[i] directly, and they’re still strings. A good fix is to use t instead of inputs[i] during the comparisons. Or, a quick fix to that line is:

if(+abs(inputs[i]) < +abs(min))

Nvm i just realised last number is amount of numbers

hii imtrying this code in c bot some erros showing…can give the solution

Hello, what is your problem exactly ?

1 Like

Give the solution you damn cat! :rage:

2 Likes

sorry, here is the solution. It was not nice from me to hold it from you, i apologize :confused:

3 Likes

Hi!
I work with java. I used arrays, for loop and if statements in my code.
I pass all the tests except I don’t know how to check if no temperature is provided.
Any help?
Thanks

The first input is the number of temperatures. If it is zero, then no temperature is provided. (In case you miss it, a description of the inputs is included in the statement.)