'function which tells whether an array is subsequence of another but 1 of the loop isnt working

def is_subsequence_array(array, sequence):
   for num in sequence:
      if array.count(num) != 1 :
         output = False
   for i  in range(len(sequence) - 1):
     if array.index(sequence[i + 1]) < array.index(sequence[i]):
            output = False
     else:
           output = True
   return(output)
print(is_subsequence_array([5, 6, 3], [2, 1]))


Solution 1:[1]

I think this should work:

def is_subsequence_array(array, sequence):
    for index, num in enumerate(array):
        if num == sequence[0] and len(array) - index >= len(sequence):
            output = True
            for i in range(len(sequence) - 1):
                if array[index + i] != sequence[i]:
                    output = False
                    break
            if output:
                return True
    return False

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Dmytro Kolupaiev