Prepare > Python > Built-Ins > Athlete Sort

2023. 10. 1. 19:58HackerRank-Python

 

Athlete Sort | HackerRank

Sort the table on the kth attribute.

www.hackerrank.com

 

문제


You are given a spreadsheet that contains a list of N athletes and their details (such as age, height, weight and so on). You are required to sort the data based on the Kth attribute and print the final resulting table. Follow the example given below for better understanding.

Note: If two attributes are the same for different rows, for example, if two atheletes are of the same age, print the row that appeared first in the input.

Input Format

The first line contains N and M separated by a space.
The next N lines each contain M elements.
The last line contains K

Output Format

Print the N lines of the sorted table. Each line should contain the space separated elements. Check the sample below for clarity.

 

=> N명의 선수에 대한 속성M개를 줄건데, K번째 속성값으로 선수들을 정렬해 출력하라

 

 

 

 

코드


import math
import os
import random
import re
import sys

if __name__ == '__main__':
    nm = input().split()

    n = int(nm[0])

    m = int(nm[1])

    arr = []

    for _ in range(n):
        arr.append(list(map(int, input().rstrip().split())))

    k = int(input())


ordered_arr = sorted(arr, key=lambda x:x[k])

for i in ordered_arr:
    print(*i)

 

 

 

 

노트


import math
import os
import random
import re
import sys

if __name__ == '__main__':
    nm = input().split()

    n = int(nm[0])

    m = int(nm[1])

    arr = []

    for _ in range(n):
        arr.append(list(map(int, input().rstrip().split())))

    k = int(input())




ordered_arr = sorted(arr, key=lambda x:x[k])

print(*(i for i in ordered_arr), sep='\n')
더보기
[7, 1, 0]
[10, 2, 5]
[6, 5, 9]
[9, 9, 9]
[1, 23, 12]
  • comprehension 구문? 사용할 때는 감싸주는게 필수

 

 

 

 

참조


 

 

Prepare > Python > Collections > Company Logo

Company Logo | HackerRank Print the number of character occurrences in descending order. www.hackerrank.com 문제 A newly opened multinational brand has decided to base their company logo on the three most common characters in the company name. They are n

my-little-diary.tistory.com