Temperatures puzzle discussion

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.
Thanks.

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.

1 Like

Hello,

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
    ...
done;

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)) 
in
(*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).

1 Like

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)
PLEASE HELP

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

 echo($near);

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 ?

Thks

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]);
i=0;
temp=A[0];
for(min=fabs(A[0]);i<n;i++)
if(fabs(A[i]))<fabs(temp)
    temp=A[i];
printf("%d\n",temp);
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)
    a.add(t)
    c = map(abs,a)
smallest = [n < min(c)+1 for n in c]
list1 = list(compress(a, smallest))
if list1 == []:
    print(0)
else:
    for x in list1:
        print(x)

Bonjour, je suis encore débutant en C# et je ne comprend pourquoi mon code me renvoie 0 alors qu’il devrait me renvoyer 1 pour le premier test. Si quelqu’un pouvait m’éclairer s’il vous plait, ce serait très gentil. Merci

if (t<0)
{
Tableau[i]= -1*t;
}
else
{
Tableau[i]= t;
}

        Array.Sort(Tableau);

Console.WriteLine(Tableau[0]);

En fait ici j’essaie de changer les nombres négatif en positif et du coup avoir un tableau que d’entiers positif.
Je sais que ce n’est pas la bonne voie pour résoudre cette exercice mais je deviens fou a ne pas comprendre pourquoi il continue a me retourner 0 alors que si je fais juste:

Tableau[i]=t;
Array.Sort(Tableau);
Console.WriteLine(Tableau[0]);

il me renvoie bien -8, pour le premier test.

Help please