Question: Athlete Sort - Hacker Rank (Python)
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 a better understanding.
Note that K is indexed from 0 to (M-1), where M is the number of attributes.
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.
Constraints
1 <= N, M <= 1000
0 <= K < M
Each element <= 1000
Output Format
Print the N lines of the sorted table. Each line should contain space-separated elements. Check the sample below for clarity.
Sample Input 0
5 3
10 2 5
7 1 0
9 9 9
1 23 12
6 5 9
1
Sample Output 0
7 1 0
10 2 5
6 5 9
9 9 9
1 23 12
The details are sorted based on the second attribute since K is zero-indexed.
Possible Solutions
Sorting in programming is simply arranging the items in either ascending order or descending order. There are many possible algorithms that help us in sorting. But in our case, we will use built-in methods to sort the items.
Now we will move toward the possible solutions that we can use in order to solve this question.
Solution-1: Using map() and lambda function
Let us now solve the question in a simple way. We will use the map and lambda function in our solution. Also, we will use sorted() function to sort the elements. Let's start the solution by importing the required modules.
# initiailzing map function
N, M = map(int, input().split())
# taking for rows
rows = [input() for _ in range(N)]
# taking input from user
K = int(input())
# sorting using sorted function
for row in sorted(rows, key=lambda row: int(row.split()[K])):
print(row)
Sample input:
5 3
10 2 5
7 1 0
9 9 9
1 23 12
6 5 9
1
Output:
7 1 0
10 2 5
6 5 9
9 9 9
1 23 12
As you can see, we get the desired output values. Let us now use the same input values and sort the data based on the very first column.
Input:
5 3
10 2 5
7 1 0
9 9 9
1 23 12
6 5 9
0
Output:
1 23 12
6 5 9
7 1 0
9 9 9
10 2 5
As you can see, now the data is sorted based on the very first column.
Solution-2: Using __name__ variable
This time we will change the above solution a little bit and will solve the question. We will use the __name__ variable and apply the solution under it. Python files can act as either reusable modules or as standalone programs. if __name__ == “main”: is used to execute some code only if the file was run directly, and not imported. Let us see how we can use it in our solution:
# using __name__ variable
if __name__ == "__main__":
# Taking the input from the user
n, m = input().strip().split(' ')
# storing the input as list
n, m = [int(n), int(m)]
arr = []
# looping throug
for arr_i in range(n):
# taking the data from the user
arr_t = [int(arr_temp) for arr_temp in input().strip().split(' ')]
arr.append(arr_t)
# taking the input from user
k = int(input().strip())
# using the sorted function to sort
sorted_arr = sorted(arr, key = lambda x : x[k])
# using for loop to print the sorted values
for row in sorted_arr:
print(' '.join(str(y) for y in row))
Sample input:
5 3
10 2 5
7 1 0
9 9 9
1 23 12
6 5 9
2
Output:
7 1 0
10 2 5
9 9 9
6 5 9
1 23 12
As you can see, the data is sorted based on the last column as we specified in the input values.
Solution-3: Using user-defined function
A user-defined function is a function that is defined by the user. We will now create a function that will print out the sorted data based on the index number of the column.
def athlete_sort():
# initiailzing map function
N, M = map(int, input().split())
# taking for rows
rows = [input() for _ in range(N)]
# taking input from user
K = int(input())
# sorting using sorted function
for row in sorted(rows, key=lambda row: int(row.split()[K])):
print(row)
# Calling the function
athlete_sort()
Sample input:
5 3
10 2 5
7 1 0
9 9 9
1 23 12
6 5 9
1
Output:
7 1 0
10 2 5
6 5 9
9 9 9
1 23 12
As you can see, the data is sorted based on the second column as specified.
Summary
In programming, sorting is arranging the data in either ascending or descending order. In this short article, we learned multiple ways to solve the athlete sort question from Hacker Rank.
References
Python programming language
Hacker Rank: Athlete Sort
great work!!