
âREAD4(char âBUFFER[]â)â
1) It reads 4 characters at a time from the âFILEâ.
2) The return value of the âREAD4â method is the actual number of characters read.
3) For example, if in a âFILEâ only three characters are remaining, then it returns 3.
âREAD(char âBUFFER[]â, int âNâ)â
1) âNâ represents the number of characters to be read from âFILEâ.
2) In this method, you have to store your result in âBUFFERâ.
3) In this method, you have to return the number of characters read from âFILEâ.
âFILEâ = âabcdefâ
Initially file pointer âFPâ points to âaâ.
âREAD4(BUFFER) returns 4 and âBUFFERâ contains âabcdâ, Now the âFPâ points to âeâ.
âREAD4(BUFFER) returns 2 and âBUFFERâ contains âefâ, Now the âFPâ points to the end of the âFILEâ.
The first line of input contains an integer âTâ which denotes the number of test cases or queries to be run. Then the test cases follow.
The first line of each test case contains a string âFILEâ which represents a file that Ninja has to read.
The next line of each test case contains an integer âQâ, representing how many times the âREADâ function is called.
The next lines of each test case contain âQâ space-separated integers which represent how many characters are to be read from the âFILEâ.
For each test case, print the number of characters and actual characters which are read by Ninja from the âFILEâ.
Print the output of each test case in a separate line.
You do not need to print anything; it has already been taken care
of. Just implement the given function.
1 <= âTâ <= 100
1 <= |âFILEâ| <= 5000
âFILE[i]â = {âaâ to âzâ}
Where âTâ denotes the total number of test cases, âFILEâ represents the file that Ninja has to read, and |âFILEâ| represents the length of the âFILEâ.
Time Limit: 1 second
As we know, we have to implement the âREADâ method using the âREAD4â method.
So first, we call the âREAD4â method and pass a âTEMP_BUFFERâ array/list of 4 sizes. And this method returns the number of characters reads from the âFILEâ and all these characters are stored in this âTEMP_BUFFERâ array/list. When we read the âNâ characters from the âFILEâ we return the number of actual characters that are read from âFILEâ.
As we know, the âREADâ method is called any number of times. So we have to store the remaining characters of the âTEMP_BUFFERâ array/list also. So that's why we declare this âTEMP_BUFFERâ array/list as a global variable.
Here is the algorithm: