https://school.programmers.co.kr/learn/courses/30/lessons/72410
https://github.com/stellaluminary/Programmers
요청한 방법을 순서대로 구현한 방법
다만 '.'에 대하여 유의해서 진해해야 함.
def solution(new_id):
tmp = ''
answer = ''
for i in range(len(new_id)):
if new_id[i].isnumeric() or new_id[i] in ['-', '_', '.']:
tmp += new_id[i]
elif new_id[i].isalpha():
tmp += new_id[i].lower()
for i in range(len(tmp)):
if i > 0 and tmp[i] == '.' and tmp[i - 1] == '.':
continue
else:
answer += tmp[i]
if answer[0] == '.':
answer = answer[1:] if len(answer) > 1 else '.'
if len(answer) == 0:
answer += 'a'
elif len(answer) > 15:
answer = answer[:15]
if answer[-1] == '.':
answer = answer[:-1]
if len(answer) < 3:
answer += answer[-1] * (3 - len(answer))
return answer
방법 1보다 직관적으로 진행한 방법
def solution(new_id):
answer = ''
# 1
new_id = new_id.lower()
# 2
for c in new_id:
if c.isalpha() or c.isdigit() or c in ['-', '_', '.']:
answer += c
# 3
while '..' in answer:
answer = answer.replace('..', '.')
# 4
if answer[0] == '.':
answer = answer[1:] if len(answer) > 1 else '.'
# 5
if answer == '':
answer = 'a'
# 6
if len(answer) > 15:
answer = answer[:15]
if answer[-1] == '.':
answer = answer[:-1]
# 7
while len(answer) < 3:
answer += answer[-1]
return answer
정규표현식을 활용한 방법
re.sub(패턴, 바꿀 패턴, string)의 순서임을 확인
정규표현식
import re
def solution(new_id):
st = new_id
st = st.lower()
st = re.sub('[^a-z0-9-_.]', '', st)
st = re.sub('\.+', '.', st)
st = re.sub('^[.]|[.]$', '', st)
st = 'a' if len(st) == 0 else st[:15]
st = re.sub('^[.]|[.]$', '', st)
st = st if len(st) > 2 else st + "".join([st[-1] for i in range(3 - len(st))])
return st