Find a unique subarray indexes in a unique array (4 types) 🐤🦆🐦🐧🦃

Several simple functions for finding indexes of a subarray in a unique array.

The 4 types differ based on two parameters which are in the parentheses in titles (1/2):

  1. words have the same order – yes/no
  2. words stay close to each other – yes/no

They are posted here for the sake of organization of some code I wrote for myself. I am fairly new to the coding game, and I am sure that in a month I will reconsider some of these (I’ve already, actually), but I still post it with a remark unique (sub)array for the sake of coming back and improving.

As for any kind of (sub)array, not only unique, I am currently working on it and will post it later.

If you have opinions and want to share them, welcome to the comment section below.

1. Words have the same order and stay close (yes/yes)

It is about finding the first index of the whole subarray and making a range(first index, first index + length of subarray).

import numpy as np

def is_subarray_yesOrder_yesClose(array, subarray):
    array = np.array(array)
    subarray = np.array(subarray)
        one = array.tostring().index(subarray.tostring()) // array.itemsize
        two = one + len(subarray)
        res = list(range(one, two))
    except Exception as e:
        res = None
    return res

It looks for the first subarray of the same kind, but it can be looped using slices – after finding each subarray, the next subarray should be searched for in array[first_index_of_last_subarray + len(last_subarray):]

2. Words have the same order and do NOT stay close (yes/no)

It is simply checking if the subarray has a 1st type place in a new array of array elements that are present in the subarray.

def is_subarray_yesOrder_noClose(array, subarray):
    new_arr = [x for x in array if x in subarray]
    return is_subarray_sOrder_sClose(new_arr, subarray)

3. Words have NO order and stay close (no/yes)

It is about checking if indexes array is a descending or an ascending array that is made by range(min(indexes_array), max(indexes_array)

def is_subarray_noOrder_yesClose(array, subarray):
        new_arr = [array.index(x) for x in subarray]
        new_arr_set = list(set(new_arr))\
        icon_asc = list(range(min(new_arr), max(new_arr) +1))
        icon_desc = icon_asc[::-1]
        if new_arr == icon_asc or new_arr == icon_desc:
            return new_arr
    return None


4. Words have NO order and do NOT stay close (no/no)

If the subarray is a set, it will end quickly. If not, it is removing every element from subarray from the array until subarray is empty (success) or subarray is left with unwanted elements (Fail).

def is_subarray_noOrder_noClose(array, subarray):
    sub_set = set(subarray)
    if sub_set == subarray:
        res = sub_set.issubset(array)
        new_array = array.copy()
        res = []
        for subitem in subarray:
            if subitem in new_array:
                subitem_index = new_array.index(subitem)
                del new_array[subitem_index]
                res = None
    return res


Default data:

array = [‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’]

What 4 types of position searching return for the subarrayN arrays:

VariablesValueCorrect1 Order + Close2 Order + not Close3 no Order + Close 4 no Order + not Close
subarray_1[‘1’, ‘2’, ‘3’][1, 2, 3][1, 2, 3][1, 2, 3][1, 2, 3][1, 1, 1]
subarray_2[‘0’, ‘2’, ‘5’][0, 2, 5]None[0, 2, 5]None[0, 1, 3]
subarray_3[‘4’, ‘3’, ‘2’][4, 3, 2]NoneNone[4, 3, 2][4, 3, 2]
subarray_4[‘5’, ‘1’, ‘0’][5, 1, 0]NoneNoneNone[5, 1, 0]
subarray_5[‘0’, ‘0’, ‘3’][0, 0, 3]NoneNoneNoneNone
subarray_6[‘6’, ‘0’, ‘3’][None, 0, 3]NoneNoneNoneNone

I may have made a mistake or two, trying to come up with v1.0, but overall it must resemble what I had to say on the matter.

Again, if you have things to share, please do so below.

Thank you!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.