Athlete Sort

Sort by

recency

|

527 Discussions

|

  • + 0 comments
    sorted_lst = sorted(arr, key=lambda x: x[k])
        [print(*i)  for i in (sorted_lst)]
    
  • + 0 comments
        sorted_arr = [arr[i][k] for i in range(n)]
        
        sorted_arr.sort()
        
        for x in sorted_arr:
            j = 0
            while arr[j][k] != x:
                j += 1
            print(*arr[j])
            arr.pop(j)
    
  • + 0 comments
    sort_factor = []
    for i in range(len(arr)):
        sort_factor.append(arr[i][k])
    sort_factor = list(set(sorted(sort_factor)))
    
    
    for j in sort_factor:
        for y in range(len(arr)):
            if j == arr[y][k]:
                print(*arr[y])
                
    
  • + 0 comments

    Solution with our Using Lamda function

    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())))
    
    #taking one duplicate of arr 
    
    arrdup = arr[:]
    
    k = int(input().strip())
    
    
    
    for i in range(0,len(arr)-1):
        for j in range (i+1,len(arr)) :
    
            #("we are now comparing : ", arr[i][k],arr[j][k])
            if arr[i][k] > arr[j][k] :
                   arr[i], arr[j] = arr[j], arr[i]
            if arr[i][k] == arr[j][k] :
                if arrdup.index(arr[i]) > arrdup.index(arr[j]):
                    arr[i], arr[j] = arr[j], arr[i]
    
    
    for i in arr :
        print(*i)
    
  • + 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)))