Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.70 KB | None | 0 0
  1. #!/bin/python3
  2.  
  3. DEBUG = False
  4.  
  5. import math
  6. import os
  7. import random
  8. import re
  9. import sys
  10.  
  11. #
  12. # Complete the 'passwordCracker' function below.
  13. #
  14. # The function is expected to return a STRING.
  15. # The function accepts following parameters:
  16. # 1. STRING_ARRAY passwords
  17. # 2. STRING loginAttempt
  18. #
  19.  
  20. def trie_traverse(trie, path):
  21. matches = []
  22. node = trie
  23. pwd = ''
  24.  
  25. while path:
  26. if node.get(path[0]) is not None:
  27. pwd = pwd + path[0]
  28.  
  29. if len(path) == 1:
  30. matches.append(pwd)
  31. break
  32.  
  33. node = node[path[0]]
  34. path = path[1:]
  35. else:
  36. if trie.get(path[0]) is None or not node.get('$'):
  37. matches = None
  38. break
  39. else:
  40. matches.append(pwd)
  41. pwd = ''
  42. node = trie
  43.  
  44. return matches
  45.  
  46. def trie_inc(node, path):
  47. if not node.get(path[0]):
  48. node[path[0]] = {}
  49.  
  50. if len(path) == 1:
  51. node[path[0]]['$'] = True
  52.  
  53. if len(path) > 1:
  54. trie_inc(node[path[0]], path[1:])
  55.  
  56. def passwordCracker(passwords, loginAttempt):
  57. trie = {}
  58.  
  59. for pwd in passwords:
  60. trie_inc(trie, pwd)
  61.  
  62. answers = trie_traverse(trie, list(loginAttempt))
  63.  
  64. return ' '.join(answers) if answers else 'WRONG PASSWORD'
  65.  
  66. if __name__ == '__main__':
  67. if DEBUG:
  68. lines = [l for l in open('in4.txt', 'r')]
  69. t = int(lines[0])
  70. lines = lines[1:]
  71. i = 0
  72. while i < len(lines):
  73. n = int(lines[i])
  74. passwords = lines[i+1].rstrip().split()
  75. loginAttempt = lines[i+2].rstrip()
  76. result = passwordCracker(passwords, loginAttempt)
  77. print(result)
  78. i = i+3
  79. else:
  80. fptr = open(os.environ['OUTPUT_PATH'], 'w')
  81.  
  82. t = int(input().strip())
  83.  
  84. for t_itr in range(t):
  85. n = int(input().strip())
  86.  
  87. passwords = input().rstrip().split()
  88.  
  89. loginAttempt = input()
  90.  
  91. result = passwordCracker(passwords, loginAttempt)
  92.  
  93. fptr.write(result + '\n')
  94.  
  95. fptr.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement