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
Learn more about how to use this guide.
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
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))