Information Masking

Given a email address and a phone number you want to mask the identity of this information in a user-friendly way.

The email: Mask every character in the username but the first and last character. There is only every at most 5 asterisks.

The phone number: Mask every number but the last 4 digits. You additionally need to ignore white space, and paranthesis. Dashes should be included in the masking in properly phone number format.

Example:

Input:
jackAndJill@twitter.com
+111 (333) 444-5678

Output: 
j*****l@twitter.com
+***-***-***-5678
def n_digits(str):
    num_digits = 0
    for char in str:
        if char.isdigit():
            num_digits += 1
    return num_digits


def information_mask_phones(phone):
    no_accept = {' ', '(', ')', '-'}

    accept_nums = n_digits(phone) - 4
    phone = reversed(phone)
    phone = [c for c in phone if not no_accept.__contains__(c)]
    cleaned = [c for i, c in enumerate(phone) if i < 4]

    for i in range(0, accept_nums):
        if i % 3 == 0:
            cleaned.append('-')
        cleaned.append('*')

    if '+' in phone:
        cleaned.append('+')

    return ''.join(reversed(cleaned))


def information_mask_email(email):
    email_split = email.split('@')
    name = email_split[0]
    suffix = email_split[1]

    name_start = name[0]
    name_end = name[-1]

    mask_max = min((len(name) - 2), 5)
    remainder = '*' * mask_max

    final = name_start + remainder + name_end + '@' + suffix
    return final

print(information_mask_email("jackAndJill@twitter.com"))
print(information_mask_phones("+111 (333) 444-5678"))
print(information_mask_phones("3334445678"))

Last updated