The Python Oracle

Python - Check if all n numbers are present in a list

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Romantic Lands Beckon

--

Chapters
00:00 Python - Check If All N Numbers Are Present In A List
00:44 Accepted Answer Score 5
00:58 Answer 2 Score 1
01:13 Answer 3 Score 1
01:31 Answer 4 Score 0
01:37 Thank you

--

Full question
https://stackoverflow.com/questions/3069...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #python3x

#avk47



ACCEPTED ANSWER

Score 5


There is no need to use zip with multiple zip function.You can use sorted :

if sorted(l)==list(range(max(l)+1))

example :

>>> sorted(l)==list(range(max(l)+1))
False
>>> l= [0,2,1,7,6,5,4,3] 
>>> sorted(l)==list(range(max(l)+1))
True



ANSWER 2

Score 1


Slow and dirty solition:

def f(myList):
    ll = [i for i in range(max(myList))] 
    diff = set(ll).difference(set(myList))
    if diff: return (False, diff)
    return (True, "sequence without blanks")

#lets test:
t1,t2 = [0,1,7,4,5,2],[3,5,4,2,1,0]
print(map(f,(t1,t2)))



ANSWER 3

Score 1


As always - sets are my favorite here -

Original list

l = [ 1, 2,4 3, 0, 5,6,7]

another compare list

l2 = range(8)

# intersection of two sets is the set of compare list. 
# This solution would work when the size of original list is different than size of the compare list
set(l) & set(l2) == set(l2) 



ANSWER 4

Score 0


You may try test set:

len(set(l))==max(l)+1