Temperatures puzzle discussion

With C++, to parse a string with multiple numbers separated by spaces, is easy use something like:

int temp;
for (int i = 0; i < n; i++) {
    cin >> temp; // cin skips spaces between values
    // DO something with temp
}

Instead of:

string temps;
getline(cin, temps);
1 Like

Can someone paste their working code for this puzzle in C#?
I did it in a rather strange way and I want to see what other people did.

if you’ve solved the puzzle in C#, you can check other (published) C# solutions.

With PHP trying to output the values of the array with

print_r($temps);

i get numbers higher or lower than -273 to 5526 as mentioned in the description.

Example: -12 -5 -1370 right now on test 2. It changes after reloading the page.

Is that correct?

Hi, my logic to solve this problem (in C#) is:

get the temps into an array.
while there is no temps == 0 i do this :
- get all positive numbers into an array and the others (negatif) into another array.
- get the smalest of each array (positif and negatif)
- compare them and return the samlest.

is it a good way of thinking?
thx in advance

thx Done

C’est bon j’ai réglé ce pb

1 Like

There seems to be an error in the “All Negative Numbers.”
When I execute the code the output is:
Failure
Found: “2”
Expected: “-”

I am working in Swift
It appears that it is responding that “no number” is closest to zero, which is impossible. As I understand the instructions, if the absolute value of two numbers match, then the positive integer is considered closest. So a negative number set would still have a closest number. It appears that the expectation is that negative numbers are not being considered.
Am I doing something wrong?
Thanks for your help

Most of the times, negative numbers start with a negative sign: “-”.

The expected field stops once your answer differ.

The test case name says “All negative numbers” and your program is able to display a positive number, so you may look into it.

You have to output the positive one, only when there is -X and X in the provided data.

Thank you very much for the response.
My question is, if the set is “-5 -3 -6”
The correct answer would be “2”
The puzzle is expecting “-”, which to me seems to be a non answer because you are to return the sequence number of the value closest to zero.

No if the set is “-5 -2 -6” the closest should be -2.

if the set is “-5 -2 -6 2”, we should output 2, because Abs(-2) = Abs(2), so we print the positive one.

Thanks, I was printing its position in the array
Ugh!

I have it ALMOST done(Python3). I just want to know how to format the input into a set.

hey can anyone help? I have this so far:
import sys
import math

# Auto-generated code below aims at helping you parse
# the standard input according to the problem statement.

temps = input()  # the n temperatures expressed as integers in a a strind ranging from -273 to 5526
arry = temps.split(",") #every time a ',' is found in the input, that creates a new element.
arry2 = map(int, arry) # create a map that calls func 'int' to each item in arry
s = set(arry2)#make a new set out of arry2
try:
    #print(s)
    print(min(s, key=lambda x: abs(x)))#find the minimum of the abs vals of each item x in s
# Write an action using print
# To debug: print("Debug messages...", file=sys.stderr)
    print("result")
except ValueError: #if the set has no vals, print 0
    print(0)


but when I run, it would print an actual set(which is why it's commented out. can someone run it, and see if they can fix it?
  temps.sort(function(a, b){return a-b;});
   for(var i = 0; i < n; i++){
   if(temp[i] > 0){
        if(temp[i - 1] < 0){
            var l = temp[i] - 0;
            var t = temp[i - 1] - 0;
            if(l > t || l === t){
            print(temp[i]);
            }
            if(l<t){
                print(temp[i -1]);
            }    
            }
        else{print(temp[i - 1]);}
   }
   else if(temp[i] === 0){
       print(temp[i]);
   }
   }

Any idea why sort() is not working?

every time i try to use the int() function i get the error “invalid literal for int() with base 10: ‘1 -2 -8 4 5’”
Like in this case:
temps = int(raw_input())

The string can’t be translated in an int as is.
You have to convert each right substring in an int.

Where can I report a bug? A valid solution (from Visual Studio IDE) fails on the absolute value test.

Do you use exactly the same code online and in VS or have you changed the part for input/output? How do you feed the input to your VS code?

We have a contact page.

But I’d rather you explain the “bug” here first. It’s very rare to see bugs in classic puzzles (they’ve been solved by many other codingamers and not touched since a while.

Bug test 2

Échec
Trouvé : 5
Attendu : -

???

in JAVA

Maybe a negative number…