In this Athlete sort problem, 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.


HackerRank Athlete sort solution in python


Problem solution in Python 2 programming.

N,M = map(int,raw_input().split())
lines = []
for i in xrange(N):
    lines.append(map(int,raw_input().split()))
K = int(raw_input())
lines = sorted(lines,key = lambda x: x[K])
for line in lines:
    print ' '.join(str(k) for k in line)


Problem solution in Python 3 programming.

#!/bin/python3

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())

    P=sorted(arr,key=lambda row:row[k])
    for i in range(len(P)):
        for j in range(len(P[i])):
            print(P[i][j], end=' ')
        print()


Problem solution in pypy programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
N, M = map(int, raw_input().split())
rows = [raw_input() for _ in range(N)]
K = input()

for row in sorted(rows, key=lambda row: int(row.split()[K])):
    print(row)


Problem solution in pypy3 programming.

# Enter your code here. Read input from STDIN. Print output to STDOUT
tbl = []
rc = input()
(r,c) = map(int, rc.split())

for i in range(r):
    _row = input()
    row = list(map(int, _row.split()))
    tbl.append(row)

idx = int(input())
    
tbls = sorted(tbl, key = lambda x: x[idx])
for t in tbls:
    print (*t)