python problem 1: find the runner-up score

Carvia Tech | April 26, 2019 | 1 min read | 60 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. Python Flask Interview Questions
  3. Top 100 interview questions on Data Science & Machine Learning
  4. Google Data Scientist interview questions with answers
  5. Find extra long factorials in python
  6. Write a program for Hailstone Sequence in Python
  7. Pass the ball game: NxN matrix in python



Find more on this topic:
Machine Learning image
Machine Learning

Data science, machine learning, python, R, big data, spark, the Jupyter notebook, and much more

Last updated 1 week ago


Recommended books for interview preparation:

This website uses cookies to ensure you get the best experience on our website. more info