Temperatures puzzle discussion

Si une seule valeur est envoyée, tu dois la retourner telle quelle.

Don’t I have to at some point convert the input String into an int? (Java). I’m having trouble doing this.
int num = Integer.parseInt(temps);
That’s what I initially put on there, but it doesn’t work. Mainly because there are multiple numbers. How can I convert them in order to later be able to compare absolute values?
Or, do I have to do this at all?

Anyway, fairly beginner programmer here, could use a hint.

Thanks.

This should get you started:

    String temps= in.nextLine();
    String[] tempArr = temps.split(" ");
    int first = Integer.parseInt(temps[0]);
  • danBhentschel
1 Like

You sire, are my new hero.
BTW, they are making a movie of that book.

I am working on the Temperatures puzzle in Java on the website, and when I go to compile it gives me the following error message. Thanks for your help!

Exception in thread “main” java.lang.NumberFormatException: For input string: “1 -2 -8 4 5”
at java.lang.NumberFormatException.forInputString on line 65
at java.lang.Integer.parseInt on line 580
at java.lang.Integer.valueOf on line 766
at Solution.main on line 22

One question dealing with this puzzle do you think I need to create another integer in the puzzle or do I need to convert the string temps into an integer to work with int n?

Also its in C# programming language

You can split the TEMPS string in a string array (string[] or List) and convert each item in int.

Thanks SaikskyApo you really helped me out… One more question how do you split it do I have to make another string name. Because I’m trying but, it’s not coming out right here’s what I have right now:

static void Main(string[] args)
    {

        int n = int.Parse(Console.ReadLine()); // the number of temperatures to analyse
        string temps = Console.ReadLine(); // the n temperatures expressed as integers ranging from -273 to 5526
        int[] tempsO = new int[n];

        int t = Convert.ToInt32(Console.ReadLine());
        string[] temps1 = new string[(t)temps];
          

        try
        {
            
            
        }
        catch
        {
            Console.WriteLine("ERROR, my friend... Don't let me freeze!");
        }
            //int.Parse(Console.ReadLine());


        // Write an action using Console.WriteLine()
        // To debug: Console.Error.WriteLine("Debug messages...");

        Console.WriteLine("result");
        Console.WriteLine("PAK");
        Console.ReadKey();
    }

Also it’s in C#…

C# have a Split method:

temps.Split() will give you a String[], you will have to call Int.Parse (or your Convert.ToInt32) in every item of the new array.

You can also use LinQ functionalities to get an int[] easily :
int[] tempsInt = temps.Split().Select(Int.Parse).ToArray()

Guys I am completley stuck. Culd you help with code for Java? I am beginner.

if you’re not telling us what’s blocking you, we’ll have a hard time helping you. Check the history of this topic, there should be plenty of hints to help you manage the puzzle.

Would it be possible that there is still a malfunction with the making system for this code? On the test samples I get 100% however when submitted it is returned with a 81 or 91% success depending on alterations I make.

Would there be any way to ask for someone to check the code?

Bonsoir,

Je ne comprends pas pourquoi mon printf l’afficherait les " " ou les “-”

Sortie standard :
1 -2 -8 4 5

code :
while (temps[i] != ‘\0’)
{
if (temps[i] == " " || temps[i] == “-”)
{
i++;
}
else
{
printf("%c",temps[i]);
i++;
}
}

You’re comparing chars (temps[i]) to pointers ("-" is a C string literal, typed as a char pointer). You probably intend to compare to character literals, singled quoted '-'.

Good evening to every one

Well im trying to learn to program (javaScript)) and i may need some help, sorry if my mistakes are too silly. i wrote this to solve the puzzle but it doesn´t work. can you tell me what is wrong with it? Thanks a lot

var n = parseInt(readline()); // the number of temperatures to analyse
var temps = readline(); // the n temperatures expressed as integers ranging from -273 to 5526

for (var i=0; i <= n-1; i++) {
var valorMin = 0;
var absoluto = [Math.abs (temps[n])]; // i create an Array with the absolute values of the array temps
valorMin = Math.min(absoluto) // then i take the minimum value of the absolute array
}

print(valorMin);

I found one interesting solution in Java 8. I think it’s the top rated solution.

I am very puzzle by ONE piece:
[no code spoiler please]

As I understand it, ‘b’ is the next item to be added, and everything after -> determines HOW it will be sorted. The first condition is that the two items are not equal (in absolute value).

Assuming that’s true, the the absValue of that item is subtracted from the item it’s tested against ?¿?¿?¿

I hope my question is clear. If not I can try again.

I think there’s an error in the PHP version of this on Test Case 02 “Only Negative Numbers”. The output states “Output Display 0 (zero) if no temperatures are provided. Otherwise, display the temperature closest to 0.” However when I run the code it fails because it’s expecting a non number
Failure
Found: 5
Expected: -

A negative number is a number too. A negative number often starts with a negative sign like ‘-’.