[codewars_python]Best travel
2019-04-11 10:33:25来源:博客园 阅读 ()
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- ?
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:
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:
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:
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方法可实现排列组合,用法如下:
from itertools import combinations test_data = [100, 76, 56, 44, 89] for x in combinations(test_data, 3): print(x)
打印结果如下:
(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)
原文链接:https://www.cnblogs.com/icris/p/10659892.html
如有疑问请与原作者联系
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:常见数据类型及其方法
下一篇:跟python的第一次会晤
- python3基础之“术语表(2)” 2019-08-13
- python3 之 字符串编码小结(Unicode、utf-8、gbk、gb2312等 2019-08-13
- Python3安装impala 2019-08-13
- 小白如何入门 Python 爬虫? 2019-08-13
- python_字符串方法 2019-08-13
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash