Welcome Guest
You last visited April 24, 2018, 8:46 pm
All times shown are
Eastern Time (GMT-5:00)

# Combination generator....

Topic closed. Posted 13 years ago. No replies.

 Page 1 of 1
New Member
Rome
Italy
Member #14832
May 4, 2005
5 Posts
Offline
 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 []          else:              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) ]          else:              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:              pass          elif size == 1:              outText = "%02d"%( seq[0], )          else:              outText = "%02d"%( seq[0], )              for item in seq[1:]:                outText += " " + "%02d"%( item, )          return outText              def Usage(name):          print \      """Generates combinations.      %s         Example:      %s 10 3 outfile.txt      """%(name,name)              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:              Usage(sys.argv[0])          elif len(sys.argv) == 3:              Start(totalCombs(int(sys.argv[1]),int(sys.argv[2])))              tstart = time.time()              genCombs(int(sys.argv[1]),int(sys.argv[2]))              RunTime( time.time() - tstart )          elif len(sys.argv) == 4:              Start(totalCombs(int(sys.argv[1]),int(sys.argv[2])))              try:                file1 = file(sys.argv[3],"wt")              except:                print "Impossible to open file"              else:                sys.stdout = file1                tstart = time.time()                time.clock()                genCombs(int(sys.argv[1]),int(sys.argv[2]))                ttotal = time.time() - tstart                t1 = time.clock()                sys.stdout = sys.__stdout__                file1.close()                RunTime(ttotal)                RunTime(t1)          else:              Usage(sys.argv[0])

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!