Benka Posted April 9, 2010 Posted April 9, 2010 (edited) Hello I'm trying to produce a list of all possible unique combinations from another list. The order does not matter. For example: combinaisons_uniques (a, b, c, d) will return : a, b, c, d a, b, c a, b, d a, b a, c a, d a, c, d b, c, d b, c b d b c, d c d any Idea? Edited April 9, 2010 by Guest
Søren Dyhr Posted April 9, 2010 Posted April 9, 2010 There is no apparent need to go recursive here since the number of combinations is down to 16 (15) - making it recursive involves an overhead and safeguarding not needed here. The function can then by and large be writen like you have put it, including a pilcrow at the end of each line... Where is the real problem here? --sd
Benka Posted April 9, 2010 Author Posted April 9, 2010 the real problem is that i don't know how to do... :-(
TheTominator Posted April 9, 2010 Posted April 9, 2010 (edited) Your example list has 4 items. Do you know in advance if all of the lists for your custom function will have 4 items? If it can be different from 4, is there a maximum number of items it will have? Edited April 9, 2010 by Guest
Benka Posted April 9, 2010 Author Posted April 9, 2010 (edited) no, it could be from 2 to 10 items in the list. I found a java source : http://stackoverflow.com/questions/403865/algorithm-to-sum-up-a-list-of-numbers-for-all-combinations http://www.briandunning.com/cf/1023 unique_combinations_counter (number_values) Edited April 9, 2010 by Guest
Søren Dyhr Posted April 9, 2010 Posted April 9, 2010 Although I spotted you missed out a combination, could it be witten this way: --sd Benka.zip
Benka Posted April 9, 2010 Author Posted April 9, 2010 Thanks. But there is a problem. With 4 items, there are 15 combinations, but with 7 items, there are 127 combinations... I'm trying to make a custom function to do this.
Søren Dyhr Posted April 9, 2010 Posted April 9, 2010 (edited) Ah yes then it begins to make sense to make it recursive instead. But it was a little unclear in the beginning of the thread! --sd Edited April 9, 2010 by Guest
TheTominator Posted April 9, 2010 Posted April 9, 2010 (edited) Usage: ListCombinations("a¶b¶c¶d", ", ") Custom Function: (uses recursion) ListCombinations(listOfValues; separator) = Let( [ listLength = ValueCount(listOfValues); lastValue = GetValue(listOfValues; listLength); lesserList = Case( listLength > 1; ListCombinations(LeftValues(listOfValues; listLength - 1); separator); "" ) ]; Case( listLength = 0; ""; listLength = 1; RightValues(listOfValues; 1); /* Ensures ¶ at end */ lastValue & "¶" & lesserList & /* already terminated by ¶ */ Substitute(lesserList; "¶"; separator & lastValue & "¶") ) ) Edited April 9, 2010 by Guest
Benka Posted April 9, 2010 Author Posted April 9, 2010 (edited) :goodpost: It works! Thanks a lot! :thankyou: Edited April 9, 2010 by Guest
Benka Posted April 29, 2010 Author Posted April 29, 2010 Hello I'm trying to produce a list of all possible unique consecutive combinations from another list. The order does matter. For example: combinaisons_uniques (a, b, c, d) will return a,b,c,d a,b,c a,b a b,c,d b,c b c,d c d any Idea?
Søren Dyhr Posted April 29, 2010 Posted April 29, 2010 Whats wrong with the last reply you got in this direction? http://fmforums.com/forum/showtopic.php?tid/214260/post/354242/hl//fromsearch/1/#354242 ...is it just the sortorder that makes it wrong? --sd
TheTominator Posted April 29, 2010 Posted April 29, 2010 This situation is different from the older one in that each result in the list must contain sequential letters from the original sequence without skipping a letter. Notice that "b, d", "a, d", and so on are not in the list as they were in the older post. From what I can tell, this is essentially the result of Middle("abcd", n, m) where n and m are appropriately chosen and commas are added. In designing the function I would use MiddleValues("a¶b¶c¶d"; n;m) and substitute "¶" for "," at the end.
Søren Dyhr Posted April 29, 2010 Posted April 29, 2010 Ah yes I now as well spotted the difference - --sd
TheTominator Posted April 29, 2010 Posted April 29, 2010 I think the custom function described here provides most of this functionality. That function has duplicate entries and doesn't separate by commas, but it does provide a starting point.
TheTominator Posted April 29, 2010 Posted April 29, 2010 (edited) This works with the caveat that it produces some duplicates in the results. (Maybe someone can suggest a tweak that prevents the duplicates.) ListSequences("a¶b¶c¶d"; ",") yields a,b,c,d a,b,c a,b a b b,c b c b,c,d b,c b c c,d c d // ListSequences(listOfValues; separator) Let( [ listLength = ValueCount(listOfValues); properList = RightValues(listOfValues; listLength) /* Ensures ¶ at end */ ]; Case( listLength = 0; ""; listLength = 1; properList; Substitute( Left( properList; Length(properList) - 1); "¶"; separator) & "¶" & ListSequences(LeftValues(properList; listLength-1); separator) & ListSequences(RightValues(properList; listLength-1); separator) ) ) Edited April 29, 2010 by Guest
Benka Posted April 30, 2010 Author Posted April 30, 2010 Hello Thank you all for your answers! Sorry for the response time I'm in France. I'll try to implement a duplicate management in this function.
Recommended Posts
This topic is 5332 days old. Please don't post here. Open a new topic instead.
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now