For those who can’t start Paiza skill check because they don’t understand fgets or STDIN or standard input

Sep 10, 2020 PHP beginner paiza

#Introduction It’s okay until I started learning programming and arrived at Paiza looking for something like a collection of problems in order to improve my skills, but “standard input? Fgets? STDIN? What’s that?” “Standard input value Please refer to the sample code on the following page for the acquisition method. I saw the value acquisition / output sample code, but it doesn’t make sense. “, I couldn’t solve even the D rank problem within 60 minutes at first. Passes.

I’m learning PHP, so I’ll write this article assuming PHP.

If you are an advanced person, and have any tips or supplementary information, please do not hesitate to contact us.

#Standard input The standard input here is the data passed from the Paiza side in the Paiza skill check. At the moment, to check the integrity of your program, we will prepare 10 patterns of data and check the program you submitted. That data must be ready to be loaded into your program as this standard input.

So what you need is an object (thing) called the fgets function and STDIN. What is #fgets fgets is a function and is defined as follows in PHP official.

fgets — Get a line from the file pointer

What is #STDIN? STDIN is like a variable.

#First, take out the first line properly

Suppose that the following data is given. There is a line break on the last line.

1
2
3

To get this value, you can use the code below. It’s the code that is already prepared at the very beginning of Paiza, isn’t it? Of course, $ input_line is the same even if it is $ i.

$ input_line = fgets (STDIN);

Output result ↓

1

It’s hard to understand, but there is a line break after 1. This is not good, so I will trim it using the trim function.

trim — Remove whitespace at the beginning and end of the string

$ input_line = trim (fgets (STDIN));

↑ By doing this, you can retrieve 1 without line breaks or spaces. #Retrieve data that spans multiple lines It was easy to retrieve the first line, but how should we retrieve the second and subsequent lines? Since the fgets function “gets one line from the file pointer”, you can retrieve the second and subsequent lines by repeating the process.

$ a = trim (fgets (STDIN));
$ b = trim (fgets (STDIN));
$ c = trim (fgets (STDIN));
$ d = trim (fgets (STDIN));

echo = $ a; // 1
echo = $ b; // 2
echo = $ c; ///3
echo = $ d; // No data

↑ In this way, the second fgets fetches the second line.

Also, in Paiza, the first line of data that spans multiple lines passes the number of data, and the second and subsequent lines are often the data that is actually processed. An example like the one below. (It’s persistent, but there is a line break on the last line.)

3
1
2
3

↑ The data itself is the same as the previous data, but the information on the number of data (there are 3 lines of data) is inserted in the first line. The number on the first line is used to set the conditions for the iterative syntax of for and while.

$ input_line = trim (fgets (STDIN)); // $ input_line is assigned 3

for ($ i = 0; $ i <$ input_line; $ i ++) {// ← Used to set repeated conditions
 // Here is what you want to process
}

↑ If you do this, the processing in for will be repeated 0, 1, 2 and 3 times.

Also, I think that data will be used there as a matter of course, so I will write fgets (STDIN) in the for statement ↓.

$ input_line = trim (fgets (STDIN)); // $ input_line is assigned 3

for ($ i = 0; $ i <$ input_line; $ i ++) {
 $ a = trim (fgets (STDIN)); // Since 3 on the first line is already used above, it is assigned line by line from 1
}

↑ In this way, the data in the first line is used as the number of data, and the second and subsequent lines are taken out as actual data and cooked.

By the way, here ↓ is Paiza’s value acquisition / output sample code, PHP version. I will quote. If you look at it like this, the first three lines may be easier to understand. ?? From the 4th line, we just code the acquired data according to the problem.

2
twenty five
3 4
$ input_line = trim (fgets (STDIN));
for ($ i = 0; $ i <$ input_line; $ i ++) {
    $ s = trim (fgets (STDIN));
    $ s = str_replace (array ("\ r \ n", "\ r", "\ n"),'', $ s);
    $ s = explode ("", $ s);
    echo "hello =". $ S [0]. ", world =". $ S [1]. "\ N";
}

#Finally It’s a very rudimentary content, but I was able to sort out the points I had stumbled upon in the past. I hope it helps someone. Thank you for your suggestions.