0

I hope all you are doing great. I have an interesting question, which has stuck me. Its about generating combinations in a precise order. For example i have 4 variables(can be vary) and these 4 variables has some limit to increase for example in this case 2. so i want to generate 2d matrix in a order as:


0 0 0 0
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
1 1 0 0
1 0 1 0
1 0 0 1
0 1 1 0
0 1 0 1
0 0 1 1 
1 1 1 0
0 1 1 1
1 1 1 1 
2 0 0 0
0 2 0 0
0 0 2 0
0 0 0 2
2 1 0 0
2 0 1 0 
......
......
and so on.

the number of variables ( in this case 4 ) can be varied and also the maximum limit ( in this case 4) can be varied. Even i have also find all possible combinations but i am not able to arrange them in this sequence. it would be great if somebody gives an answer. cheers!

  • 1
    possible duplicate of [Find all combinations of a given set of numbers](http://stackoverflow.com/questions/1991361/find-all-combinations-of-a-given-set-of-numbers) and [number of possible combinations in matrix](http://stackoverflow.com/questions/11439497/number-of-possible-combinations-in-matrix?rq=1), among many others. – Ken White Jul 31 '14 at 20:59
  • @KenWhite its totally different in logic...here we just know the number of input of variables and the maximum limit of each variable. than you need to calculate all combinations which may be in thousands and than you need to build a matarix containing all these number of combinations... for example for 4 inputs and maximum limit of each is 6, than the number of combinations would be 2041. i hope you understand – user3897269 Jul 31 '14 at 21:07
  • 1
    Combinations will be from `0 0 0 0` to `9 9 9 9`, inclusive? – Fiddling Bits Jul 31 '14 at 21:14
  • @FiddlingBits. yes, you are right. – user3897269 Jul 31 '14 at 21:27
  • so if you say that limit for all of 4 variables is 9 9 9 9 , than there will be 10,000 possible combinations. – user3897269 Jul 31 '14 at 21:28
  • I assume you don't want to simply print `0 0 0 0` to `9 9 9 9` sequentially. What equation, or what is the logic, used to determine what number is printed? – Fiddling Bits Jul 31 '14 at 21:31
  • This is what i am trying to find how to build them sequentially. and i want to print them in a sequence. – user3897269 Jul 31 '14 at 21:35
  • I understand that the other topics give you suggestions on where to start, and your post here is nothing but a requirements statement. We're happy to help, but you have to at least show some effort to solve it yourself first, and that effort includes doing some research. – Ken White Jul 31 '14 at 21:46
  • I have done some research...thats why i am asking at the end...if you know omething please comment...thankyou – user3897269 Jul 31 '14 at 21:50

1 Answers1

0

I'm going to assume you've got n variables, each of which is allowed to range from 0 to b-1. What you want is just counting n-digit numbers in base b. For example, if n = 2 and b = 3, then the sequence you want to produce is

00
01
02
10
11
12
20
21
22

To implement this, write a loop something like the following: (warning: untested code)

def inc(v, b):
  for i in range(len(v)):
     v[i] = v[i] + 1
     if v[i] < b:
        break
     v[i] = 0
def is_zero(v):
   for i in range(len(v)):
      if v[i] != 0:
         return False
   return True

v = [0, 0, 0]
b = 3
while True:
  print(v)
  inc(v, b)
  if is_zero(v):
     break

If you look carefully at how this works, you should see how to generalize it if your variables have different upper bounds.

Dale Hagglund
  • 16,074
  • 4
  • 30
  • 37