第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

去元組替代

去元組替代

Go
嗶嗶one 2022-07-11 10:35:02
我正在學(xué)習(xí)麻省理工學(xué)院計(jì)算思維和數(shù)據(jù)科學(xué)導(dǎo)論課程第 2課講座 PDF,我正在嘗試將以下樹(shù)搜索算法 python 代碼翻譯成 Golang。主要問(wèn)題是python代碼使用了一個(gè)元組。def maxVal(toConsider, avail):    """Assumes toConsider a list of items, avail a weight       Returns a tuple of the total value of a solution to the         0/1 knapsack problem and the items of that solution"""    if toConsider == [] or avail == 0:        result = (0, ())    elif toConsider[0].getCost() > avail:        #Explore right branch only        result = maxVal(toConsider[1:], avail)    else:        nextItem = toConsider[0]        #Explore left branch        withVal, withToTake = maxVal(toConsider[1:],                                     avail - nextItem.getCost())        withVal += nextItem.getValue()        #Explore right branch        withoutVal, withoutToTake = maxVal(toConsider[1:], avail)        #Choose better branch        if withVal > withoutVal:            result = (withVal, withToTake + (nextItem,))        else:            result = (withoutVal, withoutToTake)        return resultdef testMaxVal(foods, maxUnits, printItems = True):    print('Use search tree to allocate', maxUnits,          'calories')    val, taken = maxVal(foods, maxUnits)    print('Total value of items taken =', val)    if printItems:        for item in taken:            print('   ', item)我知道 Go 沒(méi)有元組,我四處尋找解決方法。我嘗試了這個(gè)解決方案但沒(méi)有運(yùn)氣:type Food struct {    name     string    value    int    calories int}type Pair struct{ //found this work around on another stack overflow question but I think I incorrectly implemented it     a,b interface{}}
查看完整描述

1 回答

?
UYOU

TA貢獻(xiàn)1878條經(jīng)驗(yàn) 獲得超4個(gè)贊

Go 沒(méi)有元組,但它可以返回多個(gè)值:


func maxval(toConsider []Food, avail int) (int,[]Food) {

   if len(toConsider)==0 || avail == 0) { // len(toConsider)==0 will work even if toConsider is nil

      return 0,nil

   }

 ...

}


查看完整回答
反對(duì) 回復(fù) 2022-07-11
  • 1 回答
  • 0 關(guān)注
  • 148 瀏覽
慕課專(zhuān)欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)