[英]How to pull highest value out of an array and double its value
所以我有一些代碼可以給我組合。 我想從每個組合中找到最高的driver_points值,然后將其值加倍。
這是我目前的代碼:
# DRIVER AND TEAM ARRAYS
driver_points = { "john" => 18.1, "mike" => 19.3, "paul" => 15.6, "mark" => 1.1 }
driver_price = { "john" => 4.0, "mike" => 5.0, "paul" => 6.0, "mark" => 2.1 }
team_points = { "cowboys" => 20.1, "bears" => 19.3, "lions" => 15.6, "united" => 2.8 }
team_price = { "cowboys" => 1.0, "bears" => 2.0, "lions" => 3.0, "united" => 2.4 }
# DEFINE VARIABLE AND TARGET
teams = team_price.keys
drivers = driver_price.keys
target = 13.5
# CREATE METHOD TO SUM BOTH PRICES AND POINTS FOR GIVEN COMBINATION
def add_up(combo, ht, hd)
t, d = combo
ht[t] + hd.values_at(*d).sum
end
# ALL POSSIBLE COMBINATIONS OF TEAM AND DRIVERS
all_driver_combos = drivers.combination(3).to_a
all_combos = teams.product(all_driver_combos)
# SHOW ALL COMBOS WHERE SUM DOES NOT EXCEED TARGET
valid_combos = all_combos.select do |c|
add_up(c, team_price, driver_price) <= target
end
# SORT VALID COMBOS BY SUM OF POINTS FOR EACH ELEMENT
ordered = valid_combos.sort_by do |c|
-add_up(c, team_points, driver_points)
end
#OUTPUT
@combo = ordered.map do |c|
{ c.join(" ")=>{ price: add_up(c, team_price, driver_price),
points: add_up(c, team_points, driver_points).round(2) } }
end
當前根據目標值輸出組合(1 個團隊和 3 個車手),但按最高分排序:
[{"cowboys john mike mark"=>{:price=>12.1, :points=>58.6}}, {"bears john mike mark"=>{:price=>13.1, :points=>57.8}}, {"cowboys john paul mark"=>{:price=>13.1, :points=>54.9}}, {"united john mike mark"=>{:price=>13.5, :points=>41.3}}]
我希望能夠根據每個組合的最高driver_points找到最高的驅動程序,並將其點值加倍。
例如在我的第一個輸出組合中: {"cowboys john mike mark"=>{:price=>12.1, :points=>58.6}}
我希望能夠找到三人中得分最高的車手 - 這將是約翰、邁克和馬克中的一個,因為我不想包括球隊。 答案是邁克,因為他有 19.3 分。 然后將 Mike 的駕駛員積分加倍,然后應該更新:points=>總和。 給出新的總分 77.9。 此外,我希望能夠將最高點驅動程序移動到驅動程序組合的開頭,以便在視圖中進行樣式設置。
所以我想要的 output 是這樣的:
團隊 | 雙倍的 | 組合 | 價格 | 積分 |
---|---|---|---|---|
牛仔 | 麥克風 | 約翰馬克 | 12.1 | 77.9 |
你已經計算了
valid_combos
#=> [["cowboys", ["john", "mike", "mark"]],
# ["cowboys", ["john", "paul", "mark"]],
# ["bears", ["john", "mike", "mark"]],
# ["united", ["john", "mike", "mark"]]]
考慮組合
c = ["cowboys", ["john", "mike", "mark"]]
20.1 18.1 19.3 1.1
其中, "cowboys"
的團隊積分和該組合中三位車手各自的車手積分如上所示。 (有人需要和馬克談談。)
這種組合的總分被認為是
add_up(c, team_points, driver_points)
#=> 58.6
為此,我們希望添加19.3
分,這是"Mike"
的車手積分,因為他在組合的三位車手中得分最高:
58.6 + 19.3
#=> 77.9
我們可以在代碼中執行此操作,如下所示。
def add_dbl(combo, team_points, driver_points)
t, ds = combo
dmax = ds.max_by { |d| driver_points[d] }
driver_points[dmax] + add_up(combo, team_points, driver_points)
end
對於上面定義的組合c
,
add_dbl(c, team_points, driver_points)
#=> 77.9
正如預期的那樣。 我們現在可以使用add_dbl
代替add_up
來對組合進行排序。
ordered = valid_combos.sort_by do |c|
-add_dbl(c, team_points, driver_points)
end
#=> [["cowboys", ["john", "mike", "mark"]],
# ["bears", ["john", "mike", "mark"]],
# ["cowboys", ["john", "paul", "mark"]],
# ["united", ["john", "mike", "mark"]]]
以下代碼重新排列驅動程序,使驅動程序點數最高的驅動程序按ordered
出現在每個驅動程序數組中。
ordered.each do |_t,ds|
imax = ds.each_index.max_by { |i| driver_points[ds[i]] }
ds.unshift(ds.delete_at(imax))
end
#=> [["cowboys", ["mike", "john", "mark"]],
# ["bears", ["mike", "john", "mark"]],
# ["cowboys", ["john", "paul", "mark"]],
# ["united", ["mike", "john", "mark"]]]
參見Array#each_index 、Enumerable#max_by 、 Array#unshift和Array#delete_at 。 請注意, Array#each返回其接收者(已ordered
),但我們正在修改該數組。
要僅對驅動程序進行排序,從最高驅動程序點到最低驅動程序點,請按如下方式修改上述表達式。
ordered.each do |_t,ds|
ds.sort_by! { |d| -driver_points[d] }
end
#=> [["cowboys", ["mike", "john", "mark"]],
# ["bears", ["mike", "john", "mark"]],
# ["cowboys", ["john", "paul", "mark"]],
# ["united", ["mike", "john", "mark"]]]
巧合的是,這arrays的驅動與上面幾位沒有區別。
參見 數組#sort_by! .
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.