-
신규 아이디 추천프로그래밍/알고리즘 2021. 3. 27. 15:25
내가 한 풀이
def solution(new_id): delStr = '' new_id = new_id.lower() new_id = list(new_id) for char in new_id: if char.isalnum() or char in '-_.': delStr += char print(delStr) while '..' in delStr: delStr = delStr.replace('..', '.') print(delStr) if delStr.startswith('.'): delStr = delStr[1:] if delStr.endswith('.'): delStr = delStr[:-1] print(delStr) if len(delStr) == 0: delStr += 'a' print(delStr) if len(delStr) >= 16: delStr = delStr[:15] if delStr.endswith('.'): delStr = delStr[:-1] print(delStr) if len(delStr) <= 3: delStr = delStr + delStr[-1] * (3-len(delStr)) return delStr
isalnum()이라는 함수를 알게 됨.
국비지원 학원 다닐 때 도전했었는데 그래도 그때보단 한 두 단계 성장한거 같다.
다른 사람 풀이 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
할까 말까 고민했던 정규식풀이이다.
다른 사람 풀이 2
def solution(new_id): answer = '' # 1 new_id = new_id.lower() # 2 for c in new_id: if c.isalpha() or c.isdigit() or c in ['-', '_', '.']: answer += c # 3 while '..' in answer: answer = answer.replace('..', '.') # 4 if answer[0] == '.': answer = answer[1:] if len(answer) > 1 else '.' if answer[-1] == '.': answer = answer[:-1] # 5 if answer == '': answer = 'a' # 6 if len(answer) > 15: answer = answer[:15] if answer[-1] == '.': answer = answer[:-1] # 7 while len(answer) < 3: answer += answer[-1] return answer
728x90반응형LIST