Temperatures puzzle discussion

Bonjour,

Les températures sont stockées dans un tableau de str

j’essaie donc de le convertir avec int(temps) mais cela ne fonctionne pas !

Comment puis-je convertir cette valeur ?

Quel langage ?

While solving this problem, I accidentally introduced a bug into my program. If it saw that two temperatures differed in sign, it would always take the positive one – even if it was further from zero. This was clearly the wrong thing to do. But, surprisingly, my “solution” still passed!

A test case where the input closest to zero is negative would have caught this error. Something like [100, -1].

(My program is published if you want to have a look. The older version is the one with the bug.)

1 Like

thank you very much, that really solved the problem with me.

C# is still buggy. The testcase with only -273 is wrong. The number cannot be read out correctly.
I handled it with an if case where (number =! 5526 && n == 1) to catch it.

Oh and the negative numbers as well.
This can be caught by (number == -7 && n == 5)

Salut, je n’arrive pas à comprendre pourquoi mon code (en Ocaml) ne fonctionne pas. Quelqu’un peut m’aider ? Voici mon code :

let n = int_of_string (input_line stdin) in
let temps = input_line stdin in
let tab_temp = Array.make n 5526 in
let t = ref 5526 in
let string_of_char chiffre =
    let nomb = int_of_char chiffre in
    (string_of_int nomb) in
if n <> 0 then begin
    let nb = ref 0 in
    let l = ref 0 in
    for i=0 to (String.length temps)-1 do
        let chiffres = Array.make 4 "" in
        if temps.[i] <> ' ' then begin
            l := !l + 1;
            chiffres.(!l) <- (string_of_char temps.[i])
        end
        else begin
            tab_temp.(!nb) <- (int_of_string (chiffres.(3) ^ chiffres.(2) ^ chiffres.(1) ^ chiffres.(0)));
            nb := !nb + 1;
            l := 0
        end;
    done;
    for i=0 to n-1 do
        let te = tab_temp.(i) in
        if abs !t > abs te then
            t := te
        else if !t = -te then
                t := abs !t;
    done;
end
else t := 0;
print_endline (string_of_int !t);

tu as une exception qui est levée: exception Failure(“int_of_string”)

Je connais pas le Ocaml mais je dirais que ton cast de string en int a un souci.

Looks like this module is broken, or the IDE is broken.

Using python2, a point in my logic becomes:
temp1 = 1
temp2 = 2
if temp1 > temp2:
print “x”
else:
print “y”

“x”
Seems really odd, I’ve never encountered a moment where any language considered 1 > 2 == True before

Do you compare numbers or strings?

Specifically numbers. To have a list I can iterate through, I have:
temps = raw_input()

tempListstr = temps.split()
tempList = []
for i in tempListstr:
tempList.append(int(i))

All calculations are then based on tempList, which is explicitly integers

There should be a bug in your code.
Can you send me your code in private?

I am passing all of the test cases for JavaScript, except for #5 the complex test case. Can anyone tell me why? Here is my code:

var n = parseInt(readline()); // the number of temperatures to analyse
var temps = readline(); // the n temperatures expressed as integers ranging from -273 to 5526
var zero = 0;
var tempArray = temps.split(" ", n);
var distance = Math.abs(tempArray[0] - zero);

if (n !== 0){
for(i = 0; i < tempArray.length; i++){
    var idistance = Math.abs(tempArray[i] - zero);
    if(idistance < distance){
        zero = i;
        distance = idistance;
        }
} var result = tempArray[zero];
print(result);
} 
else {
    print(0);
}

your “zero” is not a zero anymore just after first step

I am using swift 3 and am completely lost on how to extract each individual number right now, someone please help me

my code should work -.- using C++, the code works just fine in every other ide iv’e used, but not here. Pretty upset.

Hello,

I was wondering if you eventually got your code to work, I came up with a similar solution which works just fine on my offline IDE but throws back a syntax error when I run it here.

Which language are you running?

I am running Python 3

Are you sure?
Here, you can run both. Moreover, check the exact version of Python.