python problem 1: find the runner-up score

Upasana | December 07, 2019 | 1 min read | 16,604 views


Problem Statement

Given the participants' score sheet for your University Sports Day, you are required to find the runner-up score. You are given scores. Store them in a list and find the score of the runner-up.

Note

This is in reference to Python 3

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

5
2 3 6 6 5

Sample Output

5

Solution

We are going to keep logic in main function only such that we are able to build array as we will be getting data from input. You shall create a script.py file and paste the below code in it.

1st line: n = int(input()) This takes input from command line. Please note that input type is going to be in string format and we have to use int() to convert from string to int

2nd line: arr = map(int, input().split()) Now, we are are mapping input(text) to int.

3rd line: arr = list(set(list(arr))) here we are convert array to list and getting its set and then converting to list.

Others: We are sorting the list by using sorted and getting second last element.

Finding 2nd runner up: python
if __name__=="__main__":
    n = int(input())
    arr = map(int, input().split())
    arr = list(set(list(arr)))
    ar = len(arr)
    arr = sorted(arr)
    print(arr[ar-2])

Top articles in this category:
  1. Python coding challenges for interviews
  2. Find extra long factorials in python
  3. Write a python program to find Largest Substring that occurs more than once
  4. Write a program for Hailstone Sequence in Python
  5. Derivative of 1/x & Tossing a coin problem
  6. Pass the ball game: NxN matrix in python
  7. Write a program to check if the given word is Isogram & Pair isogram in python

Recommended books for interview preparation:

Find more on this topic: