-
Notifications
You must be signed in to change notification settings - Fork 3
/
82_Microsoft_Implement_Read_N.py
executable file
·60 lines (43 loc) · 1.34 KB
/
82_Microsoft_Implement_Read_N.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
"""
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 "".
"""
char_ptr = 0 # pointer to a char in text for read7()
def read7(text):
global char_ptr
res = text[char_ptr:char_ptr+7]
char_ptr +=7
return res
char_buffer = "" # char_buffer for readN()
def readN(text, n):
global char_buffer
while len(char_buffer) < n:
seven_chars = read7(text)
if seven_chars == "":
break
char_buffer += seven_chars
res = char_buffer[:n]
char_buffer = char_buffer[n:]
return res
if __name__ == '__main__':
text = "Hello world"
print("-------Read-7---------")
print(read7(text))
print(read7(text))
print(read7(text))
print("-------Read-2---------")
char_buffer = ""
char_ptr = 0
for _ in range(7):
print(readN(text, 2)) # 'He', 'll', 'o ', 'wo', 'rl', 'd', ''
print("-------Read-8---------")
char_buffer = ""
char_ptr = 0
for _ in range(3):
print(readN(text, 8)) # 'hello wo', 'rld', ''
print("-------Read-4---------")
char_buffer = ""
char_ptr = 0
for _ in range(4):
print(readN(text, 4)) # 'hello wo', 'rld', ''