Find the first non-consecutive number in a list
Question:
The challenge is to find the first non-consecutive number in python. I have managed to do it with a library called more_itertools, but the challenge requires that no library be involved. How can I solve it?
This is my code:
from more_itertools import consecutive_groups
def first_non_consecutive(l):
g = []
for grp in consecutive_groups(l):
g.append(list(grp))
if len(g) > 1:
return g[1][0]
Answers:
Use enumerate
with start
parameter to your advantage here.
def first_non_consecutive(lst):
for i, j in enumerate(lst, lst[0]):
if i!=j:
return j
first_consecutive([1,2,3,4,6,7,8])
# 6
The challenge is to find the first non-consecutive number in python. I have managed to do it with a library called more_itertools, but the challenge requires that no library be involved. How can I solve it?
This is my code:
from more_itertools import consecutive_groups
def first_non_consecutive(l):
g = []
for grp in consecutive_groups(l):
g.append(list(grp))
if len(g) > 1:
return g[1][0]
Use enumerate
with start
parameter to your advantage here.
def first_non_consecutive(lst):
for i, j in enumerate(lst, lst[0]):
if i!=j:
return j
first_consecutive([1,2,3,4,6,7,8])
# 6