package com.fishercoder.solutions;

/**
 * The API: int read4(char *buf) reads 4 characters at a time from a file.
 * 
 * The return value is the actual number of characters read. For example, it returns 3 if there is
 * only 3 characters left in the file.
 * 
 * By using the read4 API, implement the function int read(char *buf, int n) that reads n characters
 * from the file.
 * 
 * Note: The read function will only be called once for each test case.
 */

/**
 * The problem description is pretty ambiguous, actually the problem means to Keep reading until
 * either you have gotten n characters or there is no more characters to read.
 */
public class _157 {
    public int read(char[] buf, int n) {
        int index = 0;
        int next = 0;
        char[] buffer = new char[4];
        while (index < n && (next = read4(buffer)) != 0) {
            for (int i = 0; i < next && index < n; index++, i++) {
                buf[index] = buffer[i];
            }
        }
        return index;
    }

    private int read4(char[] buffer) {
        //this is a fake method to make Eclipse happy
        return 0;
    }
}

@@ -29,24 +29,65 @@
 tions;%0A%0A/**%0A
+ * 157. Read N Characters Given Read4%0A *%0A
  * The API: 
@@ -147,25 +147,24 @@
 m a file.%0A *
- 
 %0A * The retu
@@ -250,19 +250,16 @@
 there is
-%0A *
  only 3 
@@ -285,25 +285,24 @@
 the file.%0A *
- 
 %0A * By using
@@ -390,19 +390,16 @@
 aracters
-%0A *
  from th
@@ -412,291 +412,362 @@
 .%0A *
- %0A * Note: The read function will only be called once for each test case.%0A */%0A%0A/**%0A * The problem description is pretty ambiguous, actually the problem means to Keep
+%0A * Example 1:%0A *%0A * Input: buf = %22abc%22, n = 4%0A * Output: %22abc%22%0A * Explanation: The actual number of characters read is 3, which is %22abc%22.%0A *%0A * Example 2:%0A *%0A * Input: buf = %22abcde%22, n = 5%0A * Output: %22abcde%22%0A *%0A * Note:%0A * The
  read
-ing
  
+f
 un
+c
 ti
-l%0A * either you have gotten n characters or there is no more characters to read.%0A */%0Apublic class _157 %7B%0A
+on will only be called once for each test case.%0A *%0A */%0Apublic class _157 %7B%0A    public static class Solution1 %7B%0A    
     
@@ -807,24 +807,28 @@
 ) %7B%0A        
+  
+  
 int index = 
@@ -834,24 +834,28 @@
  0;%0A        
+    
 int next = 0
@@ -864,16 +864,20 @@
         
+  
+  
 char%5B%5D b
@@ -897,16 +897,20 @@
 har%5B4%5D;%0A
+    
         
@@ -972,16 +972,20 @@
         
+ 
+   
 for (int
@@ -1047,16 +1047,20 @@
         
+    
 buf%5Binde
@@ -1079,34 +1079,42 @@
 i%5D;%0A            
-%7D%0A
+    %7D%0A    
         %7D%0A      
@@ -1111,24 +1111,28 @@
   %7D%0A        
+    
 return index
@@ -1141,11 +1141,19 @@
     
-%7D%0A%0A
+    %7D%0A%0A    
     
@@ -1195,16 +1195,20 @@
         
+   
+ 
 //this i
@@ -1249,24 +1249,28 @@
 ppy%0A        
+    
 return 0;%0A  
@@ -1263,20 +1263,30 @@
   return 0;%0A
+        %7D%0A
     %7D%0A%7D%0A

refactor 157