Welcome Guest
Log In | Register )
You last visited December 8, 2016, 10:57 am
All times shown are
Eastern Time (GMT-5:00)

Combination generator....

Topic closed. Posted 12 years ago. No replies.

Page 1 of 1
New Member
Member #14832
May 4, 2005
5 Posts
Posted: May 5, 2005, 10:42 am - IP Logged

This is not exactly a system but maybe it is usefull to someone...

It's a simple python script, just copy it to a file name genCombs.py (or similar) and run it from the console....


#!/usr/bin/env python

     # Generates combinations

     import sys, time


     def fact(num):

         result = num*1L

         num -= 1

         while (num!=0):

             result *= num

             num -=1

         return result



     def totalCombs(maxnum,numline):

          return ( fact(maxnum)/(fact(numline)*fact(maxnum-numline)) )


     def xuniqueCombinations(items, n):

         if n==0: yield []


             for i in xrange(len(items)-n+1):

               for cc in xuniqueCombinations(items[i+1:],n-1):

                    yield [items]+cc                   


     def genCombs(maxnum,numline):

         if (maxnum>=100):

             rg = ["%03d"%(x,) for x in xrange(1,maxnum+1) ]

         elif (maxnum>=10):

             rg = ["%02d"%(x,) for x in xrange(1,maxnum+1) ]


             rg = ["%d"%(x,) for x in xrange(1,maxnum+1) ]

         for line in xuniqueCombinations(rg,numline):

             print " ".join(line)



     def StringSequence(seq):

         size = len(seq)

         format = lambda item:"%02d"%(item)

         outText = ""

         if size == 0:


         elif size == 1:

             outText = "%02d"%( seq[0], )


             outText = "%02d"%( seq[0], )

             for item in seq[1:]:

               outText += " " + "%02d"%( item, )

         return outText


     def Usage(name):

         print \

     """Generates combinations.



     %s 10 3 outfile.txt



     def Start(combs):

         print "Starting for %d combinations"%(combs,)


     def RunTime(total):

         print "ENDED! Total runtime was %04d seconds."%(total,)


     if __name__=='__main__':

         if len(sys.argv) >= 1 and len(sys.argv)<3:


         elif len(sys.argv) == 3:


             tstart = time.time()


             RunTime( time.time() - tstart )

         elif len(sys.argv) == 4:



               file1 = file(sys.argv[3],"wt")


               print "Impossible to open file"


               sys.stdout = file1

               tstart = time.time()



               ttotal = time.time() - tstart

               t1 = time.clock()

               sys.stdout = sys.__stdout__









Quick note, install python first, register the .py extension if it asks you to..... but remember this is just a small script don't expect miracles or extraordinary performance!

I will be glad to reply to any questions that anyone might have about it...

Just me here....