"""The read4 API is already defined for you. @param buf4, a list of characters @return an integer def read4(buf4):# Below is an example of how the read4 API can be called.file = File("abcdefghijk") # File is "abcdefghijk", initially file pointer (fp) points to 'a'buf4 = [' '] * 4 # Create buffer with enough space to store charactersread4(buf4) # read4 returns 4. Now buf = ['a','b','c','d'], fp points to 'e'read4(buf4) # read4 returns 4. Now buf = ['e','f','g','h'], fp points to 'i'read4(buf4) # read4 returns 3. Now buf = ['i','j','k',...], fp points to end of file"""classSolution:defread(self,buf,n):""" :type buf: Destination buffer (List[str]) :type n: Number of characters to read (int) :rtype: The number of actual characters read (int) """i=0buf4=[0]*4v=5whilev>=4:v=read4(buf4)forjinrange(v):buf[i]=buf4[j]i+=1ifi>=n:returnnreturni
/** * The read4 API is defined in the parent class Reader4. * int read4(char[] buf4); */publicclassSolutionextendsReader4{/** * @param buf Destination buffer * @param n Number of characters to read * @return The number of actual characters read */publicintread(char[]buf,intn){char[]buf4=newchar[4];inti=0,v=5;while(v>=4){v=read4(buf4);for(intj=0;j<v;++j){buf[i++]=buf4[j];if(i>=n){returnn;}}}returni;}}
/** * The read4 API is defined in the parent class Reader4. * int read4(char *buf4); */classSolution{public:/** * @param buf Destination buffer * @param n Number of characters to read * @return The number of actual characters read */intread(char*buf,intn){charbuf4[4];inti=0,v=5;while(v>=4){v=read4(buf4);for(intj=0;j<v;++j){buf[i++]=buf4[j];if(i>=n){returnn;}}}returni;}};
/** * The read4 API is already defined for you. * * read4 := func(buf4 []byte) int * * // Below is an example of how the read4 API can be called. * file := File("abcdefghijk") // File is "abcdefghijk", initially file pointer (fp) points to 'a' * buf4 := make([]byte, 4) // Create buffer with enough space to store characters * read4(buf4) // read4 returns 4. Now buf = ['a','b','c','d'], fp points to 'e' * read4(buf4) // read4 returns 4. Now buf = ['e','f','g','h'], fp points to 'i' * read4(buf4) // read4 returns 3. Now buf = ['i','j','k',...], fp points to end of file */varsolution=func(read4func([]byte)int)func([]byte,int)int{// implement read below.returnfunc(buf[]byte,nint)int{buf4:=make([]byte,4)i,v:=0,5forv>=4{v=read4(buf4)forj:=0;j<v;j++{buf[i]=buf4[j]i++ifi>=n{returnn}}}returni}}