前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >[codewars_python]Bes

[codewars_python]Bes

作者头像
py3study
发布2020-01-16 11:55:40
4570
发布2020-01-16 11:55:40
举报
文章被收录于专栏:python3
Instructions

John and Mary want to travel between a few towns A, B, C ... Mary has on a sheet of paper a list of distances between these towns. ls = [50, 55, 57, 58, 60]. John is tired of driving and he says to Mary that he doesn't want to drive more than t = 174 miles and he will visit only 3 towns.

Which distances, hence which towns, they will choose so that the sum of the distances is the biggest possible

  • to please Mary and John- ?

Example:

With list ls and 3 towns to visit they can make a choice between:[50,55,57],[50,55,58],[50,55,60],[50,57,58],[50,57,60],[50,58,60],[55,57,58],[55,57,60],[55,58,60],[57,58,60].

The sums of distances are then: 162, 163, 165, 165, 167, 168, 170, 172, 173, 175.

The biggest possible sum taking a limit of 174 into account is then 173 and the distances of the 3 corresponding towns is [55, 58, 60].

The function chooseBestSum (or choose_best_sum or ... depending on the language) will take as parameters t (maximum sum of distances, integer >= 0), k (number of towns to visit, k >= 1) and ls (list of distances, all distances are positive or null integers and this list has at least one element). The function returns the "best" sum ie the biggest possible sum of k distances less than or equal to the given limit t, if that sum exists, or otherwise nil, null, None, Nothing, depending on the language. With C++, C, Rust, Swift, Go, Kotlin return -1.

Examples:

代码语言:javascript
复制
ts = [50, 55, 56, 57, 58]` `choose_best_sum(163, 3, ts) -> 163
xs = [50]` `choose_best_sum(163, 3, xs) -> nil (or null or ... or -1 (C++, C, Rust, Swift, Go)
ys = [91, 74, 73, 85, 73, 81, 87]` `choose_best_sum(230, 3, ys) -> 228

My solution:

代码语言:javascript
复制
from itertools import combinations
def choose_best_sum(t, k, ls):
    L = []
    for lst in combinations(ls,k):
        if sum(lst)<=t:
            L.append(lst)
    result = [sum(n) for n in L]
    if result:
        return max(result)
    else:
        return None

Best solution:

代码语言:javascript
复制
import itertools
def choose_best_sum(t, k, ls):
    try: 
        return max(sum(i) for i in itertools.combinations(ls,k) if sum(i)<=t)
    except:
        return None

---------------------------------------------------------------------------

itertools库combinations方法可实现排列组合,用法如下:

代码语言:javascript
复制
from itertools import combinations

test_data = [100, 76, 56, 44, 89]
for x in combinations(test_data, 3):
    print(x)

打印结果如下:

代码语言:javascript
复制
(100, 76, 56)
(100, 76, 44)
(100, 76, 89)
(100, 56, 44)
(100, 56, 89)
(100, 44, 89)
(76, 56, 44)
(76, 56, 89)
(76, 44, 89)
(56, 44, 89)
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019/05/24 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Instructions
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档