a = [1, 10, 5, 7, 6] a.sort() a # [1, 5, 6, 7, 10] a.sort(reverse=True) a # [10, 7, 6, 5, 1]
1 2 3 4 5 6 7 8
defsolution(participant, completion): participant.sort() completion.sort() for i inrange(len(completion)): if participant[i] != completion[i]: return participant[i] return participant[i+1]
defsolution(phone_book): for i inrange(len(phone_book)): for j inrange(i+1, len(phone_book)): if phone_book[i].find(phone_book[j]) == 0or phone_book[j].find(phone_book[i]) == 0: returnFalse returnTrue
startswith(시작하는문자, 시작지점)
1 2 3 4 5 6
defsolution(phone_book): for i inrange(len(phone_book)): for j inrange(i+1, len(phone_book)): if phone_book[i].startswith(phone_book[j]) == Trueor phone_book[j].startswith(phone_book[i]) == True: returnFalse returnTrue
defsolution(clothes): closet = {} result = 1 for element in clothes: key = element[1] value = element[0] if key in closet: closet[key].append(value) else: closet[key] = [value] # {'headgear': ['yellow_hat', 'green_turban'], 'eyewear': ['blue_sunglasses']} for key in closet.keys(): result = result * (len(closet[key]) + 1) return result - 1
다른사람 풀이
1 2 3 4 5 6
defsolution(clothes): from collections import Counter from functools import reduce cnt = Counter([kind for name, kind in clothes]) answer = reduce(lambda x, y: x*(y+1), cnt.values(), 1) - 1 return answer
for i inrange(len(genres)): if genres[i] in music.keys(): music[genres[i]] += plays[i] list_data = [i, plays[i]] list_dict[genres[i]].append(list_data) else: music[genres[i]] = plays[i] list_data = [[i, plays[i]]] list_dict[genres[i]] = list_data
res = sorted(music.items(), key=(lambda x: x[1]), reverse = True)
first_genres = [] second_genres = []
max = 0 second_value = 0 first = 0 second = 0
for idx in list_dict[res[0][0]]: print(idx) ifmax < idx[1]: ifmax !=0: second_value = max second = first first = idx[0] max = idx[1] elif second_value < idx[1]: second = idx[0] second_value = idx[1] first_genres = [first, second]
max = 0 second_value = 0 first = 0 second = 0
for idx in list_dict[res[1][0]]: ifmax < idx[1]: ifmax !=0: second_value = max second = first first = idx[0] max = idx[1] elif second_value < idx[1]: second = idx[0] second_value = idx[1] second_genres = [first, second] return first_genres + second_genres
defsolution(genres, plays): genre_cnt_dict = {} for genre, play inzip(genres, plays): if genre in genre_cnt_dict.keys(): genre_cnt_dict[genre] += play else: genre_cnt_dict[genre] = play print(genre_cnt_dict)