카카오에 입사한 신입 개발자 네오는 "카카오계정개발팀"에 배치되어, 카카오 서비스에 가입하는 유저들의 아이디를 생성하는 업무를 담당하게 되었습니다.
"네오"에게 주어진 첫 업무는 새로 가입하는 유저들이 카카오 아이디 규칙에 맞지 않는 아이디를 입력했을 때, 입력된 아이디와 유사하면서 규칙에 맞는 아이디를 추천해주는 프로그램을 개발하는 것입니다.
다음은 카카오 아이디의 규칙입니다.
"네오"는 다음과 같이 7단계의 순차적인 처리 과정을 통해 신규 유저가 입력한 아이디가 카카오 아이디 규칙에 맞는 지 검사하고 규칙에 맞지 않은 경우 규칙에 맞는 새로운 아이디를 추천해 주려고 합니다.
신규 유저가 입력한 아이디가 new_id 라고 한다면,
예를 들어, new_id 값이 "...!@BaT#*..y.abcdefghijklm" 라면, 위 7단계를 거치고 나면 new_id는 아래와 같이 변경됩니다.
1단계 대문자 'B'와 'T'가 소문자 'b'와 't'로 바뀌었습니다.
"...!@BaT#..y.abcdefghijklm" → "...!@bat#..y.abcdefghijklm"
2단계 '!', '@', '#', '' 문자가 제거되었습니다.
"...!@bat#..y.abcdefghijklm" → "...bat..y.abcdefghijklm"
3단계 '...'와 '..' 가 '.'로 바뀌었습니다.
"...bat..y.abcdefghijklm" → ".bat.y.abcdefghijklm"
4단계 아이디의 처음에 위치한 '.'가 제거되었습니다.
".bat.y.abcdefghijklm" → "bat.y.abcdefghijklm"
5단계 아이디가 빈 문자열이 아니므로 변화가 없습니다.
"bat.y.abcdefghijklm" → "bat.y.abcdefghijklm"
6단계 아이디의 길이가 16자 이상이므로, 처음 15자를 제외한 나머지 문자들이 제거되었습니다.
"bat.y.abcdefghijklm" → "bat.y.abcdefghi"
7단계 아이디의 길이가 2자 이하가 아니므로 변화가 없습니다.
"bat.y.abcdefghi" → "bat.y.abcdefghi"
따라서 신규 유저가 입력한 new_id가 "...!@BaT#*..y.abcdefghijklm"일 때,
네오의 프로그램이 추천하는 새로운 아이디는 "bat.y.abcdefghi" 입니다.
신규 유저가 입력한 아이디를 나타내는 new_id가 매개변수로 주어질 때,
"네오"가 설계한 7단계의 처리 과정을 거친 후의 추천 아이디를 return 하도록 solution 함수를 완성해 주세요.
NO | new_id | result |
---|---|---|
예1 | "...!@BaT#*..y.abcdefghijklm" | "bat.y.abcdefghi" |
예2 | "z-+.^." | "z--" |
예3 | "=.=" | "aaa" |
예4 | "123_.def" | "123_.def" |
예5 | "abcdefghijklmn.p" | "abcdefghijklmn" |
입출력 예 #1
입출력 예 #2
1단계 변화 없습니다.
2단계 "z-+.^." → "z-.."
3단계 "z-.." → "z-."
4단계 "z-." → "z-"
5단계 변화 없습니다.
6단계 변화 없습니다.
7단계 "z-" → "z--"
입출력 예 #3
1단계 변화 없습니다.
2단계 "=.=" → "."
3단계 변화 없습니다.
4단계 "." → "" (new_id가 빈 문자열이 되었습니다.)
5단계 "" → "a"
6단계 변화 없습니다.
7단계 "a" → "aaa"
입출력 예 #4
입출력 예 #5
1단계 변화 없습니다.
2단계 변화 없습니다.
3단계 변화 없습니다.
4단계 변화 없습니다.
5단계 변화 없습니다.
6단계 "abcdefghijklmn.p" → "abcdefghijklmn." → "abcdefghijklmn"
7단계 변화 없습니다.
문자열을 조작해서 만들어 보자는 접근
1단계부터 차근차근 7단계까지 만들어 보려했다
def solution(new_id):
for i in range(len(new_id)):
if (ord(new_id[i]) >= 97 and ord(new_id[i]) <= 122)
or (ord(new_id[i]) >= 48 and ord(new_id[i]) <= 57)
or ord(new_id[i]) == 45 or ord(new_id[i]) == 95
or ord(new_id[i]) == 46 :
pass
elif ord(new_id[i]) >= 65 and ord(new_id[i]) <= 90 :
new_id = new_id.replace(chr(ord(new_id[i])),chr(ord(new_id[i])+32))
else :
print(new_id[i])
new_id = new_id.replace(chr(ord(new_id[i])),'')
print(new_id)
return new_id
만들던 중간에 문자열 조작이 복잡하였다
문자열을 리스트로 변경해서 리스트를 조작하며 만들기로함
import copy
def solution(new_id):
cache = ''
list_id = list(new_id)
list_id2 = copy.deepcopy(list_id)
for i in list_id:
if ord(i) >= 65 and ord(i) <= 90 :
list_id2[list_id2.index(i)] = chr(ord(i)+32)
elif (ord(i) >= 0 and ord(i) <= 44) or (ord(i) == 47) or (ord(i) >= 58 and ord(i) <= 64) or (ord(i) >= 91 and ord(i) <= 94) or (ord(i)== 96) or (ord(i) >= 123 and ord(i) <= 126):
list_id2.remove(i)
del_list = []
for i,j in zip(list_id2,range(len(list_id2))):
if i == cache :
del_list.append(j)
cache = i
del_list.sort(reverse=True)
for i in del_list:
del list_id2[i]
if list_id2[0] == '.':
del list_id2[0]
if list_id2[-1] == '.':
del list_id2[-1]
if len(list_id2) == 0:
list_id2.append('a')
if len(list_id2) >= 16:
list_id2 = list_id2[:15]
if list_id2[-1] == '.':
del list_id2[-1]
if len(list_id2) <= 2:
add = list_id2[-1]
for i in range(3-len(list_id2)):
list_id2.append(add)
list_id2 = ''.join(list_id2)
return list_id2
테스트 코드중에 6번 코드가 실패함
아마도 원인은 리스트가 비어있는데도 인덱싱을 시도 하는데 있어서 오류가 난듯함
리스트의 길이를 확인하고 0이 아닐때만 인덱싱을 하도록 변경
import copy
def solution(new_id):
cache = ''
list_id = list(new_id)
list_id2 = copy.deepcopy(list_id)
for i in list_id:
#1단계
if ord(i) >= 65 and ord(i) <= 90 :
print(1)
list_id2[list_id2.index(i)] = chr(ord(i)+32)
#2단계
elif (ord(i) >= 0 and ord(i) <= 44) or (ord(i) == 47) or (ord(i) >= 58 and ord(i) <= 64) or (ord(i) >= 91 and ord(i) <= 94) or (ord(i)== 96) or (ord(i) >= 123 and ord(i) <= 126):
print(2)
list_id2.remove(i)
del_list = []
#3단계
print(3)
for i,j in zip(list_id2,range(len(list_id2))):
if i == cache :
del_list.append(j)
cache = i
del_list.sort(reverse=True)
for i in del_list:
del list_id2[i]
#4단계
print(4)
if len(list_id2) != 0:
if list_id2[0] == '.':
del list_id2[0]
#4단계
print(4)
if len(list_id2) != 0:
if list_id2[-1] == '.':
del list_id2[-1]
#5단계
print(5)
if len(list_id2) == 0:
list_id2.append('a')
print(6)
#6단계
if len(list_id2) >= 16:
list_id2 = list_id2[:15]
if list_id2[-1] == '.':
del list_id2[-1]
print(7)
#7단계
if len(list_id2) <= 2:
add = list_id2[-1]
for i in range(3-len(list_id2)):
list_id2.append(add)
list_id2 = ''.join(list_id2)
return list_id2
테스트 코드는 맞지만 정확도 테스트에서 5문제 실패함
원인을 찾아 보았더니 .의 연속적인 입력을 방지하는 코드가 아닌 모든 문자의 연속입력을 방지하는 코드였음
.만 적용되는 코드로 수정
import copy
def solution(new_id):
cache = ''
list_id = list(new_id)
list_id2 = copy.deepcopy(list_id)
for i in list_id:
#1단계
if ord(i) >= 65 and ord(i) <= 90 :
print(1)
list_id2[list_id2.index(i)] = chr(ord(i)+32)
#2단계
elif (ord(i) >= 0 and ord(i) <= 44) or (ord(i) == 47) or (ord(i) >= 58 and ord(i) <= 64) or (ord(i) >= 91 and ord(i) <= 94) or (ord(i)== 96) or (ord(i) >= 123 and ord(i) <= 126):
print(2)
list_id2.remove(i)
del_list = []
#3단계
print(3)
for i,j in zip(list_id2,range(len(list_id2))):
if i == '.':
if i == cache :
del_list.append(j)
cache = i
del_list.sort(reverse=True)
for i in del_list:
del list_id2[i]
#4단계
print(4)
if len(list_id2) != 0:
if list_id2[0] == '.':
del list_id2[0]
#4단계
print(4)
if len(list_id2) != 0:
if list_id2[-1] == '.':
del list_id2[-1]
#5단계
print(5)
if len(list_id2) == 0:
list_id2.append('a')
print(6)
#6단계
if len(list_id2) >= 16:
list_id2 = list_id2[:15]
if list_id2[-1] == '.':
del list_id2[-1]
print(7)
#7단계
if len(list_id2) <= 2:
add = list_id2[-1]
for i in range(3-len(list_id2)):
list_id2.append(add)
list_id2 = ''.join(list_id2)
return list_id2