๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm/Programmers

[Programmers] ์‹ ๊ทœ ์•„์ด๋”” ์ถ”์ฒœ

 

 

๋‚˜์˜ ์ฝ”๋“œ 
def solution(new_id):
    word = ''
    new_word = []
    # 1๋‹จ๊ณ„
    new_id = new_id.lower()
    
    # 2๋‹จ๊ณ„
    for i in new_id:
        if i.isalpha() or i.isdigit() or i in ['-','_','.']:
            word += i
    
    # 3๋‹จ๊ณ„ 
    while '..' in word:
        word = word.replace('..','.')
    
    # 4๋‹จ๊ณ„
    word = word.strip('.')
    
    # 5๋‹จ๊ณ„
    if len(word) == 0:
        word += 'a'
    
    # 6๋‹จ๊ณ„
    if len(word) >= 16:
        word = word[:15]
    if word[-1] == '.':
        word = word.rstrip('.')
        
    # 7๋‹จ๊ณ„
    if len(word) <= 2:
        for k in range(3-len(word)):
            word += word[-1]
    return word
๋ถ„์„ > ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์—ฌ์„œ ๊ธˆ๋ฐฉ ํ‘ผ ๋ฌธ์ œ์˜€๋‹ค. ์กฐ๊ธˆ ํ—ท๊ฐˆ๋ ธ๋˜ ๋ถ€๋ถ„์ด ์žˆ์—ˆ๋‹ค๋ฉด, 3๋‹จ๊ณ„ ๋ถ€๋ถ„์ด์ด์—ˆ๋Š”๋ฐ ๋ฌธ์ž์—ด ์ค‘์— ์—ฐ์†ํ•˜๋Š” ์˜จ์ ์ด ์žˆ์œผ๋ฉด ์ด๋ฅผ ์˜จ์  ํ•˜๋‚˜๋กœ ์น˜ํ™˜ํ•˜๋Š” ๋ถ€๋ถ„์ด์—ˆ๋‹ค. ์ด ๋ถ€๋ถ„์„ ์–ด๋–ป๊ฒŒ ํ•ด์•ผํ• ์ง€ ๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€, ์ƒ๊ฐํ•ด๋ณด๋‹ˆ while๋ฌธ์œผ๋กœ ๋ฌธ์ž์—ด์— '..'์ด ์กด์žฌํ•˜๋ฉด ์ด๋ฅผ ๊ณ„์† '.'๋กœ ๋ฐ”๊ฟ”์ฃผ๋ฉด ๋œ๋‹ค๋Š” ์ƒ๊ฐ์„ ํ•˜๊ฒŒ๋˜์—ˆ๋‹ค. 

 

 

 

 

์ฝ”๋”ฉํ…Œ์ŠคํŠธ ์—ฐ์Šต - ์‹ ๊ทœ ์•„์ด๋”” ์ถ”์ฒœ

์นด์นด์˜ค์— ์ž…์‚ฌํ•œ ์‹ ์ž… ๊ฐœ๋ฐœ์ž ๋„ค์˜ค๋Š” "์นด์นด์˜ค๊ณ„์ •๊ฐœ๋ฐœํŒ€"์— ๋ฐฐ์น˜๋˜์–ด, ์นด์นด์˜ค ์„œ๋น„์Šค์— ๊ฐ€์ž…ํ•˜๋Š” ์œ ์ €๋“ค์˜ ์•„์ด๋””๋ฅผ ์ƒ์„ฑํ•˜๋Š” ์—…๋ฌด๋ฅผ ๋‹ด๋‹นํ•˜๊ฒŒ ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. "๋„ค์˜ค"์—๊ฒŒ ์ฃผ์–ด์ง„ ์ฒซ ์—…๋ฌด๋Š” ์ƒˆ๋กœ

programmers.co.kr