-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMicrosoft_Problem_08.py
40 lines (31 loc) · 1.06 KB
/
Microsoft_Problem_08.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
38
39
40
"""
This problem was asked Microsoft.
Using a read7() method that returns 7 characters from a file, implement readN(n) which reads n characters.
For example, given a file with the content “Hello world”, three read7() returns “Hello w”, “orld” and then “”.
"""
class MyFile:
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:
extra_chars = self.read_7()
if not extra_chars:
break
self.buffer += extra_chars
n_chars = self.buffer[:n]
self.buffer = self.buffer[n:]
return n_chars.strip()
f = MyFile('Hello world')
assert f.read_7() == "Hello w"
assert f.read_7() == "orld"
assert f.read_7() == ""
f = MyFile("Hello world")
assert f.read_n(8) == "Hello wo"
assert f.read_n(8) == "rld"