# Python Challenge - Number Permutation

This community-built FAQ covers the “Number Permutation” code challenge in Python. You can find that challenge here, or pick any challenge you like from our list.

## Top Discussions on the Python challenge Number Permutation

There are currently no frequently asked questions or top answers associated with this challenge – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this challenge. Ask a question or post a solution by clicking reply ( ) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this challenge, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

## Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply ( ) below!
You can also find further discussion and get answers to your questions over in #get-help.

Agree with a comment or answer? Like ( ) to up-vote the contribution!

Need broader help or resources? Head to #get-help and #community:tips-and-resources. If you are wanting feedback or inspiration for a project, check out #project.

Looking for motivation to keep learning? Join our wider discussions in #community

Found a bug? Report it online, or post in #community:Codecademy-Bug-Reporting

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Does anybody know how to do this? I’ve been trying, but I can’t.

from itertools import product as p def makeNumber(z): # Write your code here products = [] solutions = 0 for i in range(1,6): products.append(x for x in p(range(1,10), repeat=i)) for x in products: for y in x: if sum(y) == z: print(y) solutions += 1 return '\nThere are {} solutions'.format(solutions) print(makeNumber(3))

@aryabadri8979868753 found a solution, posted below

from itertools import product, chain def makeNumber(z): res = [] for i in range(1, 6): res.append([x for x in product((y for y in range(1, 10)), repeat=i) if sum(x) == z]) return len(list(chain.from_iterable(res))) print(makeNumber(45))

I did a brute force approach instead of recursion this time:
I iterated through all possible series of 5 numbers from 0 - 9 (with repetition), and counted only the relevant ones (meaning the ones where the numbers added up to the given number z, where there were no zeros that weren’t at the beginning).
I used an integer (meaning its digits) as each permutation, so that I could iterate through all the permutations.

my code
``````def checkPermutation(n, sum_of_digits = None, max_exp_of_10 = 4):
# get sum of digits of n, check if equals sum_of_digits
# can have leading 0s, but no 0s after a non-zero

previous_was_nonzero = False
total = 0  # will store sum of digits so far

checker = 10 ** max_exp_of_10

while checker >= 1:
digit = n // checker
if digit == 0:
if previous_was_nonzero:
# if zero comes after nonzero, return False
return False
else:
total += (digit % 10)
previous_was_nonzero = True
n = n % checker
checker = checker // 10

if sum_of_digits is None:
return (total == sum_of_digits)

def makeNumber(z):
if (z < 1) or (z > 45):
return 0

count = 0
for n in range(1, 100000):
if checkPermutation(n, z):
count += 1
return count

print(makeNumber(21))
``````
``````
def makeNumber(z):

lst = [[]] # temp lst to create tuple in, inner list.
cnt = 0 # store the count of permitations

for idx in range(5): # Limit to 5 digit in tuple
lst = [tuple(i,) + (x,) for i in lst for x in range(1, z+1) if x <= 9]
# create permutations in a lst

for i in lst: # loop trough list and put a condition to get the count of the sum that equals z
if sum(i) == z:
cnt += 1
return cnt

print(makeNumber(4))

``````
def D(z, n): if(z == 0): return 1 elif(z < 0 or n == 0): return 0 else: total = 0 for i in range(1,10): total += D(z-i, n-1) return total def makeNumber(z): # Write your code here if (z == 0): return 0 return D(z, 5) for i in range(45): print(i, makeNumber(i))
from itertools import product def makeNumber(z): #defining initial array, digit array and sorting array a = [] l = range(1, 10) s = [] #getting all possible permutations for i in range(1, 6): s.append(product(l, repeat=i)) #searching for elements in initial array with sum equal to z for element in s: for i in element: if sum(i) == z: a.append(i) #returning number of elements found return len(a) print(makeNumber(4))

A small summary of my code:

1. Importing product function from itertools (to not find the permutations manually).
2. Initializing our permutations counter on 0 and creating a list for saving them.
3. Iterating throught 1 to 5 (amount of possible numbers in our permutations).
4. Saving on our list “products” the result of all the possible permutations after calling the function products (imported).
5. Checking if any of these permutations match our parameter z. In this case, we update the counter.
from itertools import product def makeNumber(z): count = 0 products = list() for it in range(1,6): products = product(range(1,10), repeat=it) for perm in products: if sum(perm) == z: count += 1 return count print(makeNumber(23))
import itertools def makeNumber(z): return len([perm for _ in range(1, 5 + 1) for perm in itertools.product(range(1, 10), repeat=_) if sum(perm) == z]) print(makeNumber(45))
def perm(z, dig, base): # If number is 0 or remaining digits are 0 if z < 1 or dig < 1: return 0 # If number is lower than base, count current number as a permutation total = 1 if z < base else 0 # Calculate the number of permutations for each number at base for i in range(1,base): total += perm(z-i, dig-1, base) return total def makeNumber(z): return perm(z,5,10) # number, digits, base print(makeNumber(21))