4 added 278 characters in body
source | link

Method 1

How about simply…?

 consecQ[a_]:= a===Range[a[[1]],a[[-1]]]

This is based on the assumption that the Mathematica can handle the Range size.

Testing

consecQ[{0, 3, 2, 1, 4}]

False

consecQ[{0, 1, 2, 3, 4}]

True


Method2

@A.G. notes that the following can be used if one is concerned about the length of list.

If[Last@a - First@a == Length[a] - 1, a == Range[First@a, Last@a], False]

The idea is to first check whether the length of the list is consistent with the first and last elements.


TestingMethod 3

This may not be efficient, but it does the job.

Assuming that the list consists of integers.

consecQ[consecQ2[a_] := Union[Differences[a]] == {1}

Testing

consecQ2[{0, 31, 2, 13, 4}]

False

consecQ[consecQ2[{0, 13, 2, 31, 4}]
consecQ2[{4, 3, 2, 1, 0}]

True
False
False

How about simply…?

 consecQ[a_]:= a===Range[a[[1]],a[[-1]]]

This is based on the assumption that the Mathematica can handle the Range size.


@A.G. notes that the following can be used if one is concerned about the length of list.

If[Last@a - First@a == Length[a] - 1, a == Range[First@a, Last@a], False]

The idea is to first check whether the length of the list is consistent with the first and last elements.


Testing

consecQ[{0, 3, 2, 1, 4}]

False

consecQ[{0, 1, 2, 3, 4}]

True

Method 1

How about simply…?

 consecQ[a_]:= a===Range[a[[1]],a[[-1]]]

This is based on the assumption that the Mathematica can handle the Range size.

Testing

consecQ[{0, 3, 2, 1, 4}]

False

consecQ[{0, 1, 2, 3, 4}]

True


Method2

@A.G. notes that the following can be used if one is concerned about the length of list.

If[Last@a - First@a == Length[a] - 1, a == Range[First@a, Last@a], False]

The idea is to first check whether the length of the list is consistent with the first and last elements.


Method 3

This may not be efficient, but it does the job.

Assuming that the list consists of integers.

consecQ2[a_] := Union[Differences[a]] == {1}

Testing

consecQ2[{0, 1, 2, 3, 4}]
consecQ2[{0, 3, 2, 1, 4}]
consecQ2[{4, 3, 2, 1, 0}]

True
False
False

3 added 310 characters in body
source | link

How about simply…?

 conseqQ[a_]consecQ[a_]:= a===Range[a[[1]],a[[-1]]]

This is based on the assumption that the Mathematica can handle the Range size.


@A.G. notes that the following can be used if one is concerned about the length of list.

If[Last@a - First@a == Length[a] - 1, a == Range[First@a, Last@a], False]

The idea is to first check whether the length of the list is consistent with the first and last elements.

 

Testing

conseqQ[consecQ[{0, 3, 2, 1, 4}]

False

conseqQ[consecQ[{0, 1, 2, 3, 4}]

True

How about simply…?

 conseqQ[a_]:= a===Range[a[[1]],a[[-1]]]

This is based on the assumption that the Mathematica can handle the Range size.

Testing

conseqQ[{0, 3, 2, 1, 4}]

False

conseqQ[{0, 1, 2, 3, 4}]

True

How about simply…?

 consecQ[a_]:= a===Range[a[[1]],a[[-1]]]

This is based on the assumption that the Mathematica can handle the Range size.


@A.G. notes that the following can be used if one is concerned about the length of list.

If[Last@a - First@a == Length[a] - 1, a == Range[First@a, Last@a], False]

The idea is to first check whether the length of the list is consistent with the first and last elements.

 

Testing

consecQ[{0, 3, 2, 1, 4}]

False

consecQ[{0, 1, 2, 3, 4}]

True

2 added 91 characters in body
source | link

How about simply…?

 conseqQ[a_]:= a===Range[a[[1]],a[[-1]]]

where aThis is based on the listassumption that the Mathematica can handle the Range size.

Testing

conseqQ[{0, 3, 2, 1, 4}]

False

conseqQ[{0, 1, 2, 3, 4}]

True

How about simply…?

  a===Range[a[[1]],a[[-1]]]

where a is the list.

How about simply…?

 conseqQ[a_]:= a===Range[a[[1]],a[[-1]]]

This is based on the assumption that the Mathematica can handle the Range size.

Testing

conseqQ[{0, 3, 2, 1, 4}]

False

conseqQ[{0, 1, 2, 3, 4}]

True

1
source | link