1. 코딩테스트 연습 - 신규 아이디 추천 | 프로그래머스 스쿨 (programmers.co.kr)
코드
.
모범 답안
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
print(answer)
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
2단계 new_id에서 알파벳 소문자, 숫자, 빼기(-), 밑줄(_), 마침표(.)를 제외한 모든 문자를 제거합니다.
막혔던 조건이다. 하지만 or을 사용해 조건을 걸어 쉽게 제거할 수 있었다.
3단계 new_id에서 마침표(.)가 2번 이상 연속된 부분을 하나의 마침표(.)로 치환합니다.
다음 조건도 while과 replace를 사용해 쉽게 치환할 수 있다.
2. 코딩테스트 연습 - [PCCE 기출문제] 10번 / 데이터 분석 | 프로그래머스 스쿨 (programmers.co.kr)
3. 코딩테스트 연습 - 성격 유형 검사하기 | 프로그래머스 스쿨 (programmers.co.kr)
코드 (막힘)
def solution(survey, choices):
score=[0,-3,-2,-1,0,1,2,3]
kakao={"R":0, "T":0, "C":0, "F":0, "J":0, "M":0, "A":0, "N":0}
zipp=zip(survey, choices)
for s, c in zipp:
if score[c]<=0:
kakao[s[0]]+=score[c]
else:
kakao[s[1]]+=score[c]
...
return
딕셔너리에 점수는 넣었으나 2개씩 분리하는 부분에서 막힘
모범 답안
def solution(survey, choices):
result=''
dic= {"R": 0, "T": 0, "C": 0, "F": 0, "J": 0, "M": 0, "A": 0, "N": 0 }
for s,c in zip(survey, choices):
if c>4: dic[s[1]] += c-4
elif c<4: dic[s[0]] += 4-c
score = list(dic.items())
for i in range(0,8,2):
if score[i][1] < score[i+1][1]:
result += score[i+1][0]
else:
result += score[i][0]
return result
'코딩테스트 > programmers (python)' 카테고리의 다른 글
Programmers / 1단계 / 개인정보 수집 유효기간 / python (1) | 2024.01.27 |
---|---|
Programmers / 1단계 / 바탕화면 정리 / python (1) | 2024.01.26 |
20240124/ programmers/ 1단계/ python (0) | 2024.01.24 |
20240123/ programmers/ 1단계/ python (1) | 2024.01.23 |
20240122/ programmers/ 1단계/ python (1) | 2024.01.22 |