Athlete Sort

Sort by

recency

|

523 Discussions

|

  • + 0 comments
    #!/bin/python3
    
    import math
    import os
    import random
    import re
    import sys
    
    if __name__ == '__main__':
        # Read the first line: number of rows (n) and number of columns (m)
        nm = input().split()
    
        n = int(nm[0])  # Number of athletes
        m = int(nm[1])  # Number of attributes per athlete
    
        arr = []
    
        # Read the next n lines containing athlete details
        for _ in range(n):
            arr.append(list(map(int, input().rstrip().split())))
    
        # Read the sorting column (k), indexed from 0
        k = int(input())
    
        # Sort the array based on the k-th attribute using a lambda function
        arr.sort(key=lambda x: x[k])
    
        # Print the sorted array
        for row in arr:
            print(" ".join(map(str, row)))
    
  • + 0 comments

    first_multiple_input = input().rstrip().split()

    n = int(first_multiple_input[0]) m = int(first_multiple_input[1])

    arr = []

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

    k = int(input().strip())

    sort by the k-th element and maintain order using the index as a secondary key

    val = sorted(arr, key=lambda x: (x[1][k], x[0]))

    for _, row in val: print(*row)

  • + 0 comments
    first_multiple_input = input().rstrip().split()
    
    n = int(first_multiple_input[0])
    m = int(first_multiple_input[1])
    
    arr = []
    
    for index in range(n):
        arr.append((index, list(map(int, input().rstrip().split()))))
    
    k = int(input().strip())
    
    # sort by the k-th element and maintain order using the index as a secondary key
    val = sorted(arr, key=lambda x: (x[1][k], x[0]))
    
    for _, row in val:
        print(*row)
    
  • + 0 comments

    if name == 'main': first_multiple_input = input().rstrip().split()

    n = int(first_multiple_input[0])
    
    m = int(first_multiple_input[1])
    
    arr = []
    
    for _ in range(n):
        arr.append(list(map(int, input().rstrip().split())))
    
    k = int(input().strip())
    
    val = sorted(arr, key=lambda x: x[k])
    [print(*i) for i in val]
    
  • + 0 comments

    This is the line of code to add

    [print(*el) for el in sorted(arr, key=lambda x: x[k])]