-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserFriendlyPassword.py
45 lines (36 loc) · 1.05 KB
/
UserFriendlyPassword.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
#
# Complete the 'authEvents' function below.
#
# The function is expected to return an INTEGER_ARRAY.
# The function accepts 2D_STRING_ARRAY events as parameter.
#
import string
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__':
events_rows = int(input().strip())
events_columns = int(input().strip())
events = []
for _ in range(events_rows):
events.append(input().rstrip().split())
result = authEvents(events)
print('\n'.join(map(str, result)))
print('\n')