Python, a straightforward approach that would be fast to design and implement without bugs during an interview. It is big-O optimal and easy to understand and refactor.
defencrypt(phone_num):# define some const. easy to refactor into method sig or config elsewhere later
ENCRYPT_NUMBER=6ENCRYPT_SYMBOL='X'# we'll return this at the end - reversed and joined.
encrypted=[]# count the numbers we encrypt
counter=0# start at the end and encrypt numbers until we reach ENCRYPT_NUMBER
forchinphone_num[::-1]:# we're done encrypting so pass everything along
ifcounter==ENCRYPT_NUMBER:encrypted.append(ch)continuetry:# if character is a number, encrypt it
int(ch)counter+=1encrypted.append(ENCRYPT_SYMBOL)exceptValueError:# character is not a number so pass it through
encrypted.append(ch)# return a string in the right order
return''.join(encrypted[::-1])print(encrypt("328 6587120"))# 328 6XXXXXX
print(encrypt("212-420-0202"))# 212-4XX-XXXX
print(encrypt("211-458-7851"))# 211-4XX-XXXX
print(encrypt("211 458 7851"))# 211 4XX XXXX
print(encrypt("2114587851"))# 2114XXXXXX
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Python, a straightforward approach that would be fast to design and implement without bugs during an interview. It is big-O optimal and easy to understand and refactor.