14
$\begingroup$

Consider a function f[l1_List,l2_List] which is given lists of integers. Whenever two of the lists contain completely disjoint sets of integers OR one of the lists is completely contained in the other, I would like the function to return True. Whenever both lists overlap, but not completely, I would like the function to return False. So for example:

f[{1,2},{3,4,5}]

True

f[{1,2,3,4},{2,3}]

True

But

f[{1,2,3},{3,4}]

False

Is there such a function in Mathematica? Or maybe one can implement it conveniently? Thanks for any suggestion!

$\endgroup$
17
$\begingroup$

Per comment,

f[l1_, l2_] := MemberQ[{l1, l2, {}}, l1 ⋂ l2]

should do the job handily, and look at 10.x (if you're on it) functions like ContainsAll, etc. for alternative ways of doing the same.

Edit: BTW - I assumed sorted lists w/o duplicates, as in your example. If they're not sorted,

 f[l1_, l2_] := MemberQ[Sort /@ {l1, l2, {}}, l1 ⋂ l2]

takes care of that.

If duplication in a list is allowed, you'll need to describe how that's to be handled.

If the lists are as in the OP (strictly sequential integers with no duplication), this will be goofy fast on large lists:

f3[l1_, l2_] := 
 OrderedQ[{l1[[1]], l2[[1]], l2[[-1]], l1[[-1]]}] || 
  OrderedQ[{l2[[1]], l1[[1]], l1[[-1]], l2[[-1]]}] || 
   (l1[[1]] > l2[[-1]] || l2[[1]] > l1[[-1]])
$\endgroup$
  • $\begingroup$ Actually, I find such a solution even better than a black box function. $\endgroup$ – Kagaratsch May 19 '16 at 21:46
  • 1
    $\begingroup$ Very elegant solution. +1. $\endgroup$ – Leonid Shifrin May 19 '16 at 23:28
  • 2
    $\begingroup$ @LeonidShifrin: A +1 from LS? Like the Pope inviting me to a bible reading. Appreciated! $\endgroup$ – ciao May 19 '16 at 23:42
  • 1
    $\begingroup$ Lol. I should make a fake new account and express my delight with others' answers using that one :). Seriously, though, your code is as close to a mathematical solution as it gets, and at the same time idiomatic, brief and using only the core language (the code using new functions is nice, but I prefer your version, because it exposes internals more). This is the type of code that the intersection of math and programming should make possible. Unfortunately, it is hard to get this level of expressiveness in the majority of cases. $\endgroup$ – Leonid Shifrin May 19 '16 at 23:49
12
$\begingroup$

For this one, you will need 10.2 or higher (ContainsAll was introduced in 10.2):

f[l1_, l2_] := Or[
 DisjointQ[l1, l2],
 ContainsAll[l1, l2],
 ContainsAll[l2, l1]
]

Including this solution because it might be easier to read this code since it closely encodes your written description.

$\endgroup$
  • $\begingroup$ Nice one. I wasn't aware of these, good to know. +1 $\endgroup$ – Leonid Shifrin May 19 '16 at 23:29
3
$\begingroup$
ClearAll[g]
g = Through[Or[ContainsNone, ContainsOnly, ContainsAll]@##]&;

g[{1,2,3}, {4,5}]

True

g[{1,2,3,4,5}, {4,5}]

True

g[{1,2,3,4}, {4,5}]

False

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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