Temperatures puzzle discussion

#include <iostream>

#include <string>

#include <vector>

#include <algorithm>

using namespace std;

/**

 * Auto-generated code below aims at helping you parse

 * the standard input according to the problem statement.

 **/

int main()

{

    int n; // the number of temperatures to analyse

    cin >> n; cin.ignore();

    int min=5600;

    int index=-1;

    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(abs(t)<abs(min))

        {

            min=t;

            index=i;

            //cout<<min<<endl;

        }

    }

    // Write an answer using cout. DON'T FORGET THE "<< endl"

    // To debug: cerr << "Debug messages..." << endl;

    //cout << "result" << endl;

    cout<<index <<endl;

    cout<<min<<endl;

}

here is my code in codeblocks. it works well while negative in codinggame. i am not sure what’s wrong.

Your code does not work for cases when a negative minimum appears before a positive minimum of the same magnitude, e.g. for the case of -5, 5, your code will output -5 instead of the expected answer of 5.

Also, your code will output both index and min to the standard stream. Anything extra in the standard output will be treated as wrong. As can be seen in the comments near the end of your code, you should use cerr instead, e.g.

cerr << index << endl; // for debugging
cout << min << endl; // for answering

btw you can format your code properly by using the </> button on the formatting toolbar in this forum :wink:

Hello, another question about this weird “no temperature”
 I’m on Java, I tried

System.out.println("");
System.out.println("0");
System.out.println(0);
System.out.println(n);

and I NEVER passed this test !
I give 0 ? “nothing” await. I give “” ? “0” await.
Please, I want my 100%


Maybe the issue is that your code never reaches that line of code?

@Lainea I think it should be System.out.println(0), not “0”. Now, what is your condition to print 0? In my case, I put all my temperatures in an int array. If the array has length ==0 then it means no temperatures were provided.

Hello, this is my condition :

        if (n == 0) {
            System.err.println("rentre ici");
            System.out.println(0);
        }

I enter into the if, my error message “rentre ici” is visible.

I also tried to do the job with an array, same problem. I enter into the if. I write 0 → nothing is expected. I write “” → “0” is expected. I write “0” → nothing is expected

I passed all the others tests

        int[] array = new int[n];
        for (int i = 0; i < n; i++) {
            array[i] = in.nextInt();
        }
        if (array.length == 0) {
            System.err.println("rentre ici");
            System.out.println(0);
        }

Your code snippets look fine, so there may be an issue somewhere else.
You may send me your full code in private message and I will take a look.

Thank you for your offer ! But I’m sorry, I don’t see how I can message you :
“Need to have a direct personal conversation with someone, outside the normal conversational flow? Message them by selecting their avatar and using the message button.”
From this page, I click on your avatar, I access to your user page
 I never see this message button

I’ve sent you a PM just now (FYI, by clicking on Message button as shown in the screenshot below). You may continue by replying to me there.

Hello tous. Je dĂ©bute en C, autant dire que je ne rĂ©ussis aucun puzzle :rofl:. Du coup, ce qui m’intĂ©resse c’est juste d’avoir des rĂ©ponses pour apprendre. On peut les trouver quelque part ? J’ai cherchĂ©, mais je n’ai pas trouvĂ© sur la page du jeu. Tx & bonne journĂ©e Ă  tous

You don’t have access to the solutions on this website until you have solved the puzzle in the same programming language yourself.

Hello, je suis dans le mĂȘme cas. Tu as trouvĂ© une solution depuis ?

Merci.

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