473,385 Members | 1,712 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,385 developers and data experts.

Combination iteration

391 Expert 256MB
Hi All

I was writing a bit of code to solve Euler Project #90, and came across the need to iterate through all combinations of a particular list. Any improvements would be most welcome, but I thought a class with an iterator would be appropriate.

Here's the class with a bit of code.

Expand|Select|Wrap|Line Numbers
  1. class myComb(object):
  2.  
  3.     """my interable combinatorial object"""
  4.  
  5.     def __init__(self,elements,length):
  6.  
  7.         if len(elements)>length:
  8.  
  9.             self.length=length
  10.  
  11.             self.elements=elements
  12.  
  13.             self.noEls=len(elements)
  14.  
  15.  
  16.  
  17.     def comb(self):
  18.  
  19.         """return the combination for the current index"""
  20.  
  21.         c=[]
  22.  
  23.         for i in self.index:
  24.  
  25.             c.append(self.elements[i])
  26.  
  27.         return c
  28.  
  29.  
  30.  
  31.     def __iter__(self):
  32.  
  33.         self.index=range(self.length)
  34.  
  35.         self.index[self.length-1]+=-1
  36.  
  37.         return self
  38.  
  39.  
  40.  
  41.     def next(self):
  42.  
  43.         if self.index==range(self.noEls-self.length,self.noEls):
  44.  
  45.             raise StopIteration
  46.  
  47.         i=self.length-1
  48.  
  49.         while self.index[i]==self.noEls-self.length+i:
  50.  
  51.             i+=-1
  52.  
  53.         self.index[i]+=1
  54.  
  55.         for j in range(i+1,self.length):
  56.  
  57.             self.index[j]=self.index[j-1]+1
  58.  
  59.         return self.comb()
  60.  
  61.  
  62.  
  63.  
  64.  
  65. C=myComb(["a","b","c","d","e"],3)
  66.  
  67. for i in C:
  68.  
  69.     print i 
This code will print out:
['a','b','c']
['a','b','d']
['a','b','e']
['a','c','d']
['a','c','e']
['a','d','e']
['b','c','d']
['b','c','e']
['b','d','e']
['c','d','e']

It's helpful for Euler Project type things!
Dec 21 '09 #1
1 4708
Glenton
391 Expert 256MB
So I feel like a bit of a clown, but the exact same thing can be done with the built-in module itertools.

It has permutations, combinations and product:
Here are the docs

But basic usage is:
Expand|Select|Wrap|Line Numbers
  1. In [17]: P=itertools.permutations([1,2,3])
  2.  
  3. In [18]: for p in P:
  4.    ....:     print p
  5.    ....:     
  6.    ....:     
  7. (1, 2, 3)
  8. (1, 3, 2)
  9. (2, 1, 3)
  10. (2, 3, 1)
  11. (3, 1, 2)
  12. (3, 2, 1)
  13.  
Jan 18 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

35
by: Raymond Hettinger | last post by:
Here is a discussion draft of a potential PEP. The ideas grew out of the discussion on pep-284. Comments are invited. Dart throwing is optional. Raymond Hettinger ...
2
by: Abdullah Khaidar | last post by:
Is there any iteration style we must use to get faster processing time? I've tried with some style to concat number in list. But I still don't know which one is the recommended style. >>> def...
1
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of...
1
by: karthigan | last post by:
I am writing a simple hardware test program in C that would run from Windows command line. Inside one of the loops, I have this code fragment that would display the Iteration count. { .......
75
by: Sathyaish | last post by:
Can every problem that has an iterative solution also be expressed in terms of a recursive solution? I tried one example, and am in the process of trying out more examples, increasing their...
0
by: mkyrou | last post by:
Community > Programming Help > .NET crystal report and iteration mkyrou Junior Member 3 Posts Today 01:09 PM #1 crystal report and iteration
10
by: D | last post by:
Hello everyone - I'm trying to compile an application to generate all possible 3 digit combinations using 0-9 and a-z, I've looked everywhere for a solution and I found Combinations! v 2.0 for...
9
by: news.microsoft.com | last post by:
I am looping through an iteration and I would like to test the next item but if its not the one that I want how do I put it back so that when my foreach continues it is in the next iteration? ...
1
by: greyseal96 | last post by:
Hi, I am a pretty new programmer, so I apologize in andvance if this is a dumb question... In a book that I'm reading to learn C#, it says that when using a foreach() loop, a read-only copy of...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.