practivceAlgorithm/programmers

[programmers][Python] 2021 kakao blind 신규아이디 추천

findTheValue 2021. 10. 20. 04:22

규칙에 따라 parsing

 

def solution(new_id):
    answer = ''
    for char in new_id:
        if char.isalpha():
            answer += char.lower()
        elif char == '.' and answer and answer[-1] != '.':
            answer += '.'
        elif char.isdigit() or char in '-_':
            answer += char
    if not answer:
        answer += 'a'
    elif len(answer) > 15:
        answer = answer[:15]
    if answer[-1] == '.':
        answer = answer[:-1]
    while len(answer) < 3:
        answer = answer + answer[-1]
    return answer