728x90
def solution(new_id):
answer = ''
# 1
new_id = new_id.lower()
# 2
for n in new_id:
if n.islower() or n.isdigit() or n in ['-', '_', '.']:
answer += n
# 3
while ".." in answer:
answer = answer.replace('..', '.')
# 4
if answer[0] == '.':
answer = answer[1:]
if answer[-1] == '.':
answer = answer[:-1]
# 5
if len(answer) == 0:
answer = 'a'
# 6
if len(answer) >= 16:
answer = answer[:15]
# 7
if len(answer) <= 2:
s = answer[-1]
while len(answer) == 3:
answer += s
return answer
print(solution("...!@BaT#*..y.abcdefghijklm"))
import re
def solution(new_id):
st = new_id
st = st.lower() #1단계
st = re.sub('[^a-z0-9\-_.]', '', st) #2단계
st = re.sub('\.+', '.', st) #3단계
st = re.sub('^[.]|[.]$', '', st) #4단계
st = 'a' if len(st) == 0 else st[:15] #5&6단계
st = re.sub('^[.]|[.]$', '', st) #6단계
st = st if len(st) > 2 else st + "".join([st[-1] for i in range(3-len(st))])#7단계
return st
print(solution("...!@BaT#*..y.abcdefghijklm"))
728x90