1. 문제 설명(달리기 경주)
https://school.programmers.co.kr/learn/courses/30/lessons/178871
# list.index()를 활용해서 풀면 시간초과 발생하여 실패
# 딕셔너리의 키:값을 활용
def solution(players, callings):
position = {}
for i,j in enumerate(players):
position[j] = i # 인덱스(순서)를 값으로 줌
for k in callings:
index = position[k]
if index > 0:
loser = players[index-1]
winner = k
players[index-1] = winner
players[index] = loser
position[winner] -= 1
position[loser] += 1
return players
2. 문제 설명(추억 점수)
https://school.programmers.co.kr/learn/courses/30/lessons/176963
# 딕셔너리 활용 & 무식하게 풀어보기
def solution(name, yearning, photo):
answer = []
albem = {}
for i in range(len(name)):
albem[name[i]] = yearning[i]
for i in range(len(photo)):
k = 0
for j in range(len(photo[i])):
if photo[i][j] in name:
k += albem[photo[i][j]]
answer.append(k)
return answer
3. 문제 설명(덧칠하기)
https://school.programmers.co.kr/learn/courses/30/lessons/161989
def solution(n, m, section):
answer = 0
arr = [0]*len(section)
for i in range(len(section)):
if i == 0:
arr[i] = section[i] + m - 1
answer += 1
elif arr[i - 1] >= section[i]:
arr[i] = arr[i-1]
elif arr[i - 1] < section[i]:
if section[i] + m > n:
answer += 1
break
elif section[i] + m <= n:
arr[i] = section[i] + m - 1
answer += 1
return answer
4. 문제 설명(대충 만든 자판)
https://school.programmers.co.kr/learn/courses/30/lessons/160586
# 딕셔너리 활용해서 키:값 사용
def solution(keymap, targets):
answer = [0]*len(targets)
dic = {}
alpha = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
for i in alpha:
dic[i] = 100
for i in keymap:
for j in i:
if dic[j] >= i.index(j) + 1:
dic[j] = i.index(j) + 1
else:
continue
for i in range(len(targets)):
for j in targets[i]:
if dic[j] != 100:
answer[i] += dic[j]
else:
answer[i] = -1
break
return answer
5. 문제 설명(개인정보 수집 유효기간)
https://school.programmers.co.kr/learn/courses/30/lessons/150370
# 예외를 찾느라 고생한 문제
# 유효기간이 12가 넘는 큰 숫자일 경우와 개인정보 수집한 달이 12월인 경우를 잘 계산하기
def solution(today, terms, privacies):
dic = {}
answer = []
today_year = int(today[:4])
today_month = int(today[5:7])
today_day = int(today[8:10])
for i in terms:
dic[i[0]] = int(i[2:])
for i in range(len(privacies)):
collect_year = int(privacies[i][:4])
collect_month = int(privacies[i][5:7])
collect_day = int(privacies[i][8:10])
if collect_month + dic[privacies[i][-1]] <= 12:
collect_month += dic[privacies[i][-1]]
else:
collect_month += dic[privacies[i][-1]]
collect_year += collect_month//12
collect_month %= 12
if collect_month == 0:
collect_year -= 1
collect_month = 12
if today_year > collect_year:
answer.append(i+1)
elif today_year == collect_year:
if today_month > collect_month:
answer.append(i+1)
elif today_month == collect_month and today_day >= collect_day:
answer.append(i+1)
return answer
6. 문제 설명(성격 유형 검사하기)
https://school.programmers.co.kr/learn/courses/30/lessons/118666?language=python3
# 딕셔너리를 활용, 키값에 대해 불필요한 것 필터링
def solution(survey, choices):
dic = {'R':0,'T':0,'C':0,'F':0,'J':0,'M':0,'A':0,'N':0}
for i in range(len(survey)):
if choices[i] > 4:
if choices[i] == 5:
dic[survey[i][1]] += 1
elif choices[i] == 6:
dic[survey[i][1]] += 2
elif choices[i] == 7:
dic[survey[i][1]] += 3
elif choices[i] < 4:
if choices[i] == 3:
dic[survey[i][0]] += 1
elif choices[i] == 2:
dic[survey[i][0]] += 2
elif choices[i] == 1:
dic[survey[i][0]] += 3
if dic['R'] >= dic['T']:
dic['T'] = 100
else:
dic['R'] = 100
if dic['C'] >= dic['F']:
dic['F'] = 100
else:
dic['C'] = 100
if dic['J'] >= dic['M']:
dic['M'] = 100
else:
dic['J'] = 100
if dic['A'] >= dic['N']:
dic['N'] = 100
else:
dic['A'] = 100
filtered_dic = {key:value for key, value in dic.items() if value != 100}
answer_list = list(filtered_dic.keys())
answer = ''.join(answer_list)
return answer