[アルゴリズム]プログラマ-新しいアイデンティティを推奨


プログラマー-2012 KAKAO BLIND RECRUITMENT新アイデア推奨

説明する

def solution(new_id):
    # 1단계
    new_id = new_id.lower()

    # 2단계
    for char in new_id:
        if char.isalnum():
            continue
        elif char in ['-', '_', '.']:
            continue
        else:
            new_id = new_id.replace(char, '')


    # 3단계
    while ".." in new_id:
        print("wtf", new_id)
        new_id = new_id.replace("..", ".")

    # 4단계
    new_id = new_id.rstrip(".").lstrip(".")

    # 5단계
    if len(new_id) == 0:
        new_id = "a"

    # 6단계
    if len(new_id) >= 16:
        new_id = new_id[:15]
        if new_id[-1] == ".":
            new_id = new_id[:len(new_id)-1]

    # 7단계
    if len(new_id) <= 2:
        while len(new_id) < 3:
            new_id = new_id + new_id[-1]

    return new_id
最初は、ステップ3でnew id=new id.replace("...")のみを使用する“.”".."歯を残さないように

他人を解く

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
これは正規式を利用したより簡潔な解答です.