-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword_cracker.py
55 lines (41 loc) · 1.44 KB
/
password_cracker.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
#problem link: https://www.hackerrank.com/challenges/password-cracker/problem?isFullScreen=true
import math
import os
import random
import re
import sys
sys.setrecursionlimit(200000)
# here the set recussion limit is a very important part, all tho the time complexity and space complexicity
# is o(n) but the n being too long, we need to set the recustion limit to higher digits
memo = {}
def stepsToSolve(passwords, loginAttempt):
global memo
if loginAttempt == '':
return True, []
if loginAttempt in memo:
return False, []
for password in passwords:
if loginAttempt.startswith(password):
memo[loginAttempt] = True
sol, words = stepsToSolve(passwords, loginAttempt[len(password):])
if sol:
return True, [password] + words
return False, []
def passwordCracker(passwords, loginAttempt):
global memo
memo = {}
sol, words = stepsToSolve(passwords, loginAttempt)
if sol:
return ' '.join(words)
else:
return "WRONG PASSWORD"
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
t = int(input().strip())
for t_itr in range(t):
n = int(input().strip())
passwords = input().rstrip().split()
loginAttempt = input()
result = passwordCracker(passwords, loginAttempt)
fptr.write(result + '\n')
fptr.close()