-
Notifications
You must be signed in to change notification settings - Fork 0
/
082.py
41 lines (32 loc) · 982 Bytes
/
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
38
39
40
41
"""
Problem:
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 "".
"""
# COUNT_7 stores the number of characters already read from the file
# STASHED_TEXT stores the unreturned data (used in readN)
COUNT_7 = 0
STASHED_TEXT = ""
def read7(filename: str = "data_082.txt") -> str:
global COUNT_7
with open(filename, "r") as f:
f.seek(COUNT_7, 0)
data = f.read(7)
COUNT_7 += 7
return data
def readN(n: int, filename: str = "data_082.txt") -> str:
global STASHED_TEXT
for _ in range((n // 7) + 1):
STASHED_TEXT += read7(filename)
text = STASHED_TEXT[:n]
STASHED_TEXT = STASHED_TEXT[n:]
return text
if __name__ == "__main__":
print(readN(3))
print(readN(10))
print(readN(15))
print(readN(25))
print(readN(1000))
print(readN(1000))