美文网首页
python马丁challenge17.Obtaining a

python马丁challenge17.Obtaining a

作者: 33jubi | 来源:发表于2019-04-29 13:20 被阅读0次

1 Obtaining a sum from a subsequence of digits
Write a program sum_of_digits.py that prompts the user for two numbers, say available_digits
and desired_sum, and outputs the number of ways of selecting digits from available_digits that
sum up to desired_sum. For instance, if available_digits is 12234 and sum is 5 then there are
4 solutions:
• one solution is obtained by selecting 1 and both occurrences of 2 (1 + 2 + 2 = 5);
• one solution is obtained by selecting 1 and 4 (1 + 4 = 5);
• one solution is obtained by selecting the first occurrence of 2 and 3 (2 + 3 = 5);
• one solution is obtained by selecting the second occurrence of 2 and 3 (2 + 3 = 5).
Here is a possible interaction:
$ python3 sum_of_digits.py

Input a number that we will use as available digits: 12234
Input a number that represents the desired sum: 5
There are 4 solutions.
$ python3 sum_of_digits.py

Input a number that we will use as available digits: 11111
Input a number that represents the desired sum: 5
There is a unique solution.
$ python3 sum_of_digits.py

Input a number that we will use as available digits: 11111
Input a number that represents the desired sum: 6
There is no solution.
$ python3 sum_of_digits.py

Input a number that we will use as available digits: 1234321
Input a number that represents the desired sum: 5
There are 10 solutions.
1

#我的解法
#这里用到了itertools的一个函数
#-》itertools.combinations(List,i)
#作用是将列表中的元素进行排列组合,实现这里的问题可以简洁不少
# Prompts the user for two numbers, say available_digits and desired_sum, and
# outputs the number of ways of selecting digits from available_digits
# that sum up to desired_sum.


import sys
import itertools


def solve(available_digits, desired_sum):
    if desired_sum < 0:
        return 0
    string=list(str(available_digits))
    List_string=[]
    for i in string:
        List_string.append(int(i))
    #print(List_string)
    
    L=[]
    for i in range(1,len(List_string)+1):
        L.append(list(itertools.combinations(List_string,i)))
    #print(L)
    L_sum=[]
    for i in L:
        for j in i:
            sum=0
            for k in j:
                sum+=k         
            L_sum.append(sum)
    nb_of_solutions=0
    for i in L_sum:
        if i==desired_sum:
            nb_of_solutions+=1
    return nb_of_solutions
    

try:
    available_digits = abs(int(input('Input a number that we will use as available digits: ')))
except ValueError:
    print('Incorrect input, giving up.')
    sys.exit()
try:
    desired_sum = int(input('Input a number that represents the desired sum: '))
except ValueError:
    print('Incorrect input, giving up.')
    sys.exit()
nb_of_solutions = solve(available_digits, desired_sum)
if nb_of_solutions == 0:
    print('There is no solution.')
elif nb_of_solutions == 1:
    print('There is a unique solution.')
else:
    print(f'There are {nb_of_solutions} solutions.')
#马丁解法
#一行迭代解决所有问题
#要理解的是下面两个式子
#solve(available_digits // 10, desired_sum) 第一个式子,比较前面迭代过程中所有可以匹配的desired_sum(本身指向前一个,就是目的值,但前一个的第一个式子又将其指向再前一个就是desired_sum - available_digits % 10,在迭代的过程中考虑了所有的可能性)  这个式子代表不包含当前值的所有情况。
#solve(available_digits // 10, desired_sum - available_digits % 10)让后面的每次迭代都考虑到目标值包含此位数值的情况  这个式子代表包含此位数值的所有情况。
import sys


def solve(available_digits, desired_sum):
    if desired_sum < 0:
        return 0
    if available_digits == 0:
        if desired_sum == 0:
            return 1
        return 0
    # Either take the last digit d form available_digits
    # and try to get desired_sum - d from the remaining digits,
    # or do not take the last digit and try to get desired_sum
    # from the remaining digits.
    return solve(available_digits // 10, desired_sum) +\
                                  solve(available_digits // 10, desired_sum - available_digits % 10)

try:
    available_digits = abs(int(input('Input a number that we will use as available digits: ')))
except ValueError:
    print('Incorrect input, giving up.')
    sys.exit()
try:
    desired_sum = int(input('Input a number that represents the desired sum: '))
except ValueError:
    print('Incorrect input, giving up.')
    sys.exit()
nb_of_solutions = solve(available_digits, desired_sum)
if nb_of_solutions == 0:
    print('There is no solution.')
elif nb_of_solutions == 1:
    print('There is a unique solution.')
else:
    print(f'There are {nb_of_solutions} solutions.')

相关文章

网友评论

      本文标题:python马丁challenge17.Obtaining a

      本文链接:https://www.haomeiwen.com/subject/rohynqtx.html