[문제]
programmers.co.kr/learn/courses/30/lessons/72410
[풀이]
def solution(new_id):
answer = []
new_id = new_id.lower()
for i in new_id:
if i=='-' or i=='_' or i=='.' or i.isalnum():
answer.append(i)
try:
while True:
check = 0
if answer[0]=='.':
answer = answer[1:]
check = 1
if answer[-1]=='.':
answer = answer[:-1]
check = 1
for a in range(len(answer)-1):
if answer[a]=='.' and answer[a+1]=='.':
check = 1
answer = answer[:a]+answer[a+1:]
break
if not check:
break
except:
pass
if len(answer)==0:
answer = 'a'
if len(answer)>15:
answer = answer[:15]
if answer[-1]=='.':
answer = answer[:-1]
if len(answer)<3:
answer += answer[-1] * (3-len(answer))
return ''.join(answer)
[다른 풀이]
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 '.'
if answer[-1] == '.':
answer = answer[:-1]
# 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
'알고리즘 > 알고리즘 문제' 카테고리의 다른 글
[python] 백준 > 나는야 포켓몬 마스터 이다솜(1620) (0) | 2021.03.04 |
---|---|
[python] 백준 > BFS > 보물섬(2589) (0) | 2021.03.04 |
[python] 백준 > 이중 우선순위 큐(7662) (0) | 2021.03.03 |
[python] 백준 > DP > 1로 만들기(1463) (0) | 2021.03.02 |
[python] 백준 > DFS > 유기농 배추(1012) (0) | 2021.03.01 |