Find The Runner - Up Score! in Python - Hacker Rank Solution
Hey, What's up Coders. Coding is fun for many people, but sometimes it becomes a burden to coders if they get a simple error in their code. To get rid of such minor errors one should practice coding on different platform like HackerRank, CodeChef and Codeforces etc.
So, today we are going to solve one of the hackerrank problems of python. After reading this post you will find the solution very simple, and you yourself will be able to solve the problem very easily.
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.
Constrains
- 2 ≤ n ≤ 10
- -100 ≤ A[i] ≤ 100
Output Format
Print the runner-up score.
Sample Input 0
5 2 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 score is 5. Hence, we print 5 as the runner-up score.
Find The Runner -Up Score In Python - Solution
Python 3
if __name__ == '__main__': n = int(input()) arr = map(int, input().split()) print(sorted(list(set(arr)))[-2])
Find The Runner - Up Score - Python |
Solution Result |
Explanation
Here in the source code we have used if __name__ == '__main__. Whenever python interpreter reads and source file, it sets a few special variables like __name__. It basically allows the code in module to importable by other modules without executing the code.
The second line of our code is quite simple. Most of you are aware of it, it is use to take the input from the user.
Split is use to covert a string into array of sub strings and return the new array.
Disclaimer: The problem used in this tutorial is generated by Hacker Rank but the solution is provided by Sloth Coders.
Conclusion
So, today we have solved the runner- up hackerrank problem. You can create your own simple program like this to boost your coding skills. I hope all of you have understand the solution of hackerrank problem (Find The Runner - Up in Python), but if you have any doubt regarding the problem feel free to contact in the Comment section.
Don't forget to share it with your coder friends, because knowledge should be share. If you want the upcoming solutions of HackerRank, CodeChef and Codeforces, Don't forget to Subscribe our Blog.
Happy Coding!!
A Sloth Who loves to Code.