-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy path157_read_n_characters_given_read4.py
More file actions
56 lines (43 loc) · 1004 Bytes
/
157_read_n_characters_given_read4.py
File metadata and controls
56 lines (43 loc) · 1004 Bytes
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
"""
The read4 API is already defined for you.
@param buf, a list of characters
@return an integer
def read4(buf):
pass
"""
class Solution:
def read(self, buf, n):
"""
:type buf: List[str], Destination buffer
:type n: int, Maximum number of characters to read
:rtype: int, The number of characters read
"""
if not buf or n <= 0:
return 0
i = 0
k = 4
tmp = [0] * k
while i < n and k == 4:
k = read4(tmp)
j = 0
while i < n and j < k:
buf[j] = tmp[j]
i += 1
j += 1
return i
if __name__ == '__main__':
data = 'abcdferrdsjfklsdjfdsr'
n = len(data)
i = 0
k = 4
def read4(buf):
global i
j = 0
while i < n and j < k:
buf[j] = data[i]
i += 1
j += 1
return j
s = Solution()
res = s.read([0] * 4, 4)
print(res)