문제 설명

내 풀이:

def solution(new_id):
    answer = ''
    # 1단계
    new_id = new_id.lower()
    print(new_id)
    # 2단계
    characters = """`~!@#$%^&*()+={}[]|\\:;"'<>,?/"""
    new_id = ''.join(x for x in new_id if x not in characters)
    print(new_id)
    # 3단계
    while ".." in new_id:
        new_id = new_id.replace('..', '.')
    print(new_id)
    # 4단계
    new_id = new_id.strip(".")
    print(new_id)
    # 5단계
    if new_id == '': new_id = 'a'
    print(new_id)
    # 6단계
    while len(new_id) >= 16: new_id = new_id[0:15]
    print(new_id)
    # 7단계
    if len(new_id) <= 2: new_id = new_id.ljust(3, new_id[-1])
    print(new_id)
    return answer

new_id = "=.="
#new_id = "z-+.^."
#new_id = "...!@BaT#*..y.abcdefghijklm"
solution(new_id)
  1. .lower()

  2. ‘’.join()

  3. .replace()

  4. .lstrip()

  5. 슬라이싱을 이용해 15문자 뒤의 문자는 제거

  6. .ljust() 를 이용해 크기가 3이고 new_id[-1]로 오른쪽을 채운 문자열 만듦

다른 사람 풀이:

import re

def solution(new_id):
    st = new_id
    st = st.lower()
    st = re.sub('[^a-z0-9\\-_.]', '', st)
    st = re.sub('\\.+', '.', st)
    st = re.sub('^[.]|[.]$', '', st)
    st = 'a' if len(st) == 0 else st[:15]
    st = re.sub('^[.]|[.]$', '', st)
    st = st if len(st) > 2 else st + "".join([st[-1] for i in range(3-len(st))])
    return st
for c in new_id:
        if c.isalpha() or c.isdigit() or c in ['-', '_', '.']:
            answer += c