반응형
programmers.co.kr/learn/courses/30/lessons/72410
코딩테스트 연습 - 신규 아이디 추천
카카오에 입사한 신입 개발자 네오는 카카오계정개발팀에 배치되어, 카카오 서비스에 가입하는 유저들의 아이디를 생성하는 업무를 담당하게 되었습니다. 네오에게 주어진 첫 업무는 새로 가
programmers.co.kr

조건대로 따라가면 되는 문제.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
def solution(new_id): | |
# 1. 소문자 치환 | |
new_id = new_id.lower() | |
# 2. 알파벳 소문자, 숫자, -, _, . 빼고 제거 | |
new_id = re.sub(r"[^a-z0-9\-\_\.]","",new_id) | |
# 3. . 두 개 이상이면 하나로 치환 | |
new_id = re.sub(r"[\.]+", ".", new_id) | |
# 4. 마침표가 처음이나 끝에 있으면 제거 | |
if new_id.startswith("."): new_id = new_id[1:] | |
if new_id.endswith("."): new_id = new_id[:-1] | |
# 5. 빈 문자열이면 a 대입 | |
if new_id == "": new_id = "a" | |
# 6. 16자 이상이면, 앞 15글자 제외한 나머지 제거. 제거 후 맨 뒷문자가 .이면 . 제거 | |
if len(new_id) >= 16: | |
new_id = new_id[:15] | |
if new_id.endswith("."): | |
new_id = new_id[:-1] | |
if len(new_id) <= 2: | |
while len(new_id) < 3: | |
new_id += new_id[-1] | |
return new_id |
반응형
'프로그래밍 > 코딩테스트 문제풀이' 카테고리의 다른 글
[Python] 프로그래머스. 2021 카카오 recruit - 순위 검색 (Level 2) (0) | 2021.02.08 |
---|---|
[Python] 프로그래머스. 2021 카카오 recruit - 메뉴 리뉴얼 (Level 2) (0) | 2021.02.02 |
[Python] 프로그래머스. 스타 수열 (Level 3) (0) | 2020.11.13 |
[Python] 프로그래머스. 내적 (Level 1) (0) | 2020.11.12 |
[Python] 프로그래머스. 이진 변환 반복하기 (Level 2) (0) | 2020.11.11 |