We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.

This code doesn't work for test case
5
-7 -7 -7 -7 -6
because his while he takes the input as lis the elements in it are strings not integers so you've to convert those elements into integers and then run the code.

N= input()
L= raw_input().split()
I=[]
assert N>=2 and N<=10
for i in L:
I.append(int(i))
I.sort()
largest=max(I)
count=I.count(largest)
for i in range(count):
I.remove(largest)
print max(I)

you cannot be sure about that. set returns unordered collection of unique elements from the iterable. we cannot be sure about the output of set() as it is unordered and need not be sorted.
sorted() is required to return a sorted list from the unordered set. maybe it worked for you but it cannot be guaranteed to work everytime.
the list() function is redundant as sorted() returns a list itself.

## Find the Runner-Up Score!

You are viewing a single comment's thread. Return to all comments →

Hi, I believe this code is correct only in the cases where the max number apears only once in the input.

Considering the following input [1, 2, 2] it would return 2 whereas it should return 1.

You can try it! :)

Prints '1' for me. The 'set' function returns only unique values (here, [1, 2]), and then after sorting, the second to last element is printed.

Oops, my bad - I missed you were using set prior sorting. Nevermind - it looks great!

5

-7 -7 -7 -7 -6

for this input i think your code wont wholes good

This code doesn't work for test case 5 -7 -7 -7 -7 -6 because his while he takes the input as lis the elements in it are strings not integers so you've to convert those elements into integers and then run the code.

N= input() L= raw_input().split() I=[] assert N>=2 and N<=10 for i in L: I.append(int(i)) I.sort() largest=max(I) count=I.count(largest) for i in range(count): I.remove(largest) print max(I)

His code works. You should try it again dude

Works in python 2 only

It still works. I just checked.

it works.

his code would work for this input , as he is using sets concept in this , the number arent repeated in sets .

How about the case like this: 3 5 5 5 set(nums) will only return one element, [-2] will out of index.

I mean 3 numbers: [5, 5, 5]

GO WITH THIS which does it include any sort or set func ...T.C of O(n) rather than O(logn) for sorting it

if

name== 'main': n = int(input()) arr = map(int, input().split()) list1 = list(set(arr)) max=max(list1[0],list1[1]) secondmax=min(list1[0],list1[1])what [-2] does please explain.

Refers to the second to last element.

what if there is a repetition of the max number -2 wont work there

code uses a set(only distinct numbers)

Set can have only non-repeating values

Set can have only non-repeating values

The list can be accessed from last, considering last element of index -1 and second last element of index -2 and so on...

like we have list=['1','4','6','7'] then [-1]=7 and similarly [-2]=6 that all.

[-2] this is indexing in this case it is used to find number which is second largest number after sorting the list

that's the second item of the list from the right. for example: if you have a list=[1,4,6,9,11] list[-1] returns 11 list[-2] returns 9

-ve index signifies starting from last index

I think you don't need sorted, works just fine without it.

you cannot be sure about that. set returns unordered collection of unique elements from the iterable. we cannot be sure about the output of set() as it is unordered and need not be sorted. sorted() is required to return a sorted list from the unordered set. maybe it worked for you but it cannot be guaranteed to work everytime. the list() function is redundant as sorted() returns a list itself.

What would your code do for Input [2,2,2,2]

Use Try and Except

Index out of bound as [-2] index doesnt exists when you use set

set does not consist of indices

moglobal thats one of the best code ive ever seen.. great job!!!

you don't need the extra list() function as sorted returns a new list itself.

to avoid [1, 1, 1] or just [1]

pretty cool. thanks for sharing. I had the same intent but missed the set/duplicate part.

Why do we need "list" here?

Correct bro,Set is able to store only non-duplicate....

Very nice, learn something new everyday, very pythonic :)

thnq

Not Working for me!

very simple to understand and easy way to code...

I think its correct as set will remove duplicate values

but set would return unique elements i guess

I think set() will delete duplicate values. so [1,2,2] will be reduced to [1,2]

he use set datatype