0

Firstly, I start with sorted trial data:

[[ 2,  4,  9, 10, 11],
 [ 2,  6,  7,  8, 14],
 [ 3,  6,  8,  8, 11],
 [ 4,  6, 10, 11, 13],
 [ 2,  3,  3,  5,  6],
 [ 3,  5, 12, 12, 13],
 [ 2,  2,  3,  9, 11],
 [ 2,  5, 11, 11, 13],
 [ 3,  5,  7,  9, 10],
 [ 2,  6,  7,  8, 14]]

Then my goal is return a True or False in the place of each array within and then print out the number of True (contiguous arrays)

So far, I have done this:

def isStraight(arr, n):

    for i in range(1,n): 
        if (arr[i] - arr[i-1] > 1) : 
            return 0
    return 1

but it returns an error saying

The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

So I am not sure how to implement another for loop to iterate through the internal arrays. Any help would be appreciated.

ngShravil.py
  • 4,742
  • 3
  • 18
  • 30
  • What is the expected output? – Chris Sep 25 '19 at 04:10
  • How do you define whether the array is contiguous or not? – ngShravil.py Sep 25 '19 at 04:15
  • Possible duplicate of [Efficiently check if numpy ndarray values are strictly increasing](https://stackoverflow.com/questions/30734258/efficiently-check-if-numpy-ndarray-values-are-strictly-increasing) – Chris Sep 25 '19 at 04:30
  • @Chris the expected output would be like this [ True True False True False] etc if the arrays go up in sequential order. For example 1 2 3 4 5 would be True, but 1 5 6 3 7 would be False – nikoslayer Sep 25 '19 at 23:24

2 Answers2

0

I am assuming that the array is contiguous if any consecutive elements are with a difference of more than 1, with this try the below code:

a =[[ 2,  4,  9, 10, 11],
 [ 2,  6,  7,  8, 14],
 [ 3,  6,  8,  8, 11],
 [ 4,  6, 10, 11, 13],
 [ 2,  3,  3,  5,  6],
 [ 3,  5, 12, 12, 13],
 [ 2,  2,  3,  9, 11],
 [ 2,  5, 11, 11, 13],
 [ 3,  5,  7,  9, 10],
 [ 2,  6,  7,  8, 14]]

def isStraight(arr, n):
    for i in range(1,n): 
        if (arr[i] - arr[i-1] > 1) : 
            return 0
    return 1

values = []
for j in a:
    if(isStraight(j, len(j))==1):
        values.append(True)
    else:
        values.append(False)

print(values)
ngShravil.py
  • 4,742
  • 3
  • 18
  • 30
0

The output of this code contains a list of true and false. True, indicates that its corresponding array is continuous, and false indicates that its corresponding array is non-continuous. Finally, the number of continuous arrays is shown.

def contiguous_arrays(array):
    value=[]
    for i in range(len(array)):
        value.append(array[i]==list(range(min(array[i]),max(array[i])+1)))
    print(value)
    print('Number of continuous arrays:',value.count(True))

array=[[ 2,  4,  9, 10, 11],
       [ 2,  6,  7,  8, 14],
       [ 3,  6,  8,  8, 11],
       [ 4,  6, 10, 11, 13],
       [ 2,  3,  3,  5,  6],
       [ 3,  5, 12, 12, 13],
       [ 2,  2,  3,  9, 11],
       [ 2,  5, 11, 11, 13],
       [ 3,  5,  7,  9, 10],
       [ 2,  6,  7,  8, 14]]
contiguous_arrays(array)
# [False, False, False, False, False, False, False, False, False, False]             Number of continuous arrays: 0