Home > database >  The number of combinations of the number 196225
The number of combinations of the number 196225

Time:01-12

I wanted to write a program that will show the number of combinations of the number 196225 but I failed because it showed very strange numbers and I can't quite understand why my code:

list = []
liczba = [1,9,6,2,2,5]
n_num = 0
iter=0

#ijlkmn

for i in range(6):
  n_num = liczba[i]*10**5
  for j in range(6):
    if j != i:
      n_num  = liczba[j]*10**4
      for l in range(6):
        if l != i and l != j:
          n_num  = liczba[l]*10**3
          for k in range(6):
            if k != i and k != j and k!= l:
              n_num  = liczba[k]*10**2
              for m in range(6):
                if m != i and m != j and m != l and m!= k:
                  n_num  = liczba[m]*10
                  for n in range(6):
                    if n != i and n != j and n != l and n!= k and n!= m:
                      n_num  = liczba[n]
                      if (n_num in list) == False:
                        list.append(n_num)
                        iter  = 1 

I know it looks very primitive but I only wanted the result which turned out to be incorrect, here are some of its numbers ~

196225, 196277, 196502, 196554, 197076, 197098, 199723, 199775, 200040

could someone tell me where did these numbers come from?

CodePudding user response:

if you want to make all the possible numbers using tose digits , you can use itertools module :

import itertools

liczba = [1, 9, 6, 2, 2, 5]
numbers = itertools.permutations(liczba, 6)
for num in numbers:
  print(num)

CodePudding user response:

You are adding to previous answers on each of your internal loops. Because the value of n_num is not reset at each iteration. For example:

digits = [1,2,3]

for k in range(3):
    n_num = digits[k]*10**2
    for k2 in range(3):
        if k2!=k:
            n_num  = digits[k2]*10
            print(n_num)

prints

120
150
210
240
310
330

because when k2 = 2 the first time, 30 is added to 120 yielding 150... etc etc

CodePudding user response:

I see what's wrong with your code - let's look into smaller example to see it clearly and find a solution

liczba = [1, 2, 3]
for i in range(3):
    n_num = liczba[i] * 10 ** 2 # it start with 1 * 100 = 100, good one!
    for j in range(3):
        if j != i: # for j = 0 it just pass
            n_num  = liczba[j] * 10 # j = 1, n_num is now 100   20 = 120
            for l in range(3):
                if l != i and l != j: we skip 0 and 1
                    n_num  = liczba[l] # n_num = 123, great
                    list.append(n_num)

Now we back to second loop:

    for j in range(3): # j jumps to 2
        if j != i:
            n_num  = liczba[j] * 10 # wait - here is the problem, our n_num should be 100 here again but now it's 123, so we add 30 to previous solution
            for l in range(3):
                if l != i and l != j: # l = 1
                    n_num  = liczba[l] # adding 2, but instead of 100   30   2 we have 123   30   2
                    list.append(n_num)

How to fix it? Very simple - you can just calculate number once at the end. You have all indexes already, so write

n_num = liczba[i] * 10 ** 5   liczba[j] * 10 ** 4   ...

And one small hint for the end - if you use set instead of list you won't need a check if n_num is already in result.

  •  Tags:  
  • Related