1
$\begingroup$

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.

$\endgroup$
  • 2
    $\begingroup$ (1) you can get using SplitBy[lst, #[[{1, -1}]] &] $\endgroup$ – kglr Jan 11 at 18:11
  • $\begingroup$ Thank you! Didn't know about this function, I'm very new to Mathematica. $\endgroup$ – amator2357 Jan 11 at 18:20
  • $\begingroup$ amator2357, my pleasure; welcome to mma.se. $\endgroup$ – kglr Jan 11 at 18:31
  • $\begingroup$ This question is crossposted here $\endgroup$ – Rohit Namjoshi Jan 12 at 23:10
0
$\begingroup$

I'm not sure if I should be posting it here or ask a new question but since it is related to my initial question I'll just do it here. The solution kglr suggested in the comment spits out the same list that we use as an argument, not sure why. To account for the Rule 1 I did:

Table[Select[test3,First[#[[i+1]]]==First[#[[i]]] \[Or] Last[#[[i+1]]]==Last[#[[i]]] &],{i,n-2}]

where n is the length of the longest possible vector in the list of lists, known in advance. But this would give me all the lists in which AT LEAST one pair of consecutive elements satisfy Rule 1. But this is not what I'm after. I need all elements in the list to satisfy Rule 1 simultaneously. I was trying to use ForAll but I couldn't figure it out. Any hints?

$\endgroup$
  • $\begingroup$ The solution proposed by @kglr does not result in the same list. Compare MatrixForm@lst and MatrixForm@SplitBy[lst, #[[{1, -1}]] &] $\endgroup$ – Rohit Namjoshi Jan 12 at 23:09

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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