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
62
63
64
65
|
import sys
import json
import jsonschema
def nearest_valid_point(x: int, y: int, points: list[list[int]]) -> int:
nearest_index = -1
distance: int
for i in range(len(points)):
this_distance: int
if points[i][0] == x:
#print("x matches")
this_distance = y - points[i][1]
elif points[i][1] == y:
#print("y matches")
this_distance = x - points[i][0]
else:
continue
#print(f'this distance {this_distance}')
if this_distance < 0:
this_distance = -this_distance
#print(f'this distance {this_distance} nearest_index {nearest_index}')
if nearest_index < 0 or this_distance < distance:
#print('setting')
distance = this_distance
nearest_index = i
return nearest_index
def main() -> None:
validator = jsonschema.Draft202012Validator({
"type": "object",
"properties": {
"x": {"type": "integer"},
"y": {"type": "integer"},
"points": {
"type": "array",
"items": {
"type": "array",
"prefixItems": [{"type": "integer"},{"type": "integer"}],
"minItems": 2,
"items": False,
},
},
},
"required": {"x","y","points"},
})
inputs: list[str] = sys.argv[1:]
for input_string in inputs:
try:
input_object = json.loads(input_string)
except Exception as inst:
print(inst)
continue
try:
validator.validate(input_object)
except jsonschema.exceptions.ValidationError as inst:
print(inst.message)
continue
result = nearest_valid_point(**input_object)
print(f'{input_string:<30} -> {result}')
if __name__ == '__main__':
main()
|