Read N Characters Given Read4 - Call multiple times

Posted: 18 Mar, 2021
Difficulty: Moderate

PROBLEM STATEMENT

Try Problem

Ninja has been given a file ‘FILE’ that can be read using a given method, ‘READ4’. Ninja has to implement a method ‘READ’, which reads ’N’ characters from the ‘FILE’ using the ‘READ4’ method.

How ‘READ4’ and ‘READ’ method’s work:

‘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’.

Note: The method ‘READ’ may be called multiple times.

For example:

‘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’.
Input Format
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’.
Output Format :
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.

Note:

You do not need to print anything; it has already been taken care 
of. Just implement the given function.
Constraints:
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
Approach 1

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:

 

  1. We declare an array/list ‘TEMP_BUFFER’ of size 4 in which we store the current 4 characters of the ‘FILE’.
  2. We declare two variables ‘TEMP_POINTER’ and ‘TEMP_COUNT’ where ‘TEMP_POINTER’ points to the location of the current character in ‘TEMP_BUFFER’ and  ‘TEMP_COUNT’ stores how many characters are present in ‘TEMP_BUFFER’.
  3. We declare a variable ‘COUNTER’ which represents the number of characters read from the ‘FILE’.
  4. We run a loop while ‘COUNTER’ less than ‘N’:
    • If ‘TEMP_POINTER’ == 0:
      • ‘TEMP_COUNT’ = ‘READ4[‘TEMP_BUFFER’].
    • If ‘TEMP_COUNT’ == 0:
      • Break.
    • We run a loop while ‘COUNTER’ less than ‘N’ and ‘TEMP_POINTER’ less than ‘TEMP_COUNT’:
      • BUFFER[POINTER] =  ‘TEMP_BUFFER[‘TEMP_POINTER’]’
      • POINTER.
      • ‘TEMP_POINTER’.
    • If ‘TEMP_POINTER’ equal to ‘TEMP_COUNT’:
      • ‘TEMP_POINTER’ = 0.
  5. Return ‘COUNTER’.
Try Problem