[codewars_python]Best travel

来源:https://www.cnblogs.com/icris/archive/2019/04/05/10659892.html
-Advertisement-
Play Games

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, 5 ...


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)

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 給定一個字元串 (s) 和一個字元模式 (p) ,實現一個支持 '?' 和 '*' 的通配符匹配。'?' 可以匹配任何單個字元。'*' 可以匹配任意字元串(包括空字元串)。兩個字元串完全匹配才算匹配成功。 說明:s 可能為空,且只包含從 a-z 的小寫字母。p 可能為空,且只包含從 a-z 的小寫字 ...
  • 最新的dubbo和zookeeper整合的問題 生活本不易,流人遂自安 博主最新在做小項目練手,在進行dubbo和zookeeper整合的時候遇到了一些問題,在這裡這些問題做個小總結吧。 首先需要說明的是,這次使用的都是 最新的版本 。過一下我自己的配置,dubbo 2.6.6,zookeeper ...
  • 第四節 數據類型(列表、元祖) 今日內容 列表 元祖 1、列表 1.格式 2.公共方法 1.len 計算長度 2.索引 輸出某一個元素 3.切片 輸出某一段元素 4.修改(字元串/數字/布爾除外) 5.步長 選取列表中第幾個元素 6.for迴圈 註意:for和while的應用場景: 有窮盡優先使用f ...
  • XML的解析簡介: 在學習JavaScript時,我們用的DOM來解析HEML文檔,根據HTML的層級結構在記憶體中分配一個樹形結構,把HTML的標簽啊,屬性啊和文本之類的都封裝成對象。 比如:document對象,element對象,屬性對象,文本對象,Node結點對象 我們通常有兩種方式來解析XM ...
  • (1) plot是標準的繪圖庫,調用函數plot(x,y)就可以創建一個帶有繪圖的圖形視窗(其中y是x的函數)。輸入的參數為具有相同長度的數組(或列表);或者plot(y)是plot(range(len(y)),y)的簡寫。 例1:python實現使用200個採樣點來繪製sin(x),並且每隔四個點 ...
  • 一、元組tuple 1、作用 存多個值,對比列表來說,元組不可變,主要是用來讀。 2、定義 與列表類型比,只不過[ ]換成() 3、常用操作 4、元組案列 二、字典dict 特別瞭解:dict是python中僅存的mapping類型 1、作用 存多個值,key-value存取,取值速度快。 2、定義 ...
  • 一,本機配置 Win10 64bit NVIDIA GeForce GTX 960M Python3.7(Anaconda) 二,安裝CUDA 親測,TensorFlow-gpu1.13.1支持cuda10.0的版本,所以我們可直接選擇cuda10.0的版本 Window10下載CUDA10 安裝步 ...
  • PHP原生寫的生成圖片縮略圖類,本文以京東商品圖片為例,分別生成三種不同尺寸的圖片。調用方法很簡單隻要傳參數高度和寬度,及新圖片的名稱。 引入縮略圖類 生成三個不同尺寸縮略圖 本實例下載:https://www.sucaihuo.com/php/867.html ...
一周排行
    -Advertisement-
    Play Games
  • 前言 本文介紹一款使用 C# 與 WPF 開發的音頻播放器,其界面簡潔大方,操作體驗流暢。該播放器支持多種音頻格式(如 MP4、WMA、OGG、FLAC 等),並具備標記、實時歌詞顯示等功能。 另外,還支持換膚及多語言(中英文)切換。核心音頻處理採用 FFmpeg 組件,獲得了廣泛認可,目前 Git ...
  • OAuth2.0授權驗證-gitee授權碼模式 本文主要介紹如何筆者自己是如何使用gitee提供的OAuth2.0協議完成授權驗證並登錄到自己的系統,完整模式如圖 1、創建應用 打開gitee個人中心->第三方應用->創建應用 創建應用後在我的應用界面,查看已創建應用的Client ID和Clien ...
  • 解決了這個問題:《winForm下,fastReport.net 從.net framework 升級到.net5遇到的錯誤“Operation is not supported on this platform.”》 本文內容轉載自:https://www.fcnsoft.com/Home/Sho ...
  • 國內文章 WPF 從裸 Win 32 的 WM_Pointer 消息獲取觸摸點繪製筆跡 https://www.cnblogs.com/lindexi/p/18390983 本文將告訴大家如何在 WPF 裡面,接收裸 Win 32 的 WM_Pointer 消息,從消息裡面獲取觸摸點信息,使用觸摸點 ...
  • 前言 給大家推薦一個專為新零售快消行業打造了一套高效的進銷存管理系統。 系統不僅具備強大的庫存管理功能,還集成了高性能的輕量級 POS 解決方案,確保頁面載入速度極快,提供良好的用戶體驗。 項目介紹 Dorisoy.POS 是一款基於 .NET 7 和 Angular 4 開發的新零售快消進銷存管理 ...
  • ABP CLI常用的代碼分享 一、確保環境配置正確 安裝.NET CLI: ABP CLI是基於.NET Core或.NET 5/6/7等更高版本構建的,因此首先需要在你的開發環境中安裝.NET CLI。這可以通過訪問Microsoft官網下載並安裝相應版本的.NET SDK來實現。 安裝ABP ...
  • 問題 問題是這樣的:第三方的webapi,需要先調用登陸介面獲取Cookie,訪問其它介面時攜帶Cookie信息。 但使用HttpClient類調用登陸介面,返回的Headers中沒有找到Cookie信息。 分析 首先,使用Postman測試該登陸介面,正常返回Cookie信息,說明是HttpCli ...
  • 國內文章 關於.NET在中國為什麼工資低的分析 https://www.cnblogs.com/thinkingmore/p/18406244 .NET在中國開發者的薪資偏低,主要因市場需求、技術棧選擇和企業文化等因素所致。歷史上,.NET曾因微軟的閉源策略發展受限,儘管後來推出了跨平臺的.NET ...
  • 在WPF開發應用中,動畫不僅可以引起用戶的註意與興趣,而且還使軟體更加便於使用。前面幾篇文章講解了畫筆(Brush),形狀(Shape),幾何圖形(Geometry),變換(Transform)等相關內容,今天繼續講解動畫相關內容和知識點,僅供學習分享使用,如有不足之處,還請指正。 ...
  • 什麼是委托? 委托可以說是把一個方法代入另一個方法執行,相當於指向函數的指針;事件就相當於保存委托的數組; 1.實例化委托的方式: 方式1:通過new創建實例: public delegate void ShowDelegate(); 或者 public delegate string ShowDe ...