Search code examples
python-3.xnumpypython-3.2

How to see if a given array is between two arrays


I am using python 3.2.0 and numpy. I would like to check if one of the arrays is in between two other specified arrays. I would like it if you suggest a function or few of them together. Any help is appreciated , as it is a school project and I need to submit it quickly.


Solution

  • If you mean how the last item of arr1 is less than all items of input_arr, and the first item of arr2 is greater than all those in input_arr, you can do this, with "biggest" of arr1 and "smallest" of arr2:

    biggest = arr1[len(arr1)-1]
    smallest = arr2[0]
    between=True
    for item in input_arr:
        if not (biggest<item and smallest>item): 
            between=False
            break 
    

    Alternatively, you could change the < and/or the > to <= or >= if you allow equals to (so [1,3,4],[4,6,8],[8,17,18] is True)

    This assumes that the lists are consecutive. If they're not, you'll have to loop through arr1 to find the largest number and arr2 to find the smallest first.

    biggest=0
    for item in arr1: 
        if item>biggest:
             biggest=item
    
    smallest=arr2[0]
    for item in arr2:
        if item<smallest:
           smallest = item
    

    Use this as a skeleton guide, don't just copy and paste. If you don't understand it and therefore can't construct your own version, you probably need to do some sort of online course (eg. Codecademy). In the mean time, copy the 2nd bit first if you need it, then the first.