Skip to content

Commit 522c199

Browse files
Sort the table
Signed-off-by: Chandra Prakash S <[email protected]>
1 parent 98bcf16 commit 522c199

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

Session-5/sortedtable.py

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
"""You are given a spreadsheet that contains a list of athletes and their details (such as age, height, weight and so on). You are required to sort the data based on the th attribute and print the final resulting table. Follow the example given below for better understanding.
2+
(Image: image)
3+
Note that is indexed from to , where is the number of attributes.
4+
Note: If two attributes are the same for different rows, for example, if two atheletes are of the same age, print the row that appeared first in the input.
5+
Input Format
6+
The first line contains N and M separated by a space.
7+
The next N lines each contain M elements.
8+
The last line contains K.
9+
Constraints
10+
1≤N,M≤1000
11+
0≤K<M
12+
Each element≤1000
13+
Output Format
14+
Print the N lines of the sorted table. Each line should contain the space separated elements. Check the sample below for clarity.
15+
16+
Logical Test Cases
17+
Test Case 1
18+
INPUT (STDIN)
19+
5 3
20+
10 2 5
21+
7 1 0
22+
9 9 9
23+
1 23 12
24+
6 5 9
25+
1
26+
Test Case 2
27+
INPUT (STDIN)
28+
5 2
29+
10 2
30+
7 1
31+
9 9
32+
1 23
33+
6 5
34+
1
35+
36+
EXPECTED OUTPUT
37+
7 1 0
38+
10 2 5
39+
6 5 9
40+
9 9 9
41+
1 23 12
42+
43+
EXPECTED OUTPUT
44+
7 1
45+
10 2
46+
6 5
47+
9 9
48+
1 23"""
49+
50+
n, m = map(int, input().strip().split(' '))
51+
52+
# read the data into a list of tuples
53+
data = []
54+
for i in range(n):
55+
row = tuple(map(int, input().strip().split(' ')))
56+
data.append(row)
57+
58+
# sort the data based on the k-th attribute
59+
k = int(input().strip())
60+
data = sorted(data, key=lambda x: x[k])
61+
62+
# print the sorted data
63+
for row in data:
64+
print(" ".join(map(str, row)))

0 commit comments

Comments
 (0)