Message Boards Message Boards

0
|
6986 Views
|
5 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Select cases from a list based on two conditions?

Let's say we have a list of lists of length 3, as below:

 {{{1}, {2}, {1, 2, 3}}, {{1}, {3}, {1, 2, 3}}, {{1}, {1, 2}, {1, 2, 
   3}}, {{1}, {2, 3}, {1, 2, 3}}, {{2}, {3}, {1, 2, 3}}, {{2}, {1, 
   2}, {1, 2, 3}}, {{2}, {2, 3}, {1, 2, 3}}, {{3}, {1, 2}, {1, 2, 
   3}}, {{3}, {2, 3}, {1, 2, 3}}, {{1, 2}, {2, 3}, {1, 2, 3}}}

Now, from that list I want to extract those lists which satisfy the following rules:

1) neighbouring elements in the list have either the same first element or last element, e.g. {{1},{1,2},{1,2,3}} or {{2},{2,3},{1,2,3}}.

2) when there are more than one element of length 1 in the list, we take the difference of all such pairs of elements. If that difference is not equal to the absolute value of 1 for all such pairs and the rest of the elements of that list satisfy Rule 1., we pick that list. We keep in mind that the lists in the list below can be of any length. So for example, we would pick {{1},{3},{1,2,3}} but not {{1},{2},{1,2,3}}.

Applying those 2 rules to the list above we would get:

{{1},{3},{1,2,3}},{{1},{1,2},{1,2,3}},{{{2},{1,2},{1,2,3}},{{2},{2,3},{1,2,3}},{{3},{2,3},{1,2,3}}

I kind of want to avoid using loops but if that's the only possibility then that's fine. If someone can see some different rules that would allow us the get the same output from that list above then I'd love to see it too. Any help is much appreciated.

5 Replies
Posted 5 years ago

The description for Rule 2 states:

So for example, we would pick {{1},{3},{1,2,3}} but not {{1},{2},{1,2,3}}.

Wouldn't {{1},{2},{1,2,3}} be eliminated by Rule 1 since 2 is not the first or last element of {1} or {1, 2, 3}?

POSTED BY: Rohit Namjoshi

Thank you for your comment. Yes, you're correct. But then {{1},{3},{1,2,3}} would also be eliminated, by Rule 1, since 3 is not the first or last element of {1} and so this the example for why we need the Rule 2. Rule 1 has to hold for all consecutive elements in the the list.

Posted 5 years ago

Crossposted here.

POSTED BY: Rohit Namjoshi

Welcome to Wolfram Community! Please make sure you know the rules: https://wolfr.am/READ-1ST

Please next time link your post to the duplicated one from MSE site.

POSTED BY: Moderation Team

With

In[1]:= Clear[data]
data = {{{1}, {2}, {1, 2, 3}}, {{1}, {3}, {1, 2, 3}}, {{1}, {1, 
    2}, {1, 2, 3}}, {{1}, {2, 3}, {1, 2, 3}}, {{2}, {3}, {1, 2, 
    3}}, {{2}, {1, 2}, {1, 2, 3}}, {{2}, {2, 3}, {1, 2, 3}}, {{3}, {1,
     2}, {1, 2, 3}}, {{3}, {2, 3}, {1, 2, 3}}, {{1, 2}, {2, 3}, {1, 2,
     3}}};

one implements the conditions straightforward

(* it is actually a conditional condition *)
Clear[wierzC1, wierzC]
wierzC1[l_List] := True /; Length[l] == 1
wierzC1[l_List] := (Length[l] - 
     Length[Select[
       Partition[l, 2, 
        1], ((#[[1, 1]] == #[[2, 1]]) || (#[[1, -1]] == #[[
             2, -1]])) &]] == 1) /; Length[l] > 1
(* preconditions:
p[i]: list of lists without sublists 
p[ii]: no empty sublists
p[iii]: length of the sublists in list is monoton
 *)
wierzC[l_List] := Block[{l0 = Length /@ l, sub1 = {}},
   If[Length[Cases[l0, 1]] > 1, (* condition 2 *)
    sub1 = Select[l, (Length[#] == 1) &];
    If[ContainsAny[
      Abs[Dot[{-1, 1}, #]] & /@ 
       First[Flatten[Tuples[sub1, {2}], {3}]], {1}],
     False, (* else: condition 1 for the rest *)
     wierzC1[Complement[l, sub1]]
     ], (* else: condition 1 *)
    wierzC1[l]
    ]
   ] /; (And @@ (VectorQ /@ 
       l)) && (And @@ (NonNegative /@ (Dot[{-1, 1}, #] & /@ 
         Partition[Length /@ l, 2, 1]))) && !ContainsAny[l, {{}}]

to reach the result

In[8]:= Select[data, wierzC]
Out[8]= {{{1}, {3}, {1, 2, 3}}, {{1}, {1, 2}, {1, 2, 3}}, {{2}, 
          {1, 2}, {1, 2, 3}}, {{2}, {2, 3}, {1, 2, 3}}, {{3}, {2, 3}, {1, 2, 3}}}

even in a language with an overwhelming rich set of keywords it is more often than not the case that a single word is not enough to let it do what you want it to do.

POSTED BY: Dent de Lion
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract