The real combination in Groovy

Is there a way or some smart way that is easy to read to make a combination of elements in Groovy? I know Iterable#combinations or GroovyCollections#combinations , but it does partial permutation with repetition, as far as I understand it so far. See an example.

 // Groovy combinations result def e = ['a', 'b', 'c'] def result = [e, e].combinations() assert [['a', 'a'], ['b', 'a'], ['c', 'a'], ['a', 'b'], ['b', 'b'], ['c', 'b'], ['a','c'], ['b', 'c'], ['c', 'c']] == result // What I'm looking for def e = ['a', 'b', 'c'] def result = ??? assert [['a', 'b'], ['a', 'c'], ['b', 'c']] == result 

Feel free to post alternative solutions. I'm still looking for better readability (it is used in the script for non-developers) and performance (without unnecessary iterations).

+5
source share
2 answers

I'm not sure about readability, but that should do the trick.

 def e = ['a', 'b', 'c'] def result = [e, e].combinations().findAll { a, b -> a < b } assert [['a', 'b'], ['a', 'c'], ['b', 'c']] == result 

Please note that if an item appears twice in the list, its combinations will also be repeated twice. Add '.unique ()' to the end if they are undesirable

+9
source

Here's a more generalized approach that lets you specify the value of "r" for your nCr combinations. He does this while preserving permutations in sets, with sets providing uniqueness:

 // returns combinations of the input list of the provided size, r List combinationsOf(List list, int r) { assert (0..<list.size()).contains(r) // validate input def combs = [] as Set list.eachPermutation { combs << it.subList(0, r).sort { a, b -> a <=> b } } combs as List } // the test scenario... def e = ['a', 'b', 'c'] def result = combinationsOf(e, 2) assert [['a', 'b'], ['a', 'c'], ['b', 'c']] == result 
+7
source

Source: https://habr.com/ru/post/1204305/


All Articles