알고리즘 공부 1일차

김서영·2023년 12월 11일
0

알고리즘

목록 보기
1/25

1. 프로그래머스 - 신고 결과 받기

def solution(id_list, report, k):
    answer = [0 for i in range(len(id_list))]
    id_dict = {id:i for i, id in enumerate(id_list)}
    reported_list = [[] for i in range(len(id_list))]
    for i in range(len(report)):
        fr, to = report[i].split() # fr는 신고하는 사람, to는 신고 당하는 사람
        if fr not in reported_list[id_dict[to]]:
            reported_list[id_dict[to]].append(fr)
    for i in range(len(reported_list)):
        if len(reported_list[i]) >= k:
            for j in range(len(reported_list[i])):
                answer[id_dict[reported_list[i][j]]] += 1
    return answer

리스트 -> 딕셔너리 변환

string_list = ['A','B','C']
dictionary = {string : 0 for string in string_list}
=> {'A': 0, 'B': 0, 'C': 0}
string_list = ['A','B','C']
dictionary = {string : i for i,string in enumerate(string_list)}
=> {'A': 1, 'B': 2, 'C': 3}

2. 프로그래머스 - 개인정보 수집 유효기간

- 풀이 1

def solution(today, terms, privacies):
    answer = []
    terms_dict = {term.split()[0]:int(term.split()[1]) for term in terms }
    year, month, day = map(int, today.split("."))
    for i in range(len(privacies)):
        date, check = privacies[i].split()
        b_year, b_month, b_day = map(int, date.split("."))
        if b_month + terms_dict[check] > 12:
            b_year += (b_month + terms_dict[check]) // 12
            b_month = b_month + terms_dict[check] - ((b_month + terms_dict[check]) // 12) * 12
            if b_month == 0:
                b_year -= 1
                b_month = 12
            if b_day == 1:
                if b_month == 1:
                    b_year -= 1
                    b_month = 12
                    b_day = 28
                else:
                    b_month -= 1
                    b_day = 28
            else:
                b_day -= 1
        else:
            b_month += terms_dict[check]
            if b_day == 1:
                if b_month == 1:
                    b_year -= 1
                    b_month = 12
                    b_day = 28
                else:
                    b_month -= 1
                    b_day = 28
            else:
                b_day -= 1
        if year > b_year:
            answer.append(i+1)
        elif year == b_year:
            if month > b_month:
                answer.append(i+1)
            elif month == b_month:
                if day > b_day:
                    answer.append(i+1)
        
    return answer

일수 계산으로 했으면 훨씬 쉽지 않았을까....ㅋㅋㅋㅋㅋ큐ㅠㅠㅠ
그래서 다시 풀어봤다!!

- 풀이 2

def solution(today, terms, privacies):
    answer = []
    terms_dict = {term.split()[0]:int(term.split()[1]) for term in terms }
    year, month, day = map(int, today.split("."))
    today_value = year * 12 * 28 + month * 28 + day
    for i in range(len(privacies)):
        date, check = privacies[i].split()
        b_year, b_month, b_day = map(int, date.split("."))
        date_value = b_year * 12 * 28 + b_month * 28 + b_day + terms_dict[check] * 28 - 1
        if today_value > date_value:
            answer.append(i+1)
    return answer

3. 프로그래머스 - 성격 유형 검사하기

def solution(survey, choices):
    answer = ''
    score_dict = {'1':3, '2':2, '3':1, '5':1, '6':2, '7':3}
    personality = {'R':0, 'T':1, 'C':2, 'F':3, 'J':4, 'M':5, 'A':6, 'N':7}
    personality_list = ['R', 'T', 'C', 'F', 'J', 'M', 'A', 'N']
    score_list = [0, 0, 0, 0, 0, 0, 0, 0]
    for i in range(len(survey)):
        if choices[i] < 4:
            score_list[personality[survey[i][0]]] += score_dict[str(choices[i])]
        elif choices[i] > 4:
            score_list[personality[survey[i][1]]] += score_dict[str(choices[i])]           
    for i in range(0, 8, 2):
        if score_list[i] > score_list[i+1]:
            answer += personality_list[i]
        elif score_list[i] < score_list[i+1]:
            answer += personality_list[i+1]
        else:
            if personality_list[i] > personality_list[i+1]:
                answer += personality_list[i+1]
            elif personality_list[i] < personality_list[i+1]:
                answer += personality_list[i]
    return answer
profile
개발과 지식의 성장을 즐기는 개발자

0개의 댓글