5

This is my homework.

The problem is to find a way to check whether the items in a list are consecutive or not.

The following is code I've written:

def consecutive(var):
for x in range(2, len(var)):
    forward = var[x] - var[x-1]
    backward = var[x-1] - var[x-2]
if forward == backward:
    return True
else:
    return False

var = []
print 'Enter your number:'
while True:
    num = raw_input()
    if num == '':
        break
    var += [int(num)]

print consecutive(var)

If I input numbers like 1, 2, 3, 4, 5 then I will get True

If I input numbers like 2, 6, 3, 9, 7, 1, 4 then I'll get False

Here, I succeeded returning True or False values respectively.

But there're two questions that make me upset because if I use my code to solve the questions, I don't get the value that I want (it gives me an error)

First question: Is an empty list considered a consecutive list or not?

Second: Is a list that involves a single value considered a consecutive list or not?

Would you like to help me?

6
  • 3
    These are edge cases that should be defined in your homework. In my opinion however, an empty list or a list with one element should indeed be seen as having consecutive order, as by any construction they are ''sorted''. Commented Apr 16, 2016 at 18:03
  • 1
    I would say that a for a list to be consecutive, you must have at least two elements to make a comparison. Commented Apr 16, 2016 at 18:14
  • @JohnTitusJungao: while that's not a crazy perspective, it goes against convention. We call [] and [1] sorted because [] == sorted([]) and [1] == sorted([1]) even though there aren't two elements, because there are no elements which are unsorted. A similar logic should apply to consecutive elements, IMHO. Commented Apr 16, 2016 at 18:17
  • @DSM Although my initial thinking was to agree with you, now I'm not sure. This is obviously just semantics but if the question specified a list with consecutive elements, can you really attribute that back to sorting and sets? Or does that implicitly restrict your list to having two elements? (asking because I'm not sure :P) Commented Apr 16, 2016 at 18:19
  • @DonkeyKong: this really is just a convention, and the standard convention in math is to allow vacuous truth in definitions. Commented Apr 16, 2016 at 18:21

1 Answer 1

2

By mathematical convention as discussed in the comments, it would be standard to consider both an empty list and a list with a single element as consecutive, should it not be specified in your homework more precisely.

By the logic of vacuous truth, were a list to not have consecutive order it would require enough elements (2) to break this condition of ordering. To handle this in your existing code, you could simply perform a check before your main check to ensure the base cases of an empty list and a list with one element return True.

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.