Skip to main content

Parties and Seats

Problem Description​

Given two arrays, one is array of political parties namely - party, and their corresponding array of seats received - seats. You have to print the political parties in lexicographical sorted order along with their seats, and highest seats received by the party.

Input Format: The first line of input contains T denoting the number of testcases. T testcases follow. Each testcase contains three lines of input. The first line contains number of parties N. The second line contains the names of the parties. The third line contains the votes corresponding to each party.

Output Format: For each testcase, in a new line, print the required answer.

User Task: Your task is to complete the function Election2019(party, seats, n) which accepts three arguments and prints the required result.

Examples​

Example 1:

Input: 
1
7
A B C D E F G
90 150 33 23 17 500 2

Output:
A 90
B 150
C 33
D 23
E 17
F 500
G 2
500

Constraints​

  • 1 ≀ T ≀ 100
  • 1 ≀ N ≀ 26
  • 1 ≀ seats ≀ 543

Solution for Parties and Seats​

Code in Different Languages​

Written by @vansh-codes
 def Election2019(party, seats, n):
# Combine party and seats into a list of tuples
v = [(party[i], seats[i]) for i in range(n)]

# Sort the list by party name
v.sort(key=lambda x: x[0])

# Find the maximum seats
max_seats = max(v, key=lambda x: x[1])[1]

# Print the sorted party and seats
for p, s in v:
print(f"{p} {s}")

# Print the maximum seats
print(max_seats)

# Example usage
party = ['A', 'B', 'C', 'A', 'B']
seats = [10, 20, 30, 15, 25]
n = len(party)
Election2019(party, seats, n)

References​