3
$\begingroup$

I am trying to solve the following problem. I have tuples of size 3

{{a, b, c}, {a, b, c}, {a, b, c}}

I would like to find cases where

  • all the elements in a tuple are the same
  • all the elements in a tuple are different

I can solve the first problem with a named pattern something like

x = {{a, b, c}, {a, a, a}, {a, b, c}}
Cases[x, {i_,i_,i_}]

-> {{a, a, a}}

I am unable to solve the other one. Naive approach like this

x = {{a, b, c}, {a, a, a}, {a, b, c}}
Cases[x, {i_,j_,k_}]

-> {{a, b, c}, {a, a, a}, {a, b, c}}

does not work since i, j, k can be different but also can match the same element.

I am looking for a solution using patterns and rules. I could probably construct some solution based on uniqueness of the set but this is a part of larger problem so I am trying to find some elegant solution and also it serves as an exercise to learn more about patterns.

$\endgroup$
3
  • $\begingroup$ I am closing this question as already has an answer. (See the link inserted in the header above your question.) All of those methods are applicable here. If you have trouble writing one of them as a pattern please let me know. $\endgroup$
    – Mr.Wizard
    Commented Oct 15, 2014 at 19:10
  • $\begingroup$ Sorry. I were not able to find the aforementioned answer. I think I have more than enough great info to make it work. Thanks $\endgroup$ Commented Oct 15, 2014 at 20:04
  • $\begingroup$ No need to be sorry; it is often hard to find prior questions, which is why I work hard to find and link related questions and close as appropriate. I can use all the help I can get so please use the "flag" link below any posts which you feel are duplicates and let the moderators (of which I am one) know about them. $\endgroup$
    – Mr.Wizard
    Commented Oct 15, 2014 at 23:32

3 Answers 3

1
$\begingroup$

A slightly different pattern from Leonid Shifrin's answer

t = Tuples[{a, b, c}, 3];

Length[t]

27

For all of the elements being different

Length[DeleteCases[t, {___, x_, ___, x_, ___}]]

6

$\endgroup$
3
  • $\begingroup$ Bob would you mind explaining the idea behind it? Honestly I do not fully grasp how that works. $\endgroup$ Commented Oct 15, 2014 at 17:56
  • $\begingroup$ "___ (three _ characters) or BlankNullSequence[] is a pattern object that can stand for any sequence of zero or more Wolfram Language expressions." The pattern represents two identical expressions that are preceded, separated, or followed by zero or more expressions. $\endgroup$
    – Bob Hanlon
    Commented Oct 15, 2014 at 19:42
  • $\begingroup$ Awesome, if I get this right it should work for arbitrary size of a tuple. Thank you. $\endgroup$ Commented Oct 15, 2014 at 20:03
2
$\begingroup$

There is new DuplicateFreeQ. So

x = {{a, b, c}, {a, a, a}, {a, b, c}, {a, b, b}};

Cases[x, _?DuplicateFreeQ]
(* {{a, b, c}, {a, b, c}} *)
$\endgroup$
1
$\begingroup$
x = {{a, b, c}, {a, a, a}, {a, b, c}, {a, b, b}};

Cases[x, {(y_) ..}]
(* {{a,a,a}} *)
Cases[x, y_?(Length@DeleteDuplicates[#] == 1 &)]
(* {{a,a,a}} *)
Cases[x, y_?(Length@Union[#] == 1 &)]
(* {{a,a,a}} *)

Cases[x, y_?(DeleteDuplicates[#] == # &)]
(* {{a,b,c},{a,b,c}} *)
Cases[x, y_?(Length@Union[#] == Length@# &)]
(* {{a,b,c},{a,b,c}} *)
$\endgroup$
2
  • $\begingroup$ DeleteDublicates seems to be a bit faster $\endgroup$
    – ybeltukov
    Commented Oct 15, 2014 at 18:32
  • $\begingroup$ Thank you @ybeltukov; added a variant with DeleteDuplicates. $\endgroup$
    – kglr
    Commented Oct 15, 2014 at 18:58

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