2 <= n <= 10 -100 <= A[i] <= 100
python problem 1: find the runner-up score
Carvia Tech | 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.
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
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.
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:
- Python coding challenges for interviews
- Find extra long factorials in python
- Write a python program to find Largest Substring that occurs more than once
- Write a program for Hailstone Sequence in Python
- Derivative of 1/x & Tossing a coin problem
- Pass the ball game: NxN matrix in python
- Write a program to check if the given word is Isogram & Pair isogram in python
Find more on this topic:
Subscribe to Interview Questions
Recommended books for interview preparation:
Similar Posts
- Configure Logging in gunicorn based application in docker container
- Connect to Cassandra with Python 3.x and get Pandas Dataframe
- Connect to MySQL with Python 3.x and get Pandas Dataframe
- Connect to Postgresql with Python 3.x and get Pandas Dataframe
- Python - Get Google Analytics Data
- Installing PySpark with Jupyter notebook on Ubuntu 18.04 LTS
- Python send GMAIL with attachment
- Send rich text multimedia email in Python
- Blueprints in Flask API Development
- Singleton Design Pattern in Python