Problem
Given the participants’ score sheet for your University Sports Day, you are required to find the runner-up score. You are given n scores. Store them in a list and find the score of the runner-up.
Input Format
The first line contains n. The second line contains an array A[] of n integers each separated by a space.
Constraints
- 2 <= n <= 10
- -100 <= a[i] <= 100
Output Format
Print the runner-up score.
Sample Input 0
52 3 6 6 5
Sample Output 0
5
Explanation 0
Given list is [2, 3, 6, 6, 5]. The maximum score is 6 second maximum is 5. Hence, we print 5 as the runner-up score.
Problem Solution in Python3
n = int(input())scores = list(set(list(map(int, input().split()))))new_score = list()for i in scores: if i < max(scores): new_score.append(i)if len(new_score) > 0: runner = max(new_score)elif len(new_score) == 0: runner = None print(runner)
Explanation – Find the Runner-Up Score In Python | HackerRank
In this problem, we are to write a program that find the First Runner-Up Score (the score of the person in the second position).
The line 2 collect the score in a list.
scores = list(set(list(map(int, input().split()))))
Then I went ahead to create a new list containing other scores except the maximum of the given score.
for i in scores: if i < max(scores): new_score.append(i)
The coming lines check if there any score in the newly created list, if there are, it print the maximum of the new list of scores, which is automaticallt that of the First Runner-Up, else print None.
if len(new_score) > 0: runner = max(new_score)elif len(new_score) == 0: runner = None
Comments
Post a Comment