Temperatures puzzle discussion

Bonjour,
Je pensais à transformer la chaine de caractère en un tableau d’entiers. Pour cela, il me faudrait faire la conversion en pensant au code ASCII. Cette solution me parait un peu compliquée. Quelqu’un a-t-il un algorithme plus simple ?
Merci d’avance pour votre aide précieuse.
Cordialement.

Hey I’m a beginner in C. Were you able to solve the puzzle? I’m unable to convert the string temp into int. Could you tell me how you did it? Thanks

Hi there, I’m a beginner in C and I’ve been trying to solve this puzzle for a long time, but am unable to convert the string temps into an int array. I have tried using atoi, but it doesn’t work. Could you please tell me how you did it? thanks!

Use the Horner algorithm when the char is a digit and store the next one when it’s a space.

Dites moi si je me trompe mas le test comprenant que des nombres négatifs n’aurait pas un problème ?
Lorsque je lance le test j’ai le droit à :
Erreur
Trouvé : 0
Attendu : -
Que dois-je en conclure ?

Je pensais aussi qu’il fallait donner la valeur la plus proche de zero mais pour valider mon premier test j’ai du donner l’indice de position de ma valeur la plus proche de zéro

Il faut bien donner la valeur, pas l’indice.

Effectivement c’est la valeur qu’il faut donner et pas l’indice, le problème que j’ai c’est qu’en lisant l’énoncé j’ai lu un tableau d’entier étais donné en entrée alors que dans le code c’est un tableau de char qui est donné.

Ce qui fais toute la différence et répond à ma question du pourquoi à la console je voyais affiché
Résultat attendu: -

1 Like
int N;
scanf("%d", &N); fgetc(stdin);
int TEMPS[N];
for (int i = 0; i < N; ++i) {
    scanf("%d", &TEMPS[i]);
}
1 Like

Thank you! Solved it now :smile:

Hey guys, I’m actually coding that algorythm in PHP and I’m blocked at the last part, which is to choose the positive temperature between two temperatures (for example -5 and 5), here’s my little code, I’m pretty sure there are some errors or things that may be improved, feel free to tell me !

<?php
fscanf(STDIN, "%d", $n );
$temps = stream_get_line(STDIN, 256 + 1, "\n");
if ($n != 0) {
    $temp = 5526;
    $data = explode(" ", $temps);
    for ($i = 0; $i < $n; $i++) {
     if ($data[$i] == 0) { $temp = 0;
        break; }
     elseif ($data[$i] < 0) {
        $data[$i] = abs($data[$i]);
    }
    if ($data[$i] < $temp) $temp = $data[$i];
    }
}
elseif ($n == 0) $temp = 0;
$t = "-".$temp;
$neg = strpos($temps, $t);
if ($neg === false) echo ($temp."\n");
else echo ($t."\n");
echo $temps;
?>

Thanks for the hint!

Hello everyone! I have a problem.
I done all tasks, but have only 90% of final score. When I saw validators, validator
“5526 alone” colored red, instead of others(green), can You explain me please why this problem occured?

2 Likes

Your code don’t work if the input is :
1
5526

1 Like

Can someone give me some pseudo code that I can use for C++? I am stuck.

Hi ArunJp, first of all let me clarify why your code fails in your case. Thing is, you are trying to access the first element of empty string -> int(temps[0]), which means you are trying to access something that does not exist. One walk around for that is to enclose your code in try except block, such as:
try: a = abs(int(temps[0])) except: #here you handle the above exception, as for example: print('0') # since you have to return *0* if no temperatures are provided

Hope it helps
P.S. sorry could not indent code :slight_smile:

a simpler approach would be checking if array is empty (has a size of zero), before accessing it’s elements

Je peux me tromper mais j’ai l’impression que les solutions attendues sur javascript sont fausses et ce dès le deuxième test.

Tu te trompes :stuck_out_tongue:

1 Like

I think there is an error in the pascal version, it says that it expected 1 and got A… The inputs are not correctly formatted to begin with?

I’ve seen a bug in bash:

  • if I do an echo to temps:
    echo $temps
    1 -2 -8 4 5
  • so I use awk for getting every single temperature. for example:
    echo $temps|awk ‘{print $5}’
    5
  • but if I do with a loop:
    for (( i=1; i<=n; i++ )); do
    actual=echo $temps|awk '{print $i}'
    echo $actual

actual is getting the whole list (1 -2 -8 4 5) instead 1, -2, -8…