1

So I'm writing this code to see if an element in a list could be removed to make the list increasing. in the code below why do I get this error?

def almostIncreasingSequence(sequence):
    sorted_sequence = sorted(sequence)
    counter = 0
    for i in len(sequence):
        if sorted_sequence[i] != sequence[i]:
            counter += 1
    if counter > 1:
        return True
    else:
        return False
1
  • 4
    Look again at for i in len(sequence). len(sequence) is a number. You probably mean range(len(sequence)). Commented Feb 23, 2018 at 2:04

1 Answer 1

1

len(sequence) is a number here, and you can't iterate a number:

for i in len(sequence):
    ...

You probably wanted

for a,b in zip(sequence, sorted_sequence):
    ...

You may as well return the count rather than a boolean, or return from within the for-loop, since it's not necessary to iterate the entire sequence to check whether this count is > 1.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.