How many numbers III?
Problem Description:
Click here to get the original problem.
We want to generate all the numbers of three digits that:
the value of adding their corresponding ones(digits) is equal to 10.
their digits are in increasing order (the numbers may have two or more equal contiguous digits)
The numbers that fulfill the two above constraints are: 118, 127, 136, 145, 226, 235, 244, 334
Make a function that receives two arguments:
the sum of digits value
the amount of desired digits for the numbers
The function should output an array with three values: [1,2,3]
1 - the total amount of all these possible numbers
2 - the minimum number
3 - the maximum numberwith
The example given above should be:
find_all(10, 3) == [8, 118, 334]
If we have only one possible number as a solution, it should output a result like the one below:
find_all(27, 3) == [1, 999, 999]
If there are no possible numbers, the function should output the empty array.
find_all(84, 4) == []
The number of solutions climbs up when the number of digits increases.
find_all(35, 6) == [123, 116999, 566666]
Features of the random tests:
Numbers of tests: 111
Sum of digits value between 20 and 65
Amount of digits between 2 and 15
Sample Tests:
test.describe("Example Tests")
test.assert_equals(find_all(10, 3), [8, 118, 334])
test.assert_equals(find_all(27, 3), [1, 999, 999])
test.assert_equals(find_all(84, 4), [])
test.assert_equals(find_all(35, 6), [123, 116999, 566666])
Describe the problem in my own words:
The problem mainly means: input to integer—(sum_dig, digs), each number has ‘digs’ digits, ‘sum_dig’ represents the total number of the ‘digs’ digits. At last, you should return :
1、the number of numbers that satisfy the criteria
2、the min number of the numbers
3、the max number of the number
Version of Python:
Solutions:
Method1:
from itertools import combinations_with_replacement
def find_all(sum_dig, digs):
combs = combinations_with_replacement(list(range(1, 10)), digs)
target = [''.join(str(x) for x in list(comb)) for comb in combs if sum(comb) == sum_dig]
if not target:
return []
return [len(target), int(target[0]), int(target[-1])]
Method2:
from itertools import *
lists=[]
list1=[]
def find_all(sum_dig, digs):
for comb in combinations_with_replacement(range(1,10),digs):
#print(comb)
if sum(comb)==sum_dig:
#print(comb)
list1=list(comb)
#print("list1: ",list1)
lists.append(int("".join('%s' %i for i in list1)))
#print(lists)
if len(lists)==0:
return []
else:
return [len(lists),lists[0],lists[-1]]
Grammar Explanation(Method2):
combinations_with_replacement
can’t be replace bycombinations
For detailed reasons, please click .This link contains almost the usage of theitertools
.
"".join('%s' %i for i in list1)
If the list contains numbers instead of string, we can’t usejoin()
function directly to change the number into string.
A way to solve the problem is to walk through the list, make each factor of the list change into string.
Finally, useint()
function change the datatype of the string into integer.