Skip to content

Instantly share code, notes, and snippets.

@martync
Created March 8, 2013 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martync/5116875 to your computer and use it in GitHub Desktop.
Save martync/5116875 to your computer and use it in GitHub Desktop.
def get_max_consecutive_number(datas):
"""
Retourne le nombre maximum de chiffres consécutifs
dans une listes. (Y'a t'il un built-in pour ça ? Itertool ?)
>> suit = [2003, 2008, 2007, 2001, 2010]
>> print get_max_consecutive_number(suit)
>>>> 2
>> suit = [2003, 2008, 2007, 2001, 2002, 2004]
>> print get_max_consecutive_number(suit)
>>>> 4
"""
prev_num = None
count = 0
counts = []
for d in sorted(list(set(datas))):
if prev_num is None:
count += 1
else:
nexts = (prev_num+1, prev_num-1)
if d in nexts:
count += 1
else:
count = 1
prev_num = d
counts.append(count)
return sorted(counts, reverse=True)[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment