Created
July 21, 2022 18:51
-
-
Save les-peters/763d07560a1bc7cead17e39ae6877c43 to your computer and use it in GitHub Desktop.
Hide Email Address
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
question = """ | |
Given a string that has a valid email address, write a function to hide | |
the first part of the email (before the @ sign), minus the first and last | |
character. For extra credit, add a flag to hide the second part after the | |
@ sign to your function excluding the first character and the domain extension. | |
Examples: | |
> hideEmail('[email protected]') | |
> 'e*****[email protected]' | |
> hideEmail('[email protected]', hideFull) | |
> 'e**********t@e******.co.uk' | |
""" | |
import re | |
def hideEmail(email, hideFull=False): | |
(name, domain) = re.split(r'@', email) | |
name_chs = [char for char in name] | |
domain_chs = [char for char in domain] | |
mod_name = "" | |
i = 0 | |
for ch in name_chs: | |
if i == 0: | |
mod_name += ch | |
elif i == len(name_chs) - 1: | |
mod_name += ch | |
else: | |
mod_name += '*' | |
i += 1 | |
if hideFull: | |
mod_domain = "" | |
i = 0 | |
flag = True | |
for ch in domain_chs: | |
if ch == ".": | |
flag = False | |
if i == 0: | |
mod_domain += ch | |
elif flag == True: | |
mod_domain += '*' | |
else: | |
mod_domain += ch | |
i += 1 | |
else: | |
mod_domain = domain | |
mod_email = mod_name + "@" + mod_domain | |
print(mod_email) | |
return None | |
hideEmail('[email protected]') | |
hideEmail('[email protected]', hideFull = True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment