it works just fine now thanks. The issue was that it threw a syntax error whenever Python encountered an eval() or int() function, It seems to be resolved now though
Anybody mind explaining the behavior of the first two lines to me?
Somehow, when I remove var n = parseInt(readline());
the next lineās behavior changes to fill in for it. Suddenly var temps = readline()
starts pumping out ā5ā in the first test scenario for no apparent reason.
Itās really irritating because ānā is totally unused in my code. I changed line 2 to var temps = readline().split(" ");
so itās rendered into an array, and it works perfectly in every scenario, so I just use that to count the integers instead.
Hi, Iām new to C. I tested my solution with several given arrays on another code writing app and it worked. However it failed here. The algorithm generates 3 instead of 1 in the 1 test. Iām really clueless. Can someone help me?
for(i=0;i<n;i++){
Abs[i]=abs(temps[i]);
fgets(Abs,257,stdin);
}
min=Abs[i-1];
for(i=1;i<n;i++){
if(Abs[i]<min){
min=Abs[i];
}
}
for(i=0;i<n;i++){
if(Abs[i]==min){
position=i;
break;
}
}
readline() reads what is sent in the input stream. n=5 is sent is the input stream in your scenario. If you remove the line that reads it, next line that reads the input stream will read it.
Is there a way to debug the cases we have after submitting (in C language) ?
@niklasnguyen: Because temps is an array of ācharā, and you probably use it as an array of āintā
For C++ option it seems to be the case that for and while loops cannot contain an if statement. Is this a known bug?
int v = 0;
do
{
cerr << "temperature_vect " << temperature_vect[v] << endl;
if( abs(temperature_vect[v] < result))
result = temperature_vect[v];
v += 1;
}while( v<temperature_vect.size() );
I have tried putting an if statement inside of a for loop, a while loop, and a do while loop. Always crashes. I have tested with simple if statements such as if( 1 > 2) still crashes. In C++ there seems to be some sort of bug about putting if statements inside of loopsā¦or perhaps I am loopyā¦
Also note when I comment out if statement the program proceeds.
#include
#include
#include
#include
#include <stdlib.h>
using namespace std;
/**
-
Auto-generated code below aims at helping you parse
-
the standard input according to the problem statement.
**/
int main()
{
int n; // the number of temperatures to analysestring my_int_str; // temporary hold integer string
int input;
int abs_;
int result = 5527;
cin >> n; cin.ignore();
string temps; // the n temperatures expressed as integers ranging from -273 to 5526
getline(cin, temps);cerr << n << endl;
cerr << temps << endl;
cerr << temps[1] << endl;// Only works if this is executed first??? Weird
// If I comment this out program wonāt execute other for loop???
// Itās a mystery
for(int i =0; i<1; i++){cerr << "int i is " << i << endl;
}
for( int y, i = 0; (y+i) < temps.length(); y++ ) {
int t; for( ; (temps[y+i] != ' ') &&( (y+i) < temps.length()); i++ ) { my_int_str = my_int_str + temps[y+i];
}
input = stoi(my_int_str); if( (abs(input)) < abs(result)) { result = input; } else if( ((abs(input)) == abs(result)) && (input > 0)) { result = input; } cerr << "input integer " << input << endl;
my_int_str.clear();
}
// find shortest distance to 0. If a distance repeats then positive temp wins.
// Write an action using cout. DONāT FORGET THE ā<< endlā
// To debug: cerr << āDebug messagesā¦ā << endl;// cout << āresultā << endl;
cout << result << endl;
}
Program wonāt work unless I insert that first ādo nothingā for loop??? Must be hauntedā¦
Youāve got one of the closing parenthesis in the wrong spot.
Hello!
When I click on āPlayā I get everything green
But when I click on āSubmitā it says I have an error ā-273 aloneā. What is the meaning of this error?
I tried to run my code on XCode with N=1 and with a string which only has ā-273ā and the output is ā273ā. So I donāt understand why here I got ā-273 aloneā as an error message.
Thanks
If the input is -273, the output should also be -273.
Do you all have suggestions where I can go for further reading about this?
I have this segmentation fault which I can not seem to get my head around. Any suggestions? Iām using Pascal to solve this.
Maube you try to read a value outside of a list.
And also, the problem seem to emerge when Iām trying to do some custom test with the input as similar negative number, and only restrict to that particular text case.
Hello
Does anyone know what is validators 1, 3, 11 and 12?
Hello, Iām doing this puzzle with Javascript. Do you have any suggestions about how to understand the āmindsetā of this puzzle? For example, I know the solution will include a condition, a loop and an array - at least I suppose so - and that, among all the temps the program will throw, it must choose only the closest to 0. But how does this translate to actual code?
- Use split() to convert a string into an array of string
- Use parseInt() to convert a string into a number
- Use Math.abs() to compare positive and negative number to 0
mmmm I still donāt get itā¦I need to print the temperature closest to 0 among the input data. Ok.
If two numbers are equally distant from 0 the positive one is the closest. Ok.
But there are āNā number of temperatures, all ranging between -273 to 5526. Itās this that I find difficult to understand. The logic. Could you help me?
You are given a number N and a string (TEMPS) that looks like "1 2 3 -7 5526 -273", here N = 6;
there are 6 temperatures to evaluate.
You need to convert the string into an array ["1","2","3","-7","5526","-273"] (split)
Then [1,2,3,-7,5526,-273] (parseInt)
And then use a for-loop (0->N) to find the closest to 0, here 1.