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('not equally spaced')
I was wondering if there were a better way?
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('not equally spaced')
I was wondering if there were a better way?
Here's what i ended up doing...
def same_dist_elems(arr):
diff = arr[1] - arr[0]
for x in range(1, len(arr) - 1):
if arr[x + 1] - arr[x] != diff:
return False
return True
Credits to Barmar but he only put in a comment not an answer
diff = arr[1] - arr[0] will raise an IndexError.try this, for a list to be consecutive
code
def check_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
def check_evenly_space(input_list):
if len(input_list)==1:
return False
space = input_list[1] - input_list[0]
if space==0:
return False
return list(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
[1, 2, 5] would be accepted as the code can't tell the difference between it and [1, 3, 5]. And sorting the list is a bit problematic, as you need to ensure the items are in order anyway. [3, 5, 1] is not valid![3] or [5, 5, 9].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.
def list_is_evenly_spaced(lst):
# special case: a list with 0, 1 or 2 elements is evenly spaced
if len(lst) <= 2:
return True
first = lst[0]
gap = lst[1] - first
# special case: a constant list is evenly spaced
if gap == 0:
return all(x == first for x in lst)
# general case: an evenly spaced list equals a range
r = range(first, first + gap*len(lst), gap)
return all(x == y for x,y in zip(lst, r))
Explanation of the special cases, which have to be handled separately:
You can try this
def ContiguousElem(arr):
ss = set()
for i in arr: ss.add(i)
count = 1
curr = arr[0] - 1
while curr in ss:
count += 1
curr -= 1
curr = arr[0] + 1
while curr in ss:
count += 1
curr += 1
return (count == len(ss))
arr1 = [1,2,3,4,5,6]
arr2 = [1,2,3,5,6]
if ContiguousElem(arr1): print("Yes")
else: print("No")
if ContiguousElem(arr2): print("Yes")
else: print("No")
o/p : Yes
No