Skip to content Skip to sidebar Skip to footer

How To Check If Consecutive Elements Of Array Are Evenly Spaced?

For example: [1,2,3,4,5,6] -> True [1,2,3,5,6] -> False I guess i could do something like: if len(set([arr[x] - arr[x - 1] for x in range(1, len(arr))])) > 1: print('

Solution 1:

Here's what i ended up doing...

defsame_dist_elems(arr):
    diff = arr[1] - arr[0]
    for x inrange(1, len(arr) - 1):
        if arr[x + 1] - arr[x] != diff:
            returnFalsereturnTrue

Credits to Barmar but he only put in a comment not an answer

Solution 2:

try this, for a list to be consecutive

  • the difference between the last item and first item plus 1 must be equal to the list length
  • length of list vs length of set must match to rule out repeated entries
  • The list need to be sorted for this to works.

code

defcheck_consecutive(input_list):
    input_list = sorted(input_list)
    return (input_list[-1] - input_list[0]) + 1 == len(input_list) and (len(input_list) == len(set(input_list))

check_consecutive([1,2,3,4,5,6]) # True
check_consecutive([1,2,3,5,6])   # False

like the comment mention, code above only works for spacing of 1 unit and we could omit the sort list portion if the input_list already sorted

to make it generic for any space unit, we could try code below. It first create the ideal evenly space list based on input_list space unit then diff against input_list. Code below assume input_list already sorted

defcheck_evenly_space(input_list):
    iflen(input_list)==1:
        returnFalse
    space = input_list[1] - input_list[0]
    if space==0:
        returnFalsereturnlist(range(input_list[0], input_list[-1] + space, space)) == input_list

check_evenly_space([1,2,3,4,5,6]) # True
check_evenly_space([1,2,3,5,6])   # False
check_evenly_space([2,4,6,8,10])  # True
check_evenly_space([2,4,6,7,10])  # False

Solution 3:

You can just check if the difference are all the same (using numpy.diff):

import numpy as np
a = np.array([1,2,3,4,5,6])
b = np.array([1,2,3,5,6])

all(np.diff(a)==np.diff(a)[0])  # --> Trueall(np.diff(b)==np.diff(b)[0])  # --> False

Solution 4:

A simple solution is to compare the list with a range. To save converting the range to a list, we can use zip and all. This isn't the most efficient solution possible, but it's still O(n) time and O(1) auxiliary space.

deflist_is_evenly_spaced(lst):
    # special case: a list with 0, 1 or 2 elements is evenly spacediflen(lst) <= 2:
        returnTrue

    first = lst[0]
    gap = lst[1] - first
    # special case: a constant list is evenly spacedif gap == 0:
        returnall(x == first for x in lst)

    # general case: an evenly spaced list equals a range
    r = range(first, first + gap*len(lst), gap)
    returnall(x == y for x,y inzip(lst, r))

Explanation of the special cases, which have to be handled separately:

  • A list of 0, 1 or 2 elements is always evenly spaced. To be unevenly spaced there have to be two different gaps, implying at least three elements.
  • A constant list is always evenly spaced, because every gap is 0. However, it's not valid to construct a range with a gap of 0.

Solution 5:

You can do this easily with numpy

import numpy as np
np.diff(a) == len(a)-1True

If speed is a concern make sure

type(a)
<type'numpy.ndarray'>

with np.array(a)

Post a Comment for "How To Check If Consecutive Elements Of Array Are Evenly Spaced?"