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. - class myComb(object):
-
-
"""my interable combinatorial object"""
-
-
def __init__(self,elements,length):
-
-
if len(elements)>length:
-
-
self.length=length
-
-
self.elements=elements
-
-
self.noEls=len(elements)
-
-
-
-
def comb(self):
-
-
"""return the combination for the current index"""
-
-
c=[]
-
-
for i in self.index:
-
-
c.append(self.elements[i])
-
-
return c
-
-
-
-
def __iter__(self):
-
-
self.index=range(self.length)
-
-
self.index[self.length-1]+=-1
-
-
return self
-
-
-
-
def next(self):
-
-
if self.index==range(self.noEls-self.length,self.noEls):
-
-
raise StopIteration
-
-
i=self.length-1
-
-
while self.index[i]==self.noEls-self.length+i:
-
-
i+=-1
-
-
self.index[i]+=1
-
-
for j in range(i+1,self.length):
-
-
self.index[j]=self.index[j-1]+1
-
-
return self.comb()
-
-
-
-
-
-
C=myComb(["a","b","c","d","e"],3)
-
-
for i in C:
-
-
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!
1 4708
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: - In [17]: P=itertools.permutations([1,2,3])
-
-
In [18]: for p in P:
-
....: print p
-
....:
-
....:
-
(1, 2, 3)
-
(1, 3, 2)
-
(2, 1, 3)
-
(2, 3, 1)
-
(3, 1, 2)
-
(3, 2, 1)
-
Sign in to post your reply or Sign up for a free account.
Similar topics
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
...
|
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...
|
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...
|
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.
{
.......
|
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...
|
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
|
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...
|
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?
...
|
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...
|
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,...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
| |