-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuestion5-array2 intersection array1
35 lines (26 loc) · 1.23 KB
/
Question5-array2 intersection array1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
Validate Sub sequence
Given two non-empty arrays of integers, write a function that determines whether the second array is a subsequence of the first one.
A sub sequence of an array is a set of numbers that aren't necessarily adjacent in the array but that are in the same order as
they appear in the array but that are in the same order as they appear in the array. For instance, the numbers [1, 3, 4] form a subsequence of the array [1, 2, 3, 4],
and so do the numbers [2, 4]. Not that a single number is an array and the array itself are both valid subsequences of the array.
array = [ 5, 1, 22, 5, 6, -1, 8, 10]
subsequence = [1, 6, -1, 10]
true
def bool2str(m_bool):
if m_bool:
return 'true'
else:
return 'false'
def isValidSubsequence(array, sequence):
is_valid=False
new_list=[]
for i in range(0,len(sequence)):
for j in range(0,len(array)):
if sequence[i]==array[j]:
new_list.append(sequence[i])
if new_list[i]==sequence[i]:
return bool2str(is_valid)
if __name__ == "__main__":
array = [ 5, 1, 22, 5, 6, -1, 8, 10]
subsequence = [1, 6, -1, 10]
print(isValidSubsequence(array, subsequence))