Let’s Code in Python for HackerRank: Sorting Problem

You are given data in a tabular format. The data contains  rows, and each row contains  space separated elements.
You can imagine the  items to be different attributes, (like height, weight, energy, etc.) and each of the  rows as an instance or a sample.
Your task is to sort the table on the th attribute and print the final resulting table.
Note: If two attributes are the same for different rows, print the row that appeared first in the input.
Input Format
The first line contains  and  separated by a space.
The next  lines each contain  elements.
The last line contains .
Output Format
Print the  lines of the sorted table. Each line should contain the space separated elements. Check the sample below for clarity.
Sample Input
5 3
10 2 5
7 1 0
9 9 9
1 23 12
6 5 9
1
Sample Output
7 1 0
10 2 5
6 5 9
9 9 9
1 23 12
#!/bin/usr
# Coded by @VK_Intel for HackerRan

N, M = map(int, input().split(" "))
rows = [input() for i in range(N)]
K = int(input())

class C:
@staticmethod
def func():
for row in sorted(rows, key=lambda row: int(row.split()[K])):
print(row)
return
if __name__ == "__main__":
try:
elem = C()
elem.func()
except Error as e:
print("Error", e)
raise

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s