Find the Runner-Up Score! in Python - Hacker Rank Solution
Problems:
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 :
5 2 3 6 6 5
Sample Output :
5
Explanation :
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.
Solution :
1 2 3 4 5 6 7 | # Find the Runner-Up Score! in Python - Hacker Rank Solution if __name__ == '__main__': n = int(raw_input()) arr = map(int, raw_input().split()) # Find the Runner-Up Score! in Python - Hacker Rank Solution START print(sorted(list(set(arr)))[-2]) # Find the Runner-Up Score! in Python - Hacker Rank Solution END |
Disclaimer :-
the above hole problem statement is given by hackerrank.com but the solution is generated by the codeworld19 authority if any of the query regarding this post or website fill the following contact form thank you.
if __name__ == '__main__':
n = int(input())
arr=[]
arr.append(input().split())
c=arr[0]
d=[]
for i in range (len(c)):
d.append(int(c[i]))
x=sorted(d,reverse=True)
for i in range (len(x)):
if (x[0]==x[i+1]):
continue
else:
print(x[i+1])
break
hi, this code works fine until we give combination of two digit and three digit number in the scores.
Please help to write sorting command to sort based on length of int and in descending order.
arr=sorted(arr)
l=[]
for i in range(len(arr)):
if arr[i]<max(arr):
l.append(arr[i])
print(max(l))
i love you
you are that guy!!
n = int(input())
arr = (int, input().split())
arrlist = []
for t in arr:
arrlist.append(t)
num=arrlist[1]
test_list = [int(i) for i in num]
test_sorted= (sorted(test_list, reverse=True))
List = []
for i in test_sorted:
if i != max(test_sorted):
List.append(i)
print(max(List))
def find_highscore(data, n):
high_score = 0
data = sorted(data,reverse=True)
for i in range(len(data)):
if data[i] > data[i+1]:
high_score = data[i + 1]
break
return high_score
if __name__ == '__main__':
n = int(input())
arr = map(int, input().split(" "))
arr = list(arr)
print(find_highscore(arr,n))
score_sheet =[]
st_numbers = int(input("the number of students: "))
for i in range(st_numbers):
data = int(input())
score_sheet.append(data)
score_sheet.sort(reverse=True)
score_sheet[1]
Easiest solution I found for me as a beginner. Thanks!
simple solution , Thanks!
bhi code run nhi kar raha ha
if __name__ == '__main__':
n = int(raw_input())
arr = map(int, raw_input().split())
arr = list(dict.fromkeys(arr))
arr.sort()
arr.reverse()
print(arr[1])
it won't return correct answer if input has same number multile times
# use this command for repeated values
n=int(input())
arr=[]
for i in range(n):
j=int(input())
arr.append(j)
while(True):
c=max(arr)
arr.remove(c)
d=max(arr)
if d==c:
continue
else:
break
print(d)
n = int(input())
arr = input().split()
s = sorted(arr)
i = n
while i != 0:
if s[i-1] != s[n-1]:
print(s[i-1])
break
i -= 1
if __name__ == '__main__':
n = int(input())
arr = map(int, input().split())
# Find the Runner-Up Score! in Python - Hacker Rank Solution START
print(sorted(list(set(arr)))[-2])
# Find the Runner-Up Score! in Python - Hacker Rank Solution END
if __name__ == '__main__':
n = int(input())
#arr = map(int, input().split())
a=input().split()
arr=[int(x) for x in a]
#print(arr)
maxs=0
smax=0
k=len(arr)
for i in range(0,k):
if arr[i]>maxs:
maxs=arr[i]
#print(maxs)
for j in range(0,k):
if arr[j]>smax:
if arr[j]<maxs:
smax=arr[j]
print(smax)
but why? you slow down compiler and buffer goes slow.., easiest simply 5-6 lines to code....and you find this it's good
#easiest to understand
n = int(input('Size of the array - '))
score = []
for x in range(0,n):
k = int(input('Scores - '))
score.append(k)
score.sort()
scoreset=set(score)
sc = list(scoreset)
print('Runner up element is {}'.format(sc[-2]))
n = int(input())
com =[]
lis =(input().split())
lis.sort()
for i in lis:
if i not in com:
com.append(i)
com.sort()
print(f'{com[-2]}')
# Online Python compiler (interpreter) to run Python online.
# Write Python 3 code in this online editor and run it.
a=[2,6,3,6,5]
c=[]
for i in range(len(a)):
for j in range(len(a)-1):
if(a[j]>a[j+1]):
temp=a[j]
a[j]=a[j+1]
a[j+1]=temp
print(a)
for i in range(len(a)):
for j in a:
if j in c:
break
else:
c.append(j)
print(c)
b=len(c)-2
print(c[b])
a=[2,3,6,6,5]
c=[]
for i in range(len(a)):
for j in range(len(a)-1):
if(a[j]>a[j+1]):
temp=a[j]
a[j]=a[j+1]
a[j+1]=temp
for j in a:
if j not in c:
c.append(j)
b=len(c)-2
print(c[b])
This comment has been removed by the author.
I am new to programming can somebody please explain what is the problem with this code
f __name__ == '__main__':
n = int(input())
arr = map(int, input().split())
y =[]
for val in arr:
if val>0:
y.append(val)
print (max(y)-1)
n = int(input())
arr = list(map(int, input().split()))
arr.sort()
for i in reversed(range(len(arr))):
if arr[i-1]<arr[i]:
print(arr[i-1])
break
another one
if __name__ == '__main__':
n = int(raw_input())
arr = map(int, raw_input().split())
arr.sort(reverse=True)
for i in range(n):
if arr[i]>arr[i+1]:
print(arr[i+1])
break
cheek our coad
bruh
n = int(input())
arr = map(int, input().split())
new_list = sorted(list(arr))
first = max(new_list)
new_list2 = []
for index in range(len(new_list)):
if new_list[index] != first:
new_list2.append(new_list[index])
print(max(new_list2))
This comment has been removed by the author.
n = int(input())
b = map(int, input().split())
b=list(b)
d=[]
e=[]
(b.sort(reverse=True))
for i in range(0,n):
if b[i]<b[0]:
d.append(b[i])
break
else :
pass
print(d[0])
if __name__ == '__main__':
n = int(input())
arr = list(set(map(int, input().split())))
print(sorted(arr)[-2])
if __name__ == '__main__':
n = int(input())
arr = set(map(int, input().split()))
arr.remove(max(arr))
print(max(arr))
If we use the sort method to sort the values and there by using slicing operator, it works but only for the given output for the present question on hackerrank.
Thanks for providing this information
Best Python Online Training
Learn Python Online Course
if __name__ == '__main__':
n = int(input())
arr = map(int, input().split())
ar=list(arr)
ar.sort()
max=ar[n-1]
for i in range(0,n):
if ar[i]==max:
print(ar[i-1])
break
if __name__ == '__main__':
def runnerup(value,k):
if value == arr[n-1]:
value =arr[k-1]
runnerup(value,k-1)
else:
print(value)
n = int(input())
arr = list(map(int, input().split()))
arr.sort()
runnerup(arr[n-2],n-2)
if __name__ == '__main__':
n = int(input())
arr = list(map(int, input().split()))
v=max(arr)
arr.remove(v)
u=max(arr)
if (u==v):
arr.remove(u)
i=(max(arr))
if (i==u==v):
arr.remove(i)
print(max(arr))
h=max(arr)
if(u==v==h==i):
s=sorted(arr)
print(x[0])
else:
print(max(arr))
else:
print(max(arr))
if __name__ == '__main__':
n = int(input())
arr = list(map(int, input().split()))
highest = max(arr)
while highest in arr:
arr.remove(highest)
print(max(arr))
if __name__ == '__main__':
n = int(raw_input())
arr = map(int, raw_input().split())
a = max(arr)
arr.sort()
for i in range(0,n):
if arr[i] <a:
b = arr[i]
print(b)
n = int(input())
arr = list(map(int, input().split()))
arr.sort(reverse = True)
m = arr.count(arr[0])
print(arr[m])
if __name__ == '__main__':
n = int(input())
arr = map(int, input().split())
b = set(arr)
b.remove(max(b))
print(max(b))
x = list(arr)
#mylist = list(dict.fromkeys(mylist))
mylist = list(dict.fromkeys(x))
mylist.sort()
print(mylist[-2])
if __name__ == '__main__':
n = int(raw_input())
arr = map(int, raw_input().split())
arr = list(dict.fromkeys(arr))
arr.sort()
arr.reverse()
print(arr[1])
if __name__ == '__main__':
n = int(input())
arr = map(int, input().split())
arr=sorted(set(arr))
print(arr[-2])
if __name__ == '__main__':
n = int(input())
arr = map(int, input().split(" "))
arr = list(arr)
b=set(arr)
c=(list(b))
c.sort()
print(c[-2])
if __name__ == '__main__':
n = int(input())
arr = map(int, input().split())
ar=list(arr)
ar.sort()
arn=[]
maxi=max(ar)
for i in range(n):
# temp=ar[i]
if maxi>ar[i]:
arn.append(ar[i])
print(max(arn))
if __name__ == '__main__':
n = int(input())
arr = list(map(int, input().split()))
res = []
for i in arr:
if i not in res:
res.append(i)
if n==len(arr) and n>=2:
arr=sorted(res,reverse=True)
for i in range(n):
if arr[i]==arr[0]:
continue
else:
print(arr[i])
break
elif 1 == (n - len(arr)):
print("one item is missing")
elif 1 == n:
print("Items should be greater than or equal to 2")
else:
print("{} items are missing".format((n-len(arr))))
if __name__ == '__main__':
n = int(input())
arr = map(int, input().split())
arr_list = list(arr)
arr_set = set(arr_list)
arr_list1 = list(arr_set)
arr_sorted = sorted(arr_list1,reverse=True)
print(arr_sorted[1])
This comment has been removed by the author.
This comment has been removed by the author.
n = int(input())
arr = map(int, input().split())
arr = list(arr)
l=[]
x=max(arr)
for i in range(len(arr)):
if arr[i]<x:
l.append(arr[i])
l.sort()
runner_up=l[-1]
print(runner_up)
if __name__ == '__main__':
n = int(input())
arr = map(int, input().split())
arr2 = list(arr)
a = max(arr2)
for i in arr2:
if(i==a):
c = arr2.count(i)
for x in range(c):
arr2.remove(i)
print(max(arr2))