295. 신규 아이디 추천

아현·2021년 9월 8일
0

Algorithm

목록 보기
309/400



참고
,참고1


1. 구현

정해


def solution(new_id):
    # 1단계
    new_id = new_id.lower()
    # 2단계
    answer = ''
    for word in new_id:
        if word.isalnum() or word in '-_.':
            answer += word
    # 3단계
    while '..' in answer:
        answer = answer.replace('..', '.')
    # 4단계
    answer = answer[1:] if answer[0] == '.' and len(answer) > 1 else answer
    answer = answer[:-1] if answer[-1] == '.' else answer
    # 5단계
    answer = 'a' if answer == '' else answer
    # 6단계
    if len(answer) >= 16:
        answer = answer[:15]
        if answer[-1] == '.':
            answer = answer[:-1]
    # 7단계
    if len(answer) <= 3:
        answer = answer + answer[-1] * (3-len(answer))
    return answer




80점



import re
def solution(new_id):
    
    id = new_id.lower()
    id = re.sub(r"[^a-zA-Z0-9._-]","",id)
    if "." in id:
        id = re.sub(r"\.+",".",id)

    if id[0] == ".":
        id = id[1:]
    elif id[-1] == ".":
        id = id[:-1]
    
    if len(id) == 0:
        id = id + "a"
    elif len(id) >= 16:
        id = id[:15]
        if id[-1] == ".":
            id = id[:-1]
    
    if len(id) <= 2:
        last = id[-1]
        while len(id) < 3:
            id += last


    return id


profile
For the sake of someone who studies computer science

0개의 댓글