[codewars_python]Best travel
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:
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)
[codewars_python]Best travel的更多相关文章
- 图论 - Travel
Travel The country frog lives in has nn towns which are conveniently numbered by 1,2,…,n. Among n(n− ...
- 【BZOJ-1576】安全路径Travel Dijkstra + 并查集
1576: [Usaco2009 Jan]安全路经Travel Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 1044 Solved: 363[Sub ...
- Linux inode && Fast Directory Travel Method(undone)
目录 . Linux inode简介 . Fast Directory Travel Method 1. Linux inode简介 0x1: 磁盘分割原理 字节 -> 扇区(sector)(每 ...
- HDU - Travel
Problem Description Jack likes to travel around the world, but he doesn’t like to wait. Now, he is t ...
- 2015弱校联盟(1) - I. Travel
I. Travel Time Limit: 3000ms Memory Limit: 65536KB The country frog lives in has n towns which are c ...
- ural 1286. Starship Travel
1286. Starship Travel Time limit: 1.0 secondMemory limit: 64 MB It is well known that a starship equ ...
- Travel Problem[SZU_K28]
DescriptionAfter SzuHope take part in the 36th ACMICPC Asia Chendu Reginal Contest. Then go to QingC ...
- hdu 5441 travel 离线+带权并查集
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Problem Descript ...
- Codeforces Beta Round #51 A. Flea travel 水题
A. Flea travel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/55/problem ...
随机推荐
- swift语言点评十三-Lazy
Lazy Stored Properties A lazy stored property is a property whose initial value is not calculated un ...
- ActiveMQ学习笔记(8)----ActiveMQ的消息存储持久化
1. 概述 ActiveMQ不仅支持persistent和non-persistent两种方式,还支持消息的恢复(recovery)方式. 2. PTP Queue的存储是很简单的,其实就是FIFO的 ...
- 脚本_实时显示网卡eth0上的数据流量
#!bin/bash#功能:使用死循环,实时显示网卡eth0发送的数据包流量#作者:liusingbonwhile : do echo "本地网卡eth0的数据流量信息如下:&q ...
- Windows server 2012R清除并重建SID 用于制作封装模板
首先介绍下什么是SID SID也就是安全标识符(Security Identifiers),是标识用户.组和计算机帐户的唯一的号码.在第一次创建该帐户时,将给网络上的每一个帐户发布一个唯一的 SID. ...
- 多任务-进程之Queue的进程间通信
1.经过线程和进程的对比,不难的知道,线程和进程有相当大的区别,如全局变量资源不能够共享. 2.在不同的进程间,如何实现通信呢? 需要提及的一个概念就是Queue,它是一个消息队列,下面通过一个例子来 ...
- [codevs1048]石子归并&[codevs2102][洛谷P1880]石子归并加强版
codevs1048: 题目大意:有n堆石子排成一列,每次可合并相邻两堆,代价为两堆的重量之和,求把他们合并成一堆的最小代价. 解题思路:经典区间dp.设$f[i][j]$表示合并i~j的石子需要的最 ...
- libcudnn (R5) not found in library path
环境:Ubuntu 18.04 + Torch7 + cuda10 在运行使用cudnn的lua程序的时候产生错误: /home/majiabiao/torch/: /home/majiabiao/ ...
- 05004_Linux的其他命令和权限命令
1.其他命令 (1)显示当前所在位置 命令:pwd (2)搜索命令 a.命令:grep 要搜索的字符串 要搜索的文件 示例:搜索/etc/sudu.conf文件中包含字符串to的行 b.搜索/etc/ ...
- 【BZOJ 1503】[NOI2004]郁闷的出纳员
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 因为所有人工资同时递减. 所以可以设置一个变化值delta. 然后每个人的初始值为k 则把k-delta加入伸展树中. 会发现del ...
- 2015 Multi-University Training Contest 4 hdu 5335 Walk Out
Walk Out Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Su ...