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! :)
nums = [1, 2, 2]
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!
-7 -7 -7 -7 -6
for this input i think your code wont wholes good
This code doesn't work for test case
-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.
assert N>=2 and N<=10
for i in L:
for i in range(count):
His code works. You should try it again dude
Works in python 2 only
It still works. I just checked.
print(input()==0 or sorted(list(set(map(int,input().split()))))[-2])
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:
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))
for i in range(2,len(list1)):
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
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
then [-1]=7 and similarly [-2]=6
[-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.
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.
dn,arr=(input() for _ in range(2))
nums = map(int, arr.split())
to avoid [1, 1, 1] or just 
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 :)
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]