-
Notifications
You must be signed in to change notification settings - Fork 176
/
solution.py
58 lines (44 loc) · 1.2 KB
/
solution.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
#!/bin/python3
import math
import os
import random
import re
import string
import sys
#
# Complete the 'authEvents' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts 2D_STRING_ARRAY events as parameter.
#
P = 131
M = 10**9 + 7
PP = [P**i for i in range(11)]
APPENDS = [""] + list(string.ascii_letters) + [str(d) for d in range(10)]
def calc_hash(pw):
cur_h = 0
for i in range(len(pw)):
cur_h += ord(pw[-(i+1)]) * PP[i]
return cur_h % M
def authEvents(events):
cur_h = None
good_hashs = None
ans = []
for event, value in events:
if event == "setPassword":
good_hashs = set(calc_hash(value + x) for x in APPENDS)
else:
assert event == "authorize"
ans.append(1 if int(value) in good_hashs else 0)
return ans
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
events_rows = int(input().strip())
events_columns = int(input().strip())
events = []
for _ in range(events_rows):
events.append(input().rstrip().split())
result = authEvents(events)
fptr.write('\n'.join(map(str, result)))
fptr.write('\n')
fptr.close()