-
Notifications
You must be signed in to change notification settings - Fork 171
/
158.py
33 lines (28 loc) · 1.01 KB
/
158.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
"""
The read4 API is already defined for you.
@param buf, a list of characters
@return an integer
def read4(buf):
# 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'
buf = [' '] * 4 # Create buffer with enough space to store characters
read4(buf) # read4 returns 4. Now buf = ['a','b','c','d'], fp points to 'e'
read4(buf) # read4 returns 4. Now buf = ['e','f','g','h'], fp points to 'i'
read4(buf) # read4 returns 3. Now buf = ['i','j','k',...], fp points to end of file
"""
class Solution:
def __init__(self):
self.queue = []
def read(self, buf, n):
idx = 0
while True:
buf4 = [""]*4
read4(buf4)
self.queue += buf4
curr = min(len(self.queue), n-idx)
for i in range(curr):
buf[idx] = self.queue.pop(0)
idx+=1
if curr == 0:
break
return idx