7
$\begingroup$

Use Cases to identify the sublists, within the list below, that contain an integer and its square somewhere in the list. (Hint: Use the BlankNullSequence and the named BlankSequence patterns with a Condition).

list={{6,3,2,4},{3,9,5},{1,7,2},{16,5,4},{49,7},{1,3,1},{3,2,7}};

While the earlier question (Cases to identify integers within a list) did help identify the list that contain an integer, but failed to figure out how BlankNullSequence and named BlankSequence patterns can identify the lists that contain an integer and its square somewhere in the list.

$\endgroup$
2
  • 1
    $\begingroup$ Thanks for the accept. But you might want to wait more, as you might get better answers. $\endgroup$
    – Nasser
    Commented Sep 11, 2021 at 10:02
  • $\begingroup$ @Nasser It helps a lot despite someone putting other ways. $\endgroup$ Commented Sep 11, 2021 at 10:03

5 Answers 5

8
$\begingroup$

You might find OrderlessPatternSequence useful for specifying your pattern:

Cases[{OrderlessPatternSequence[x_, y_, ___] /; y == x^2}] @ lis
{{6, 3, 2, 4}, {3, 9, 5}, {16, 5, 4}, {49, 7}, {1, 3, 1}}
$\endgroup$
4
$\begingroup$

May be

lis = {{6,3,2,4}, {3,9,5}, {1,7,2}, {16,5,4}, {49,7}, {1,3,1}, {3, 2, 7}};
Cases[lis, {___, x_, ___, y_, ___} /; (x^2 == y || y^2 == x) :> {x, y}]

gives

{{2, 4}, {3, 9}, {16, 4}, {49, 7}, {1, 1}}

The {1,1} is from {1, 3, 1} because 1 is the square of 1

However, the above does not capture all possible n and n^2 inside the list. For example given {6, 3, 2, 4, 16}, it finds {2,4} but not also {4,16} from same list.

I assumed your list has only one pair of n and n^2 in it.

Edit

Per comment, output the whole list which contains any $n,n^2$ in it

lis = {{6,3,2,4}, {3,9,5}, {1,7,2}, {16,5,4}, {49,7}, {1,3,1}, {3, 2, 7}};
Cases[lis, {a___, x_, c___, y_, b___} /; (x^2 == y || y^2 == x) :> {a,x,c,y,b}]

gives

{{6, 3, 2, 4}, {3, 9, 5}, {16, 5, 4}, {49, 7}, {1, 3, 1}}
$\endgroup$
2
  • $\begingroup$ Any suggestion on how to have the output in terms of the content of the list. For instance, {6,3,2,4} as one of the output because 2,4 included. My best guess is instead of {__, x, __, y, ___}, there will be some wildcard character for ___. $\endgroup$ Commented Sep 11, 2021 at 10:07
  • 1
    $\begingroup$ @SplendidDigitalSolutions updated, You can use any letter to capture the pattern. I am sure there are many other ways to do this. $\endgroup$
    – Nasser
    Commented Sep 11, 2021 at 10:14
2
$\begingroup$
list = 
 {{6, 3, 2, 4}, {3, 9, 5}, {1, 7, 2}, {16, 5, 4}, {49, 7}, {1, 3, 1}, {3, 2, 7}};

Using SubsetCases (new in 12.1)

sc = SubsetCases[#, {x_, y_} /; y == x^2] & /@ list

{{{2, 4}}, {{3, 9}}, {}, {{4, 16}}, {{7, 49}}, {{1, 1}}, {}}

1. pairs

Join @@ sc

{{2, 4}, {3, 9}, {4, 16}, {7, 49}, {1, 1}}

2. sublists

p = Cases[MapIndexed[List, sc], {{__}, a_} :> a]

{{1}, {2}, {4}, {5}, {6}}

Extract[list, p]

{{6, 3, 2, 4}, {3, 9, 5}, {16, 5, 4}, {49, 7}, {1, 3, 1}}

$\endgroup$
2
$\begingroup$

A variant using MemberQ and TakeList:

list = {{6, 3, 2, 4}, {3, 9, 5}, {1, 7, 2}, {16, 5, 4}, {49, 7}, {1, 
    3, 1}, {3, 2, 7}};

f[k_List] := 
 TakeList[k, {{#}, All}] & /@ Range@Length@k // 
   Map[MemberQ[Last@#, (First@First@#)^2] &] // ContainsAny[{True}]

Pick[list, f /@ list]

Select[list, f]

Result:

{{6, 3, 2, 4}, {3, 9, 5}, {16, 5, 4}, {49, 7}, {1, 3, 1}}
$\endgroup$
2
$\begingroup$
list = {{6, 3, 2, 4}, {3, 9, 5}, {1, 7, 2}, {16, 5, 4},
       {49, 7}, {1, 3, 1}, {3, 2, 7}};

Using Pick:

test = ! FreeQ[#2 == #1^2 & @@@ Union[Sort /@ Permutations[#, {2}]], True] &;

Pick[#, test /@ #] &@list

(*{{6, 3, 2, 4}, {3, 9, 5}, {16, 5, 4}, {49, 7}, {1, 3, 1}}*)
$\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.