This puzzle appears to be bugged on the last test case when submitted. It’s not in all languages and it appears somewhat inconsistent (the same solution fails and then passes and then fails). A couple of users reported it and I saw it too with my old solutions.
In some cases, outputting 0 as a string instead of a variable (i.e. Console.WriteLine(“0”) instead of using a variable) causes it to pass.
I’m getting an error on my STOI line. Appreciate any hints!
int main()
{
int y=1; // current substring start position
int x=0; // loop variable
int curTemp; // current temp to be evaluated variable
int result=0; // lowest absolute temp
int n; // the number of temperatures to analyse
cin >> n; cin.ignore();
string temps; // the n temperatures expressed as integers ranging from -273 to 5526
getline(cin, temps);
// Write an action using cout. DON'T FORGET THE "<< endl"
// To debug: cerr << "Debug messages..." << endl;
if (n==0) {
result = 0;
}
else while (x<=n) {
x = temps.find (" ", y);
curTemp = stoi(temps.substr (y, x));
if (y=0) {
result = abs(curTemp);
}
else if (abs(curTemp)<result) {
result=abs(curTemp);
}
y=x++;
}
cout << "result" << endl;
The issue was that for the validator, the input only contained one line (0) where it should contain two lines: the “0” on the first line followed by an empty line. Depending on 1) the language used, 2) where the condition on n == 0 was done, 3) how the parsing of the temperatures line was done, it would fail or not when the first line was 0.
salut la communauté, je suis nouveau et apprend ET à coder ET à utiliser le forum…
donc voilà sur ce problème j’ai essayé un truc qui me renvoie une foultitude de messages d’erreurs!!
=> où puis-je laisser mon code pour avoir votre avis?
s’il vous plaît ne me donnez pas la solution toute faite je voudrais chercher un peu… mettez moi sur la voie !!!
salut à tous
je débute sur le site. Avec ce code en VB.net j’ai beaucoup de message d’erreur! pouvez vous me donner un coup de main? Sans me donner la réponse (j’aimerais trouver !!) mais au moins me mettre sur la piste!
merci !
Module Solution
’ Auto-generated code below aims at helping you parse
’ the standard input according to the problem statement.
Sub Main ()
Dim n as Integer
n = Console.ReadLine() ' the number of temperatures to analyse
Dim temps as String
temps = Console.ReadLine() ' the n temperatures expressed as integers ranging from -273 to 5526
' Write an action using Console.WriteLine()
' To debug: Console.Error.WriteLine("Debug messages...")
dim i, tplus, tmoins, tfinal as integer
tplus = 5527
tmoins = -274
if n > 0
for i = 1 to n
if temps >= 0
if temps < tplus
tplus = temps
end if
elseif temps < 0
if temps > tmoins
tmoins = temps
end if
end if
next
elseif
tfinal = 0
end if
if tplus > math.abs(tmoins)
tfinal = tmoins
elseif tplus < math.abs(tmoins)
tfinal = tplus
elseif tplus = math.abs(tmoins)
tfinal = tplus
end if
Console.WriteLine(tfinal)
End Sub
I don’t know anything about VB.net . What looks weird is that you’re not using your variable i. Is this correct to read the ith temperature like you do?
You can check in the details page of the puzzle, there is a resources section. You can find there a video from Master Hellish who resolved it in PHP (but he usually solves puzzles with VB.net)
If you ask in english, there will likely be more people who will help us
At first, temps is not an array, so you can’t iterate on it, don’t forget to use split. At second, as Thibaud said, there are missing indexes inside the for-next loop (temps(i)). At third, at the case of zero temperatures tfinal=0 is ignored and rewritten by the tmoins
I have a problem with one task - “Complex test case”.
Here’s my code:
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
int n = in.nextInt(); // the number of temperatures to analyse
in.nextLine();
String temps = in.nextLine(); // the n temperatures expressed as integers ranging from -273 to 5526
if(n > 0){
//PL: Przerobienie stringa na tablicę intów
//EN: String made into an int array of temperatures
String[] stringArray = temps.split(" ");
int[] temperatury = new int[stringArray.length];
for(int i = 0; i < stringArray.length; i++){
String numberAsString = stringArray[i];
temperatury[i] = Integer.parseInt(numberAsString);
}
//PL: Porównanie intów w tablicy i wybór najmniejszego |x|
//EN: Comparison of the temperatures and choosing the closest to 0
int tmin = Math.abs(temperatury[0]);
int imin = 0;
for(int i = 0; i < temperatury.length; i++){
if(Math.abs(temperatury[i]) < tmin){
tmin = temperatury[i];
imin = i;
}
}
//PL: Namierzanie drugiej takiej samej |wartości|, jeśli istnieje
//EN: Checking if there's any other temp, which abs is equal to the chosen one's
int imin2 = 0;
for(int i = 0; i < temperatury.length; i++){
if(Math.abs(temperatury[i]) == Math.abs(tmin) && i != imin){
imin2 = i;
}
}
//PL: Wybór większej jeśli są dwie
//EN: Choosing the positive one if there are two
if(temperatury[imin] > temperatury[imin2]){
System.out.println(temperatury[imin]);
}
else if (temperatury[imin] < temperatury[imin2]){
System.out.println(temperatury[imin2]);
}
else
System.out.println(temperatury[imin]);
}
else
System.out.println("0");
}
}
Could anyone explain what “Complex test case” exactly is?
I suppose it’s just about changing what’s inside " " in split method to make it work. But what?
hice algo como esto en C# y corre perfecto. seguramente se podría hacer mas corto el código.
class Solution
{
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 temp_min_negativa=-273;
int temp_min_positiva=5526;
int resultado=0;
string[] temps_substrings = temps.Split(’ ');
Well, here is what I was able to come up with in C# for 100%.
class Solution
{
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
string[] arrTemps = temps.Split(' ');
int result = 0;
if(n == 0)
{
result = 0;
} else if(n == 1)
{
result = int.Parse(arrTemps[0]);
} else
{
int curVal = int.Parse(arrTemps[0]);
for(int i = 0; i < n-1; i++)
{
int nextVal = int.Parse(arrTemps[i+1]);
if(Math.Abs(curVal) > Math.Abs(nextVal))
{
curVal = nextVal;
} else if (curVal == nextVal)
{
curVal = curVal;
} else if (Math.Abs(curVal) == Math.Abs(nextVal))
{
curVal = Math.Abs(curVal);
}
result = curVal;
}
}
Console.WriteLine(result);
}
JavaScript help? I keep failing and I know my code is good. I’ve run in in codepen and on my own computer just fine, but when I run the test cases I get the following error:
Failure
Found: '1 ’
Expected: ‘1’
var n = parseInt(readline()); // the number of temperatures to analyse
var temps = readline(); // the n temperatures expressed as integers ranging from -273 to 5526
if (temps.length < 1 || temps === null || temps === undefined) var result = 0;
else {
var tempArr = temps;
console.log(tempArr);
for (var i in tempArr) {
if(!isNaN(tempArr[i])){
console.log(tempArr[i]);
if (!result) var result = tempArr[i];
if(tempArr[i] == ' ');
else if (Math.abs(tempArr[i]) === Math.abs(result)) {
console.log("Absolute values of " + tempArr[i] + " and " + result + " are the same. Storing positive value if available");
if (tempArr[i] > result) {
console.log(tempArr[i] + " is greater than " + result + ", storing " + tempArr[i]);
result = tempArr[i];
}
} else if (Math.abs(tempArr[i]) < Math.abs(result)) result = tempArr[i];
console.log("Result is " + result)
}
}
}
console.log('Type: ' + typeof result + ' , Length: ' + result.length);
result = Number(result);
console.log(typeof result);
// Write an action using print()
// To debug: printErr('Debug messages...');
print(result);
Hi Bus42,
It looks like because you are using console.log(), for debugging purposes you should use printErr(). And it analyzes your input starting from console.log(tempArr) where the sequence is ‘1 -2 -8 4 5’, that’s why you receive:
Found: '1 ’
Bonjours a tous,(langage C ) je ne comprend pas pourquoi sa fonctionne pas, j’ai tester sur un compilateur gcc en local et il fonctionne, or sur l’ide ça ne fonctionne pas et je ne comprend pas pourquoi, voici mon code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/**
* Auto-generated code below aims at helping you parse
* the standard input according to the problem statement.
**/
int main()
{
int rpos = 5526;
int rneg = -273;
int moinrneg;
int final ;
int i;
int n; // the number of temperatures to analyse
scanf("%d", &n); fgetc(stdin);
int temps[257]; // the n temperatures expressed as integers ranging from -273 to 5526
fgets(temps, 257, stdin); // the n temperatures expressed as integers ranging from -273 to 5526
// Write an action using printf(). DON'T FORGET THE TRAILING \n
// To debug: fprintf(stderr, "Debug messages...\n");
for(i = 0; i < n; i++)
{
if(temps[i] > 0)
{
if(temps[i] < rpos)
{
rpos = temps[i];
}
}
else if(temps[i] < 0)
{
if(temps[i] > rneg)
{
rneg = temps[i];
}
}
else
{
rpos = 0;
rneg = 0;
}
}
moinrneg = rneg;//rneg "sauvegarde" dans moinrneg
rneg = -rneg;//rneg devient positif
if(rpos < rneg)
{
final = rpos;
}
else if(rneg < rpos)
{
final = moinrneg;
}
else if(rneg == rpos)
{
final = rpos;
}
else if(temps == 0)
{
final = 0;
}
printf("%d\n", final);
return 0;
}