Temperatures puzzle discussion

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 analyse

    string 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 :wink:
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.
2 Likes