gpt4 book ai didi

ruby - Ruby 中的并行数组

转载 作者:太空宇宙 更新时间:2023-11-03 17:31:28 25 4
gpt4 key购买 nike

我有两个数组,一个存储候选人姓名,另一个存储该候选人的票数。我正在尝试打印最大票数和相应候选人的姓名;而不是诸如 92, 4(票数,候选人索引)之类的输出,而是输出诸如 92, John 之类的内容。

我已经很接近这样做了:

puts "Candidates, index order: 0, 1, 2, 3, 4"
candidates.each { |x| puts x }
puts "Votes, index order: 0, 1, 2, 3, 4"
votes.each { |y| puts y }

votes.delete(nil)
puts "Maximum number of votes, followed by candidates array index."
puts votes.each_with_index.max { |x,y| x <=> y }

我已成功获取最大值所在的索引,但我如何使用该索引来匹配候选数组的索引以便打印名称而不是索引?

最佳答案

puts votes.zip(candidates).max_by(&:first)

关于ruby - Ruby 中的并行数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34845089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com