๋ฐ์ํ
๋ฌธ์ ๋งํฌ
๋์ ํ์ด
๊ตฌํ ์คํ์ผ๋ก ์ฐจ๊ทผ์ฐจ๊ทผ ๊ท์น ์กฐ๊ฑด์ ํ์ด๋๊ฐ๋ค.
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๋ ์ข์ง๋ง ์ ๊ท์์ ํ์ฉํด๋ณด์.
๋ฐ์ํ
'Algorithm > Programmers' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก๊ทธ๋๋จธ์ค] ์ฝ์์ ๊ฐ์์ ๋ง์ (level.1) (0) | 2021.09.03 |
---|---|
[ํ๋ก๊ทธ๋๋จธ์ค] ์์ ๋ง๋ค๊ธฐ (level.1) (0) | 2021.09.03 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์์ ๋ํ๊ธฐ (level.1) (0) | 2021.09.02 |
[ํ๋ก๊ทธ๋๋จธ์ค] ์ซ์ ๋ฌธ์์ด๊ณผ ์๋จ์ด (0) | 2021.09.01 |
[ํ๋ก๊ทธ๋๋จธ์ค] ๊ตฌํ - ๋ฌธ์์ด ์์ถ (0) | 2021.09.01 |