728x90

레벨 1 완주하지 못한 선수

from collections import defaultdict

def solution(participant, completion):								
    answer = ''
    hash = defaultdict(int)
    for i in participant:
        hash[i] += 1
    for i in completion:
        hash[i] -= 1
    for i in participant:
        if hash[i] >0:
            answer = i
    
    
    return answer

 

레벨2 전화번호 목록

def solution(phone_book):

    a={}
    for i in phone_book:
       a[i] = 1
       
    for i in phone_book:
        temp=''
        for j in i:
            temp+=j
            if temp in a and temp != i:
                return False    
                
    return True

레벨2 위장

def solution(clothes):

    answer = 1
    a={}
    for i,j in clothes:
        if a.get(j) is None:
            a[j] = 2
        else: 
            a[j]+=1
            
    for i in a.values():
        answer = i*answer
        
    return answer-1

 

레벨3 베스트앨범

def solution(genres, plays):
    answer = []
    a={}
    b={}
    c={}
    for i in range(len(plays)):
        if a.get(genres[i]) is None:
            a[genres[i]]=plays[i]
            b[genres[i]]={}
            b[genres[i]][i]=plays[i]
        else:
            a[genres[i]]+=plays[i]
            b[genres[i]][i]=plays[i]
    genre_rank = sorted(a.items(),reverse=True,key=lambda a:a[1])
    for i,_ in genre_rank:
        song_rank=sorted(b[i].items(),reverse=True,key=lambda a:a[1])
        print(song_rank)
        answer.append(song_rank[0][0])
        if len(song_rank) >1:
            answer.append(song_rank[1][0])
        
        
    return answer
728x90

+ Recent posts