Password Decryption
In a computer security course, you just learned about password decryption. Your fellow student has created their own password encryption method, and they've asked you to test how secure it is. Your task is to recover the original password given the encrypted password provided to you by your classmate.
Given string s, let s[i] represent the ith character in the string s, using O-based indexing.
- Initially i = 0.
- If s[i] is lowercase and the next character s[i+1] is uppercase, swap them, add a '*' after them, and move to i+2.
- If s[i] is a number, replace it with o, place the original number at the start, and move to i+1.
- Else, move to i+1.
- Stop if iis more than or equal to the string length. Otherwise, go to step 2.
Note:
- The original string always contains digits from 1 to 9 and does not contain 0.
- The original string always contains only alpha-numeric characters.
Function Description
Complete the function decryptPassword in the editor below. decryptPassword must return the original password string before it was encrypted by your classmate.
decryptPassword has the following parameter:
S: the password string after it was encrypted by your classmate.
Constraints
- 1 ≤ length of s ≤ 105
- scan contain Latin alphabet characters (a-z, A-Z), numbers (0-9), and the character '*'.
Solution in Python:
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 | #!/bin/python3 import math import os import random import re import sys # # Complete the 'decryptPassword' function below. # # The function is expected to return a STRING. # The function accepts STRING s as parameter. # def decryptPassword(s): s = list(s) i = 0 while i < len(s) and s[i].isdigit() and s[i] != "0": i += 1 for j, k in enumerate([l for l in range(i, len(s)) if s[l] == "0"]): s[k] = s[i - j - 1] for j in range(i, len(s)): if s[j] == "*": s[j - 1], s[j - 2] = s[j - 2], s[j - 1] return "".join(s[i:]).replace("*", "") if __name__ == '__main__': fptr = open(os.environ['OUTPUT_PATH'], 'w') s = input() result = decryptPassword(s) fptr.write(result + '\n') |