Potato
์•ˆ๋…•ํ•˜์„ธ์š”, ๊ฐ์žก๋‹ˆ๋‹ค?๐Ÿฅ” ^___^ ๐Ÿ˜บ github ๋ฐ”๋กœ๊ฐ€๊ธฐ ๐Ÿ‘‰๐Ÿป

Algorithm/Programmers

[ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค] ์‹ ๊ทœ ์•„์ด๋”” ์ถ”์ฒœ (level 1, ์นด์นด์˜ค)

๊ฐ์ž ๐Ÿฅ” 2021. 9. 2. 16:58
๋ฐ˜์‘ํ˜•

 

 

๋ฌธ์ œ๋งํฌ

 

๋‚˜์˜ ํ’€์ด

๊ตฌํ˜„ ์Šคํƒ€์ผ๋กœ ์ฐจ๊ทผ์ฐจ๊ทผ ๊ทœ์น™ ์กฐ๊ฑด์„ ํ’€์–ด๋‚˜๊ฐ”๋‹ค.

def solution(new_id):
    new = ''
    # ๊ทœ์น™1 (๋Œ€->์†Œ)
    new_id = new_id.lower()

    # ๊ทœ์น™2 (์ด๋ชจ์ง€ ์—†์• ๊ธฐ)
    emoj = ["~","!", "@","#","$","%","^","&","*","(",")","=","+","[","]","{","}",":","?",",","<",">","/"]
    for i in new_id:
        if i not in emoj:
            new += i

    #๊ทœ์น™3 (์—ฐ์†์ด๋ฉด์—†์• ๊ณ )
    while '..' in new:
        new = new.replace('..', '.')

    #๊ทœ์น™4 (์ฒ˜์Œ๊ณผ ๋ ์ ์‚ญ์ œ)
    while len(new)>0:
        if new[0] == '.':
            new = new[1:]
        else:
            break
    while len(new) > 0 :
        if new[-1] == '.':
            new = new[:-1]
        else:
            break

    #๊ทœ์น™5(๋ฌธ์ž์—ด๋น„์–ด์ž‡์œผ๋ฉด a์ถ”๊ฐ€)
    if new == '':
        new = 'a'

    #๊ทœ์น™6 (๋ฌธ์ž์—ด์ด 16์ด์ƒ์ด๋ฉด 15๊นŒ์ง€๋Š๊ณ , ๋งˆ์ง€๋ง‰ ์ ์ผ๊ฒฝ์šฐ ์‚ญ์ œ)
    if len(new) >= 16:
        new = new[:15]
        if new[-1] == '.':
            new = new[:-1]

    #๊ทœ์น™7
    while len(new) < 3:
        new += new[-1]

    return new

 

๋‹ค๋ฅธ์‚ฌ๋žŒํ’€์ด

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

 ์™€ ... ์ •๊ทœ์‹์„ ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•˜๋Š” ๊ฑฐ๊ตฌ๋‚˜ ํ•˜๊ณ  ๊ฐํƒ„์ด ๋‚˜์™”๋˜ ์ฝ”๋“œ .. ์•ž์œผ๋กœ ํŠน์ • (๋งŽ์€) ํ…์ŠคํŠธ๋ฅผ ์‚ญ์ œํ•˜๊ณ  ๋Œ€์ฒดํ•ด์•ผํ•˜๋Š” ์ผ์ด ์ƒ๊ธธ๋–„๋Š” replace๋„ ์ข‹์ง€๋งŒ ์ •๊ทœ์‹์„ ํ™œ์šฉํ•ด๋ณด์ž.

๋ฐ˜์‘ํ˜•