-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.py
61 lines (53 loc) · 2.03 KB
/
map.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
61
from Printer import Printer
import sys
def parse_int(s):
n = 0
try:
n = int(s)
except ValueError:
s_value = s.strip() if s.strip() else '{empty value}'
Printer.print_error_exit(f"map error: string {s_value} is not an integer")
return n
def validate_map(b):
nums = [parse_int(s) for s in b.split("/")]
dict_count = {i: nums.count(i) for i in nums}
if max(dict_count.values()) > 1:
[Printer.print_error(f'map error: duplicated number {key}') for key, val in dict_count if val > 1]
sys.exit(1)
if list(filter(lambda x: x >= len(nums) or x < 0, nums)):
for n in nums:
if n >= len(nums) or n < 1:
Printer.print_error(f'map error: invalid number {n}: must be in range 0:{int(math.sqrt(nums))}')
sys.exit(1)
def parse_map(file_name):
try:
f = open(file_name)
except FileNotFoundError:
Printer.print_error_exit(f"there is no file {file_name}")
with open(file_name, "r") as file:
bb = ''
line = file.readline()
l_p = line.partition('#')[0]
while not l_p:
line = file.readline()
l_p = line.partition("#")[0]
size_matr = parse_int(l_p)
line = file.readline()
n_str = 1
while line:
line = line.partition('#')[0]
while not line:
line = file.readline()
line = line.partition("#")[0]
plus = '/'.join(line.split())
bb += '/'.join(line.split())
bb += '/' # где конец строки нечего заменять =(
line = file.readline()
if (len(plus.split('/'))) != size_matr:
Printer.print_error_exit(f"invalid map: invalid values number at row {n_str}")
exit(0)
n_str += 1
bb = bb[0: -1]
if (n_str - 1) != size_matr:
Printer.print_error_exit(f'invalid map: invalid rows number = {n_str - 1}')
return bb