-
Notifications
You must be signed in to change notification settings - Fork 891
/
problem_082.py
37 lines (30 loc) · 961 Bytes
/
problem_082.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
34
35
36
37
class FileProxy:
def __init__(self, contents):
self.contents = contents
self.offset = 0
self.buffer = ""
def read_7(self):
start = self.offset
end = min(self.offset + 7, len(self.contents))
self.offset = end
return self.contents[start:end].strip()
def read_n(self, n):
while len(self.buffer) < n:
additional_chars = self.read_7()
if not (additional_chars):
break
self.buffer += additional_chars
n_chars = self.buffer[:n]
self.buffer = self.buffer[n:]
return n_chars.strip()
fp = FileProxy("Hello world")
assert fp.read_7() == "Hello w"
assert fp.read_7() == "orld"
assert fp.read_7() == ""
fp = FileProxy("Hello world")
assert fp.read_n(8) == "Hello wo"
assert fp.read_n(8) == "rld"
fp = FileProxy("Hello world")
assert fp.read_n(4) == "Hell"
assert fp.read_n(4) == "o wo"
assert fp.read_n(4) == "rld"