2
$\begingroup$

Slightly change the previous question, ordered set inclusion,

Now, consider $A=\{2,3\}$ and $B=\{2,3,4,5\}, C=\{5,2,4,3\}$, Now I want to make element $\{2,3\}$ is in $B$ as well as $C$, what can be useful command for mathematica? [Here $\{3,2\}$ is not in $B$, $C$]

For example, given (for example) $A=\{ 3,4,5\}$ and set $B=\{9,8,3,2,4,7,5\}$, want to make function which produce true. [Since the order $3,4,5$ is same with B.

$\endgroup$

1 Answer 1

2
$\begingroup$
ClearAll[orderedSubsetQ]
orderedSubsetQ = MemberQ[Subsets[#2, {Length @ #}], #] &;

Examples:

a = {2, 3} ; b = {2, 3, 4, 5} ; c = {5, 2, 4, 3};

orderedSubsetQ[a, b]
True
orderedSubsetQ[a, c]
True
orderedSubsetQ[{3,2}, b]
False
orderedSubsetQ[{3,2}, c]
False
orderedSubsetQ[{3, 4, 5}, {9, 8, 3, 2, 4, 7, 5}]
 True
orderedSubsetQ[{4, 3, 5}, {9, 8, 3, 2, 4, 7, 5}]
 False

You can also use

ClearAll[orderedSubsetQ2, orderedSubsetQ3, orderedSubsetQ4, orderedSubsetQ5]

orderedSubsetQ2 = MatchQ[#2, Riffle[#, ___, {1, -1, 2}]] &;

orderedSubsetQ3 = DeleteCases[#2, Except[Alternatives @@ #]] == # &;

orderedSubsetQ4 = SequenceCases[#2, Riffle[#, ___]] =!= {} &;

orderedSubsetQ5 = Positive @ SequenceCount[#2, Riffle[#, ___]] &;
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.