-
위클리 챌린지 6주차코딩 테스트/Level 1 2021. 9. 6. 21:48반응형
https://programmers.co.kr/learn/courses/30/lessons/85002
파이썬
def solution(weights, head2head): return list(map(lambda x: x[0], sorted(sorted(sorted([[index + 1, weights[index], sum([1 for char_index, char in enumerate(head2head[index]) if char == 'W' and weights[index] < weights[char_index]]), head2head[index].count('W') / total_count if (total_count := head2head[index].count('W') + head2head[index].count('L')) != 0 else 0] for index in range(len(weights))], key=lambda x: x[1], reverse=True), key=lambda x: x[2], reverse=True), key=lambda x: x[3], reverse=True)))
PEP를 위반한다면 1줄로도 가능....
solution = lambda weights, head2head: list(map(lambda x: x[0], sorted(sorted(sorted([[index + 1, weights[index], sum([1 for char_index, char in enumerate(head2head[index]) if char == 'W' and weights[index] < weights[char_index]]), head2head[index].count('W') / total_count if (total_count := head2head[index].count('W') + head2head[index].count('L')) != 0 else 0] for index in range(len(weights))], key=lambda x: x[1], reverse=True), key=lambda x: x[2], reverse=True), key=lambda x: x[3], reverse=True)))
풀어 쓴다면..
def solution(weights, head2head): ranking = [] for index in range(len(weights)): win_count = 0 for char_index, char in enumerate(head2head[index]): if char == 'W' and weights[index] < weights[char_index]: win_count += 0 w_count, l_count = head2head[index].count('W'), head2head[index].count('L') win_rate = 0 if (total_count := w_count + l_count) != 0: win_rate = w_count / total_count ranking.append([index + 1, weights[index], win_count, win_rate]) ranking.sort(key=lambda x: x[1], reverse=True) ranking.sort(key=lambda x: x[2], reverse=True) ranking.sort(key=lambda x: x[3], reverse=True) return list(map(lambda x: x[0], ranking))
golang
여러 번 소팅하는 것이 좋은 방법은 아니기에...
import "sort" func solution(weights []int, head2head []string) []int { ranking := make([][]int, len(weights)) for i, weight := range weights { temp := make([]int, 4) temp[0], temp[1] = i+1, weight wCount, lCount := 0, 0 for j, v := range head2head[i] { if v == 'W' { wCount++ if weights[i] < weights[j] { temp[2]++ } } else if v == 'L' { lCount++ } } if total := wCount + lCount; total != 0 { temp[3] = 1000000 * wCount / total } ranking[i] = temp } sort.Slice(ranking, func(i, j int) bool { if ranking[i][3] != ranking[j][3] { return ranking[i][3] > ranking[j][3] } if ranking[i][2] != ranking[j][2] { return ranking[i][2] > ranking[j][2] } if ranking[i][1] != ranking[j][1] { return ranking[i][1] > ranking[j][1] } return ranking[i][0] < ranking[j][0] }) result := make([]int, len(weights)) for i, v := range ranking { result[i] = v[0] } return result }
반응형