Here’s a snippet to compute all combinations of N elements over K positions.
Search
-
Recent Posts
Recent Activity
- Today, heated discussions, and a clean trunk with all pending branches reviewed and merged. Tomorrow, deployment begins! #ensemble #sprint 2 days ago
- Progress on the #Ensemble sprint! Washington feels better than I expected, btw. Being with @kapiltv and Malthe Borch helps too. 2 days ago
- Why am I so #pedantic? Can't hear the flight attendant saying "Please contact the front door." without laughing. 5 days ago
- After a few days off with a nice view of the Atlantic Forest, heading to another exciting sprint. http://tweetphoto.com/34621961 6 days ago
- RT @franciscosouza: mocker > world. Thanks @gniemeyer < Glad you're finding it helpful 6 days ago
Categories
Archive
- July 2010
- June 2010
- May 2010
- March 2010
- December 2009
- November 2009
- October 2009
- August 2009
- July 2009
- June 2009
- May 2009
- August 2008
- June 2008
- May 2008
- March 2008
- February 2008
- December 2007
- November 2007
- October 2007
- August 2007
- June 2007
- May 2007
- March 2007
- November 2006
- August 2006
- July 2006
- February 2006
- October 2005
- September 2005
- July 2005
- June 2005
- May 2005
- April 2005
- March 2005
- December 2004
- June 2004
- March 2004
- February 2004
- December 2003
- October 2003
- June 2003
hey, that someone was me! thanks for the code.
I don’t see a ‘border’ though
What I mean is that you take the border from the length of your list. At least that is what I think I see happening here.
What I meant was having a list of possible characters like ['a','b',(..) 'z']. And then also give a ‘border’. Let’s say you give a border of 3. This will effectively give you all possible 3-character combinations of *all* given characters.
I hope I explained this good enough, thanks again!
Hello Guyon! I think I understand what you meant now. The algorithm is pretty much the same:
def allcombinations(n): queue = [-1] while queue: i = queue[-1]+1 if i == 26: queue.pop() else: queue[-1] = i yield [chr(97+j) for j in queue] if len(queue) < n: queue.append(-1)The total combinations is computed by:
def total(n): return sum([26**i for i in range(1,n+1)])Which means 12356630 entries for n=5!!! Be careful.
This list is the sum of three cartesian products,
here it is cheating a bit and not in exactly the same order.
import probstat # probstat.sf.net def total(mylist): for (n) in range(len(mylist)): for (item) in probstat.Cartesian([mylist]*(n+1)): yield itemfor the list [1,2,3] it does the cartesian product of [1,2,3]
then [1,2,3] x [1,2,3] then [1,2,3] x [1,2,3] x [1,2,3]