Skip to content

Instantly share code, notes, and snippets.

@les-peters
Created July 21, 2022 18:51
Show Gist options
  • Save les-peters/763d07560a1bc7cead17e39ae6877c43 to your computer and use it in GitHub Desktop.
Save les-peters/763d07560a1bc7cead17e39ae6877c43 to your computer and use it in GitHub Desktop.
Hide Email Address
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('example@example.com')
> 'e*****e@example.com'
> hideEmail('example+test@example.co.uk', 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('example@example.com')
hideEmail('example+test@example.co.uk', hideFull = True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment