Not sure if you had a
and b
swapped, and also the possible implementation did not seems to handle the case for the last element of b
(first2!=last2
) ?.
function includes(a::AbstractVector,b::AbstractVector)
first2 = firstindex(b)
last2 = lastindex(b)
first1 = firstindex(a)
last1 = lastindex(a)
while first2 != last2
if (first1 == last1) || (b[first2] < a[first1])
return false
else !(a[first1] < b[first2])
first2 += 1
end
first1 += 1
end
first2 == last2 && return (b[first2] in a)
return true
end
v1 = ['a', 'b', 'c', 'f', 'h', 'x']
v2 = ['a', 'b', 'c']
v3 = ['a', 'c']
v4 = ['a', 'a', 'b']
v5 = ['g']
v6 = ['a', 'c', 'g']
v7 = ['A', 'B', 'C']
"""
$(v1)
includes:
a b c : $(includes(v1, v2))
a c : $(includes(v1, v3))
a a b : $(includes(v1, v4))
g : $(includes(v1, v5))
a c g : $(includes(v1, v6))
\$v1 : $(includes(v1, v1))
""" |> print
Produce this, which is closer to the expected behavior.
['a', 'b', 'c', 'f', 'h', 'x']
includes:
a b c : true
a c : true
a a b : false
g : false
a c g : false
$v1 : true
Edit:
Should be this instead.
first2 == last2 && return (b[first2] in a[first1:end])
['a', 'b', 'c', 'f', 'h', 'x']
includes:
a b c : true
a c : true
a a b : false
g : false
a c a : false
a b c g : false
a b c f h x : true