|
| 1 | +/* |
| 2 | +
|
| 3 | +
|
| 4 | +Given a file and assume that you can only read the file using a given method read4, implement a method to read n characters. |
| 5 | +Method read4: |
| 6 | +The API read4 reads four consecutive characters from file, then writes those characters into the buffer array buf4. |
| 7 | +The return value is the number of actual characters read. |
| 8 | +Note that read4() has its own file pointer, much like FILE *fp in C. |
| 9 | +Definition of read4: |
| 10 | + Parameter: char[] buf4 |
| 11 | + Returns: int |
| 12 | +
|
| 13 | +buf4[] is a destination, not a source. The results from read4 will be copied to buf4[]. |
| 14 | +Below is a high-level example of how read4 works: |
| 15 | +File file("abcde"); // File is "abcde", initially file pointer (fp) points to 'a' |
| 16 | +char[] buf4 = new char[4]; // Create buffer with enough space to store characters |
| 17 | +read4(buf4); // read4 returns 4. Now buf4 = "abcd", fp points to 'e' |
| 18 | +read4(buf4); // read4 returns 1. Now buf4 = "e", fp points to end of file |
| 19 | +read4(buf4); // read4 returns 0. Now buf4 = "", fp points to end of file |
| 20 | +Method read: |
| 21 | +By using the read4 method, implement the method read that reads n characters from file and store it in the buffer array buf. Consider that you cannot manipulate file directly. |
| 22 | +The return value is the number of actual characters read. |
| 23 | +Definition of read: |
| 24 | + Parameters: char[] buf, int n |
| 25 | + Returns: int |
| 26 | +
|
| 27 | +buf[] is a destination, not a source. You will need to write the results to buf[]. |
| 28 | +Note: |
| 29 | +Consider that you cannot manipulate the file directly. The file is only accessible for read4 but not for read. |
| 30 | +The read function will only be called once for each test case. |
| 31 | +You may assume the destination buffer array, buf, is guaranteed to have enough space for storing ncharacters. |
| 32 | +Example 1: |
| 33 | +Input: file = "abc", n = 4 |
| 34 | +Output: 3 |
| 35 | +Explanation: After calling your read method, buf should contain "abc". We read a total of 3 characters from the file, so return 3. |
| 36 | +Note that "abc" is the file's content, not buf. buf is the destination buffer that you will have to write the results to. |
| 37 | +Example 2: |
| 38 | +Input: file = "abcde", n = 5 |
| 39 | +Output: 5 |
| 40 | +Explanation: After calling your read method, buf should contain "abcde". We read a total of 5 characters from the file, so return 5. |
| 41 | +Example 3: |
| 42 | +Input: file = "abcdABCD1234", n = 12 |
| 43 | +Output: 12 |
| 44 | +Explanation: After calling your read method, buf should contain "abcdABCD1234". We read a total of 12 characters from the file, so return 12. |
| 45 | +Example 4: |
| 46 | +Input: file = "leetcode", n = 5 |
| 47 | +Output: 5 |
| 48 | +Explanation: After calling your read method, buf should contain "leetc". We read a total of 5 characters from the file, so return 5. |
| 49 | +Constraints: |
| 50 | +1 <= file.length <= 500 |
| 51 | +file consist of English letters and digits. |
| 52 | +1 <= n <= 1000 |
| 53 | +
|
| 54 | +*/ |
| 55 | + |
| 56 | +/* |
| 57 | +
|
| 58 | +
|
| 59 | +APPROACH 1 |
| 60 | +
|
| 61 | +
|
| 62 | +As we know, we have to implement the ‘READ’ method using the ‘READ4’ method. |
| 63 | +
|
| 64 | +So first, we call the ‘READ4’ method and pass a ‘TEMP_BUFFER’ array/list of 4 sizes. |
| 65 | +And this method returns the number of characters reads from the ‘FILE’ and all these characters are |
| 66 | +stored in this ‘TEMP_BUFFER’ array/list. When we read the ‘N’ characters from the ‘FILE’ we return the number of actual |
| 67 | +characters that are read from ‘FILE’. |
| 68 | +
|
| 69 | +
|
| 70 | +
|
| 71 | +
|
| 72 | +As we know, the ‘READ’ method is called any number of times. So we have to store the remaining |
| 73 | +characters of the ‘TEMP_BUFFER’ array/list also. So that's why we declare this ‘TEMP_BUFFER’ |
| 74 | +array/list as a global variable. |
| 75 | +
|
| 76 | +
|
| 77 | +
|
| 78 | +Here is the algorithm: |
| 79 | +
|
| 80 | +
|
| 81 | +
|
| 82 | +We declare an array/list ‘TEMP_BUFFER’ of size 4 in which we store the current 4 characters of the ‘FILE’. |
| 83 | +We declare two variables ‘TEMP_POINTER’ and ‘TEMP_COUNT’ where ‘TEMP_POINTER’ points to the location of the |
| 84 | +current character in ‘TEMP_BUFFER’ and ‘TEMP_COUNT’ stores how many characters are present in ‘TEMP_BUFFER’. |
| 85 | +We declare a variable ‘COUNTER’ which represents the number of characters read from the ‘FILE’. |
| 86 | +We run a loop while ‘COUNTER’ less than ‘N’: |
| 87 | +If ‘TEMP_POINTER’ == 0: |
| 88 | +‘TEMP_COUNT’ = ‘READ4[‘TEMP_BUFFER’]. |
| 89 | +If ‘TEMP_COUNT’ == 0: |
| 90 | +Break. |
| 91 | +We run a loop while ‘COUNTER’ less than ‘N’ and ‘TEMP_POINTER’ less than ‘TEMP_COUNT’: |
| 92 | +BUFFER[POINTER] = ‘TEMP_BUFFER[‘TEMP_POINTER’]’ |
| 93 | +POINTER. |
| 94 | +‘TEMP_POINTER’. |
| 95 | +If ‘TEMP_POINTER’ equal to ‘TEMP_COUNT’: |
| 96 | +‘TEMP_POINTER’ = 0. |
| 97 | +Return ‘COUNTER’. |
| 98 | +
|
| 99 | +
|
| 100 | +*/ |
| 101 | + |
| 102 | +abstract class Reader4 { |
| 103 | + int read4(List<int> buf4); |
| 104 | +} |
| 105 | + |
| 106 | +class Solution extends Reader4 { |
| 107 | + int read(List<int> buf, int n) { |
| 108 | + List<int> buf4 = [4]; |
| 109 | + int i4 = 0; // buf4's index |
| 110 | + int n4 = 0; // buf4's size |
| 111 | + int i = 0; // buf's index |
| 112 | + while (i < n) { |
| 113 | + if (i4 == n4) { |
| 114 | + // all characters in buf4 are consumed |
| 115 | + i4 = 0; // reset buf4's index |
| 116 | + n4 = read4(buf4); // read 4 (or less) chars from file to buf4 |
| 117 | + if (n4 == 0) // reach the EOF |
| 118 | + return i; |
| 119 | + } |
| 120 | + buf[i++] = buf4[i4++]; |
| 121 | + } |
| 122 | + |
| 123 | + return i; |
| 124 | + } |
| 125 | + |
| 126 | + @override |
| 127 | + int read4(List<int> buf4) { |
| 128 | + throw UnimplementedError(); |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +class Solution2 extends Reader4 { |
| 133 | + int read(List<int> buf, int n) { |
| 134 | + int copyCharIndex = 0; |
| 135 | + int charRead = 4; |
| 136 | + List<int> buf4 = List.filled(4, 0); |
| 137 | + while (copyCharIndex < n && charRead == 4) { |
| 138 | + charRead = read4(buf4); |
| 139 | + for (var i = 0; i < charRead; i++) { |
| 140 | + if (copyCharIndex == n) { |
| 141 | + return n; |
| 142 | + } |
| 143 | + buf[copyCharIndex++] = buf4[i]; |
| 144 | + } |
| 145 | + } |
| 146 | + return copyCharIndex; |
| 147 | + } |
| 148 | + |
| 149 | + @override |
| 150 | + int read4(List<int> buf4) { |
| 151 | + throw UnimplementedError(); |
| 152 | + } |
| 153 | +} |
0 commit comments