dimanche 5 avril 2020

Function that fills in missing numbers to create complete sequence

I'm trying to create a function that will fill in any missing numbers in between two numbers in a list. For example: [13,15,20] would return [13,14,15,16,17,18,19,20]. Note that I am not allowed to use a range function.

Here's my code:

def complete(list1):
    i= 0
    if len(list1) > 1:
        for number in list1:
           if number - list1[i+1] != -1:
               number += 1
               list1.insert(i + 1, number)
           i += 1
        return list1
    else:
        return list1

I got a "list index out of range" error.

Aucun commentaire:

Enregistrer un commentaire