Password Decryption - Problem Solving (Basic) certification | HackerRank

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.

At first, it seems impossible. But one day after class, you catch a peek of your classmates notebook where the encryption process is noted. You snap a quick picture of it to reference later. It says this:
Given string s, let s[i] represent the ith character in the string s, using O-based indexing.
  1. Initially i = 0.
  2. 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.
  3. If s[i] is a number, replace it with o, place the original number at the start, and move to i+1.
  4. Else, move to i+1.
  5. Stop if iis more than or equal to the string length. Otherwise, go to step 2.
There's even an example mentioned in the notebook. When encrypted, the string "hack3rr4nk" becomes "43Ah*ckorronk". (Note: the original string, "hack3rr4nk", does not contain the character 0.)

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.
Using this information, your task is to determine the original password (before encryption) when given the encrypted password from your classmate.


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')
Labels : #hackerrank ,#hackerrank certification ,#problem solving ,#problem solving (Basic) ,#python ,

Post a Comment