Temperatures puzzle discussion


Oh ! ^^

Maybe i read too fast and miss the “-” xD
I’ll look at it.

Either way, Thanks a lot, that should help =)

EDIT : Done ! ^^
I didn’t handle the one number case xD
Work better now =)

Thanks !


My result is coming up as wrong for the “choose the right temperature” and “complex test case” because I’m returning a negative when it’s expecting a positive, the directions say, “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 assume if they are both negative then we should the negative. I’m confused by the directions perhaps, but it seems to me that if you have two -5 then you should return a -5 since there is no positive 5. Can anyone shed some light on this?
If I am supposed to return a positive when I have two negative numbers then the directions are pretty unclear, but can someone help guide me towards how I should approach this? I’m using Python3.


There are no test cases where there are 2 values of -5. If your code is finding 2 values of -5, then there’s something wrong with the code you have that reads the values.

However, if there were a case where there were 2 values of -5 and no other values were closer to 0, -5 would be the correct answer.

You can look at the test cases if you select Expert Mode from the options and then click on the square in the test cases panel.



The OCaml code skeleton is bugged, you give: the following code:

let n = int_of_string (input_line stdin) in (* the number of temperatures to analyse *)
(*let line = input_line stdin in*)

for i = 0 to n - 1 do
    (* t: a temperature expressed as an integer ranging from -273 to 5526 *)
    let t = Scanf.sscanf line "%d" (fun t -> (t)) in

but working over a string sscanf will not “eat” the current char and iterate n time over the same int.

One solution is the following:

  1. replace line by:

    let line = Scanf.Scanning.stdin in

  2. replace the sscanf by a bscanf:

    let t = Scanf.bscanf line "%d " (fun t -> (t)) in

Another solution in a functional way (OCaml programmers like functional structures, it is a big disappointing (and unusual) to be confronted to imperative for loops):

let _n = int_of_string (input_line stdin) in (* the number of temperatures to analyse *)
let line = input_line stdin in
(* t_lst: a list of temperatures expressed as an integer ranging from -273 to 5526 *)
let t_lst = (List.map int_of_string (String.split_on_char ' ' line)) 
(*your code here*)

This should be more expected for an OCaml programmer to code from a List than from a loop (but I can understand you want a very similar pattern for every language).


I don’t have any hard-coded values but validation fails on test case 3. On IDE, everything checks out.


I am a beginner of programming, why I made the mistake here?(I even don’t know how to deal with the first case)

fscanf(STDIN, "%d",
    $n // the number of temperatures to analyse

$inputs = fgets(STDIN);
$inputs = explode(" ",$inputs);

$near = $inputs[0];

for ($i = 0; $i < $n; $i++)
    if ( abs($near) >= abs($inputs[$i+1]) ) {
        $near = abs($inputs[$i+1]);



Well, yes. The $inputs array has indexes ranging from zero to ($n-1) and you iterate the variable $i in the ‘for’ cycle also from zero to ($n-1). So far so good :slight_smile: But the body of ‘for’ is referring to $inputs[$i+1]. This is problematic, because when $i will reach its last value of $n-1, then the $i+1 is equal to $n. Number $n is not one of the valid indexes for $inputs and your program throws an error. You could try to use simply $i instead of $i+1 in ‘for’ construct. This will get your program to run, but the program logic too has to be little bit adjusted to get the correct answers.


Has anybody understood the third test case? I didn’t understand what exactly “Choose the right temperature” means.


Just print the inputs!


Hi everybody,
I’m trying to do this in C++, but cant succeed convert the String in a list of integer ?
I tried atoi but seems, not working, isstreaml too… i dont see how to do, an advice about a class/function ?



I feel like my logic is solid but maybe I’m making a syntax error Can someone help me? I set the program up to separate the positive and negative numbers and now it keeps returning the positive numbers array as being null. I keep looking over the code and can’t see where I went wrong any suggestions. Also not sure if pasting code here is allowed so I’m not.


larryjoe use the int function (https://docs.python.org/3/library/functions.html#int).

I have same failure (5526 alone test) and i can’t understand why!


Input is not printing with System.out.prinltn(i); in the for loop please anybody knows give me reply

#include <stdlib.h>
#include <stdio.h>
#include <math.h>
int main()
int n,temp;
long x[10000];
scanf("%d", &n);
for (int i = 0; i < n; i++)
    scanf("%hd", &x[i]);
return 0;

Why this won’t work???


Lots of stuff…
Undeclared variable.
Renamed the array without renaming all the references to it.
For loop declaration is messed up (bad copy-paste?)
Scanf has a bad format specifier.
Problem with nested parentheses.

Once that’s done, the program will run and you can find the other errors…


can u please offer a python solution for this…?


Hi!!! Would you pls send me your code? I’m totally new here and i cant figure it our with python3… many thanks in advance… pls pls


No. kthxbye. :stuck_out_tongue:


fine :cry:


hi im here again
below is my code… i dont unserstand why this code cant work for the situation in which all temperatures are negative…?

n = int(input())  # the number of temperatures to analyse
a = set()
c = set()
for i in input().split():
    t = int(i)
    c = map(abs,a)
smallest = [n < min(c)+1 for n in c]
list1 = list(compress(a, smallest))
if list1 == []:
    for x in list1: