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.

Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. If one iterable is shorter than another it is assumed to be extended with None items. If function is None, the identity function is assumed; if there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). The iterable arguments may be a sequence or any iterable object; the result is always a list.

For those that didn't understand, first input isn't actually necessary because validation of the length of the numbers entered in 2nd line isn't actually being asked

raw_input returns type string, hence it will never be zero(0) and won't print the first input under any circumstance.

Next read the input,split away the spaces and map it to integer type

My understanding is that sorted() returns a new sorted list, and also works for things besides lists (map objects, tuples, etc.). list.sort() sorts a list in place.

^ This. The set function doesn't create a list, and sets don't allow for the sort attribute. Using sorted() converts the data into a list, then sorts it.

Its always good to see List Comprehensions. But don't you think this will increase overall runtime ? I mean you are creating a new list and then finding the max. of it!

I really think I understand this answer...
I was thinking of max but this one makes more sense to me.

I have mostly worked in python 2.7 and am trying to refresh my knowledge of it while updating to 3.0 is there documentation on the sorted() and hot to use it that is easy to understand?

set defines what ,. sort used to assing in ascending order ,list fn is to print the mapped object. for this set ,how it is assing for this particular output

n = int(raw_input())
arr = map(int, raw_input().split())
dup =[]
for i in arr:
if i not in dup:
dup.append(i)
dup = sorted(dup)
print dup[len(dup) - 2]

## Find the Runner-Up Score!

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

How about this shorter one...

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!

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]

nlog(n) though.

no,BFPRT,O(n)

try this @R)Try&i

I was trying to convert set to list like you did. But I noticd you could just use sorted() for the set ! Weird, but it works just fine ...

input()

myList = sorted(set(map(int, input().strip().split()))) print(myList[len(myList)-2])

Cool. Not weird, since sorted creates a copy during the sort, so even an immutable thing like set is granted operation.

I did a similar thing:

edit (With one less call, don't reverse sort):

print(sorted(set(l))[-2]) this works

Just myList[-2] - is shorter and still correct.

hi can you tell me please where the value of n is being used and how ? thanks any ways

n is nowhere to be used

Can you please tell what map() is doing here ?

map(function, iterable, ...)

Apply function to every item of iterable and return a list of the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. If one iterable is shorter than another it is assumed to be extended with None items. If function is None, the identity function is assumed; if there are multiple arguments, map() returns a list consisting of tuples containing the corresponding items from all iterables (a kind of transpose operation). The iterable arguments may be a sequence or any iterable object; the result is always a list.

https://docs.python.org/2/library/functions.html#map

List isn't necessary as sorted takes any object iirc

For those that didn't understand, first input isn't actually necessary because validation of the length of the numbers entered in 2nd line isn't actually being asked

raw_input returns type string, hence it will never be zero(0) and won't print the first input under any circumstance.

Next read the input,split away the spaces and map it to integer type

Make it's set

Sort the set

Print the 2nd last element in the sorted set

Thank you so much.. superGogeta

Thanks & Regards

Camel casing my name lol, Your welcome :)

Nice. My code is same as yours except you handled unnecessary

`n`

input in one line :)I was able to do it in one line. But I will not tell you how! Try to figure it out yourself HA HA

Can anyone plz explain why sorted() works and not the function sort() ?

My understanding is that sorted() returns a new sorted list, and also works for things besides lists (map objects, tuples, etc.). list.sort() sorts a list in place.

You can use

`sort()`

it is efficient than`sorted()`

as it does in-place sorting. They used sorted, so that they write it in lesser number of characters.`sort()`

returns`None`

whereas`sorted()`

returns the newly created list.Using

`sort()`

:'set' object has no attribute 'sort'

^ This. The set function doesn't create a list, and sets don't allow for the sort attribute. Using sorted() converts the data into a list, then sorts it.

won't work if 6,6 element are the largest in list

thanks it worked for me.

even without using list

can you explain [-2]

seond from the end

Can you please explain it. Code worked like magic. I am new in python. So I need to get it clear in my head ASAP!

Good one moglobal!

if enter n=6 and give 10 elements then will get wrong. ur code is not using n

hy can u tell me about what set is exactly works in set(nums)?

It returns list with unique values.

Nice, I didn't think of the set function. Here is my aproach:

This is very interesing solution

Its always good to see List Comprehensions. But don't you think this will increase overall runtime ? I mean you are creating a new list and then finding the max. of it!

Nice! Here's an even shorter version using your approach:

dude where are we using n here??

Actually 'n' is used here to iterate 'arr' object

and

are same

Hope this helps!

This is a great solution.

This fails if u had more than 2 numbers of maximum value in the given list

It is wonderful broo

it can also be written like this, n = int(raw_input()) nums = map(int, raw_input().split())

print sorted(list(set(nums)),reverse=True)[1]

you dont need to convert it into list! sorted(set(nums))[-2] works fine even with negative numbers

I really think I understand this answer... I was thinking of max but this one makes more sense to me.

I have mostly worked in python 2.7 and am trying to refresh my knowledge of it while updating to 3.0 is there documentation on the sorted() and hot to use it that is easy to understand?

Python 3 version:

what if i want to print the max no as well as second max no ?

you can use:

just try it

what does the function set()do?

make set :)

I get an a error list object is not callable , however it works if I remove the list keep sorted(set(nums))[-2]

set defines what ,. sort used to assing in ascending order ,list fn is to print the mapped object. for this set ,how it is assing for this particular output

wow that was smooth... why i didnt think that way... lol i always choose long path

Good answer. It can be done without casting to a list:

Although I don't see why you had to apply

`list`

to the`set`

.May be a noob version of your code

It worked, Thanks

that's cool dude

This worked neatly for me. The only thing I'll add are paranthases wrapped around the print statement because I was getting errors without them:

n = int(input()) arr = map(int, input().split()) print (sorted(list(set(arr)))[-2])

Really awesome logic.