Temperatures puzzle discussion

Having a difficult time trying to get this solution to work, can anyone point me in the correct direction?

import java.util.*;
import java.io.*;
import java.math.*;

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

    public static void main(String args[]) {
    Scanner in = new Scanner(System.in);
    int result = 0;
    int n = in.nextInt(); // the number of temperatures to analyse
    int[] array = new int[n]; //new int[n];
    if (n <= 0) {
        result = 0;
    }
    for (int i = 0; i < n; i++) {
        int t = in.nextInt(); // a temperature expressed as an integer ranging from -273 to 5526
        array[i] = t;
    }

    for (int j = 0; j < array.length; j++) {
        result = array[0];
        if (Math.abs(array[j]) < Math.abs(result)) {
            result = array[j];
        }

        if (Math.abs(array[j]) == Math.abs(result) && array[j] > 0) {
            result = array[j];
        }
        //System.err.println("Debug messages..." + result);
    }



    // Write an answer using System.out.println()

    System.out.println(result);
}

}

There are two issues with your code:

  1. It keeps overwriting the variable “result” with array[0] because you put it inside the for loop.
  2. It does not handle the case where there are no temperatures given.

Thank you so much!

Bonjour,
Il y a un problÚme au niveau des tests exécutés. Le code ci-dessous est validé avec succÚs par la validation, pourtant il ne passerait pas si on utilisait le data set (-2, 1, 4), par exemple.

class Solution {

public static void main(String args[]) {
    Scanner in = new Scanner(System.in);
    int n = in.nextInt(); 
    int result = 0;
    ArrayList<Integer> temperatures = new ArrayList<>();

    if (n > 0 && n < 10000) {
        result = in.nextInt();
        for (int i = 0; i < n-1; i++) {
            int t = in.nextInt();
            temperatures.add(t);

            if (t >= 0)
                result = Math.min(t, result);
            else
                result = Math.max(t, result);
        }
        if (result < 0) {
            for(int item : temperatures)
                if (item == Math.abs(result))
                    result = item;

        }                           
    }
   System.out.println(result);       
}

}

n = int(input())  # the number of temperatures to analyse

temps = [int(i) for i in input().split()]
if len(temps)!=0:
    abs_temps = [abs(i) for i in temps]
    abs_minimum = min(abs_temps)
    indices = [i for i,x in enumerate(abs_temps) if x == abs_minimum]
    if len(indices)>1:
        element_filter = [i for i in indices if temps[i]>0]
        if not element_filter:
            result = temps[indices[0]]
        else:
            result = temps[element_filter[0]]

    else:
        result = temps[indices[0]]
else:
    result = 0


# Write an answer using print
# To debug: print("Debug messages...", file=sys.stderr, flush=True)

print(result)

My solution feels messy with multiple branches. Is there a better solution without using numpy?

Instead of reading all the temperatures at one go and storing them in a list, you may read the temperatures one by one and update your answer as required. You may try using no lists at all.

If you use a list you can also use the min function with a custom sorting key.

I give you an example.
Say you have a list of (r, g, b) color tuples.
You want the one that maximizes the sum r+g+b and, as a tie breaker, you want to favor those where r > g.
You’d write something like that:
max(colors, key=lambda c: (sum(c), c[0] > c[1]))

Hi I need help in coding this game using C language since there’s no tutorial in youtube using the said language. Thanks

What help do you need? Please specify.

#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()

{// a temperature expressed as an integer ranging from -273 to 5526

// the number of temperatures to analyse

int n,i,t[i],ans=5527,ab,def=0;

scanf("%d", &n);

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

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

    scanf("%d", &t[i]);

    if(t[i]<ans)

    {

        ans=t[i];

    }

    else if(abs(t[i])==abs(ans) && t>ans)

    {

         ans=t[i];

    }

         

}

if(n==0)

{

    t[i]=0;

}

I only passed the first testcase. Also, i noticed that the if condition (t[i]<ans) can be also t[i]> ans or t[i]>5000 and it still passes the first test case. I do not know why is that. sorry still a beginner

Before pointing out the issues in your code, please note that you may format your code properly in this forum by using the </> button in the formatting toolbar.

Now, the issues in your code:

  • You have initialised t[i]. But you should have waited until you know the number of temperatures n (after the first scanf). Then you can initialise t[n] (not t[i]) properly.
  • In your first if, you are comparing t[i] with ans, but you should be comparing their absolute values instead.
  • In your else if, you are comparing t and ans, but you should be comparing t[i] and ans instead.
  • In your last if, you assign 0 to t[i]. You want to assign it to ans instead, right?

Instead of just fixing the code, please make sure you understand why the code should be fixed as described :wink: Keep it up.

1 Like

Thanks a lot , appreciate the help.

Am I the only one lost and don’t understand anything?
I couldn’t do anything, and it’s the 1st exercise, a little desperate.

If you’re a beginner and haven’t learned programming before, this website can prove too difficult for you, as a certain level of programming knowledge is a pre-requisite to solve the puzzles here. You may learn it elsewhere before coming back here to practise, though. Alternatively, you may google the relevant aspects while you try to solve the puzzles here.

If you’re just confused about how the puzzles on this website work, you may take a look at the explanation here: https://www.codingame.com/faq

1 Like

[Don’t post the full solution.]

i am trying this puzzle using c++.I have not getting solution for this.

Hi , got a problem resolving this problem in PHP,
when i used $n to know if temperature have been sent , it work 1 time / 2.
:smiling_face_with_tear:

Which cases have you failed, and what error messages have you got?

The “-273 alone” always wrong ( no error message ) , should i send my code ?

I don’t know what numbers that validator contains, but does your code work correctly if -273 is the only number given, or is one of the given numbers?