Codewars--How many numbers III?

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_replacementcan’t be replace by combinations
    For detailed reasons, please click .This link contains almost the usage of the itertools.
    在这里插入图片描述

在这里插入图片描述

  • "".join('%s' %i for i in list1)
    If the list contains numbers instead of string, we can’t use join() 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, use int() function change the datatype of the string into integer.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值