일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 신규아이디추천
- 11047번
- 11727번
- 1759번
- 1793번
- programmers
- Spring Security
- 9251번
- 2163번
- WebSecurityConfigurerAdapter
- python
- 백준
- 18406번
- codility
- 프로그래머스
- 영어
- github
- 분할정복
- EBS어학당
- caniuse
- 파이썬
- Java
- 입이 트이는 영어
- SecurityFilterChain
- 2630번
- 1992번
- 알고리즘
- 1057번
- 권주현의 진짜 영국 영어
- BinaryGap
Archives
- Today
- Total
철갑이의 이모저모
[Programmers] 신규 아이디 추천 with Python 본문
728x90
문제
https://programmers.co.kr/learn/courses/30/lessons/72410
코딩테스트 연습 - 신규 아이디 추천
카카오에 입사한 신입 개발자 네오는 "카카오계정개발팀"에 배치되어, 카카오 서비스에 가입하는 유저들의 아이디를 생성하는 업무를 담당하게 되었습니다. "네오"에게 주어진 첫 업무는 새로
programmers.co.kr
풀이
https://docs.python.org/3/library/re.html
re — Regular expression operations — Python 3.10.2 documentation
This module provides regular expression matching operations similar to those found in Perl. Both patterns and strings to be searched can be Unicode strings (str) as well as 8-bit strings (bytes). However, Unicode strings and 8-bit strings cannot be mixed:
docs.python.org
re.sub('정규식패턴', '교체할문자', '문자열') 을 사용해서 풀이
아래의 표는 사용한 정규식을 간단하게 작성한 것이다.
[] | 문자의 집합을 나타낼 때 사용 |
a-z | 소문자를 의미 |
\d | 숫자의 집합을 나타냄 [0-9] 와 같은 의미 |
^ | not 또는 문자의 시작을 의미 |
\ | 특수문자 표현시 앞에 작성 (ex. \_ , \-) |
| | or 의 의미 |
+ | 반복을 의미 |
$ | 문자열의 끝을 의미 |
답
import re
def solution(new_id):
new_id = re.sub('[^a-z\d\-\_\.]','',new_id.lower()) # 1,2단계
new_id = re.sub('\.\.+','.',new_id) # 3단계
new_id = re.sub('^\.|\.$','',new_id) # 4단계
if (len(new_id) == 0) :
new_id = 'a' # 5단계
if (len(new_id) >= 16) :
new_id = re.sub('\.$','',new_id[0:15]) # 6단계
while len(new_id) < 3:
new_id += new_id[-1:] # 7단계
return new_id
728x90
'알고리즘' 카테고리의 다른 글
[Programmers] 체육복 with Python (0) | 2022.01.31 |
---|---|
[Programmers] 숫자 문자열과 영단어 with Python (0) | 2022.01.29 |
[Programmers] 정수 삼각형 with Python (0) | 2020.12.02 |
[Programmers] 모의고사 with Python (0) | 2020.11.24 |
[백준] 11047번(동전 0) with Python (0) | 2020.11.24 |