본문 바로가기
Python3/프로그래머스

2022.03.07 [프로그래머스] (python 파이썬) 신규 아이디 추천

by ian's coding 2022. 3. 7.
728x90
반응형

 

https://programmers.co.kr/learn/courses/30/lessons/72410#

 

코딩테스트 연습 - 신규 아이디 추천

카카오에 입사한 신입 개발자 네오는 "카카오계정개발팀"에 배치되어, 카카오 서비스에 가입하는 유저들의 아이디를 생성하는 업무를 담당하게 되었습니다. "네오"에게 주어진 첫 업무는 새로

programmers.co.kr

 


풀이

이 문제는 조건을 문제에서 주어진 대로 설정하면 된다. 

이때 2단계에서 word.isalnum()을 하면 알파벳이나 숫자면 True 아니면 False를 반환한다.

3단계에서 while과 replace를 사용하면 연속된 문자열을 처리하기 간편하다.

4단계에서 strip('.')을 하면 양쪽끝의 '.'을 제거한다.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
def solution(new_id):
    answer=''
    #1단계
    new_id=new_id.lower()
    #2단계
    for word in new_id:
        if word.isalnum() or word in ['_','-','.']:
            answer+=word
    #3단계
    while True:
        if '..' in answer:
            answer=answer.replace('..','.')
        else:
            break
    #4단계
    answer=answer.strip('.')
    #5단계
    if not answer:
        answer='a'
    #6단계
    if len(answer)>=16:
        answer=answer[0:15]
        answer = answer.strip('.')
    #7단계
    while True:
        if len(answer)<=2:
            answer+=answer[-1]
        else:
            break
            
    return answer
cs

 

 

 

 

728x90
반응형

댓글