Which combination of five players has the largest combined scores subject to the condition that their combined ELO's does not exceed 11,000?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP


Which combination of five players has the largest combined scores subject to the condition that their combined ELO's does not exceed 11,000?



I have a dataset which is something like the following:

Name, ELO, Score
Derek Aufderhar, 2134, 1
Hadley Kuhn, 2044, 0
Myrtie Lueilwitz, 2207, 2
Mitchell Schiller, 2036, 2
Javier Walter MD, 2485, 4
Waino Leuschke, 2486, 2
Ariel Jacobson, 2015, 3
Melvin Bailey, 2485, 0
Dovie Emmerich, 2383, 4
Adrian Stroman Jr., 2180, 1
Helen Douglas, 2352, 4
Yessenia O'Reilly, 2247, 2

I'd like to find the 5 players where they scored the most (their total score) and their ELO sum doesn't pass a specific number (which is 11.000). The dataset is dynamic every time. Any tip on where to look for doing it?



Name, ELO, Score
Derek Aufderhar, 2134, 1
Hadley Kuhn, 2044, 0
Myrtie Lueilwitz, 2207, 2
Mitchell Schiller, 2036, 2
Javier Walter MD, 2485, 4
Waino Leuschke, 2486, 2
Ariel Jacobson, 2015, 3
Melvin Bailey, 2485, 0
Dovie Emmerich, 2383, 4
Adrian Stroman Jr., 2180, 1
Helen Douglas, 2352, 4
Yessenia O'Reilly, 2247, 2





Hi! Please review How do I ask a good question? Your best bet here is to do your research, search for related topics on SO, and give it a go. If you get stuck and can't get unstuck after doing more research and searching, post a Minimal, Complete, and Verifiable example of your attempt and say specifically where you're stuck. People will be glad to help.
– T.J. Crowder
5 hours ago





I am not sure what kind of algorithm is this and how it would be the correct way of doing it. I am thinking that this is some category of algorithms which this one is achievable for doing it. If you have a similar example or a similar question to point me, I'll be more than happy so I can investigate further.
– JohnDel
5 hours ago







if the dataset is not big, picking all possible 5 players and then checking your restrictions would suffice
– juvian
5 hours ago





Yes I was thinking to sort based on score and then starting with brute force so I can find the ELO but I was wondering if there is a more elegant way of doing it. Some kind of "magic" algorithm and logic behind it.
– JohnDel
5 hours ago





Why delete the question (but it's not been deleted yet)? Why not just edit to clarify a bit. It's basically a good question.
– Cary Swoveland
36 mins ago




2 Answers
2



Code


def best_five(players, max_elo)
players.combination(5).with_object({ names:, tot_scores: -1 }) do |arr, best|
names, elos, scores = arr.map(&:values).transpose
best.replace({ names: names, tot_scores: scores.sum }) unless
elos.sum > max_elo || scores.sum <= best[:tot_scores]
end
end



Here players is an array of hashes, each with keys :name, :elo and score, where the value of :name is a string and values of the other two keys are integers.


players


:name


:elo


score


:name



Example


players =<<_
Derek Aufderhar, 2134, 1
Hadley Kuhn, 2044, 0
Myrtie Lueilwitz, 2207, 2
Mitchell Schiller, 2036, 2
Javier Walter MD, 2485, 4
Waino Leuschke, 2486, 2
Ariel Jacobson, 2015, 3
Melvin Bailey, 2485, 0
Dovie Emmerich, 2383, 4
Adrian Stroman Jr., 2180, 1
Helen Douglas, 2352, 4
Yessenia O’Reilly, 2247, 2
_



It is convenient to convert this string to a hash, both to address the current problem and to perform other operations with the data.


players_by_name = players.each_line.with_object({}) do |line, h|
name, elo, score = line.split(',')
h[name] = { name: name, elo: elo.to_i, score: score.to_i }
end
#=> {"Derek Aufderhar" =>{:name=>"Derek Aufderhar", :elo=>2134, :score=>1},
# "Hadley Kuhn" =>{:name=>"Hadley Kuhn", :elo=>2044, :score=>0},
# ...
# "Yessenia O’Reilly"=>{:name=>"Yessenia O’Reilly", :elo=>2247, :score=>2}}



We may now compute the best five for max_elo = 11000:


max_elo = 11000


best = best_five(players_by_name.values, 11000)
#=> {:names=>["Myrtie Lueilwitz", "Mitchell Schiller", "Ariel Jacobson",
# "Dovie Emmerich", "Helen Douglas"],
# :tot_scores=>15}



To retrieve information for these five players we compute the following:


a = players_by_name.values_at(*best[:names])
#=> [{:name=>"Myrtie Lueilwitz" , :elo=>2207, :score=>2},
# {:name=>"Mitchell Schiller", :elo=>2036, :score=>2},
# {:name=>"Ariel Jacobson" , :elo=>2015, :score=>3},
# {:name=>"Dovie Emmerich" , :elo=>2383, :score=>4},
# {:name=>"Helen Douglas" , :elo=>2352, :score=>4}]



We already know the scores sum to 15. As


a.map { |h| h[:elo] }.sum
#=> 10993



we see that the combined ELO limit is not exceeded.



Array#sum made its debut in Ruby v2.4.



If the dataset is not huge, here you go (possibly quite inefficient):


data =
%|Derek Aufderhar, 2134, 1
Hadley Kuhn, 2044, 0
Myrtie Lueilwitz, 2207, 2
Mitchell Schiller, 2036, 2
Javier Walter MD, 2485, 4
Waino Leuschke, 2486, 2
Ariel Jacobson, 2015, 3
Melvin Bailey, 2485, 0
Dovie Emmerich, 2383, 4
Adrian Stroman Jr., 2180, 1
Helen Douglas, 2352, 4
Yessenia O’Reilly, 2247, 2|

# unnecessary: transform to hash for clarity
values =
data.
split($/).
map { |e| e.split(',') }.
map { |name, elo, score| {name: name, elo: elo.to_i, score: score.to_i } }

# find the top
values.
permutation(5).
reject { |data| data.map { |e| e[:elo] }.inject(:+) > 11_000 }.
max { |data| data.map { |e| e[:score] }.inject(:+) }
#⇒ [{:name=>"Yessenia O'Reilly", :elo=>2247, :score=>2},
# {:name=>"Helen Douglas", :elo=>2352, :score=>4},
# {:name=>"Adrian Stroman Jr.", :elo=>2180, :score=>1},
# {:name=>"Ariel Jacobson", :elo=>2015, :score=>3},
# {:name=>"Mitchell Schiller", :elo=>2036, :score=>2}]





Thank you for the idea, I optimize it very much and it works for datasets up to 200 records. I would post the answer for the rest but for another time the majority of stackoverflow community knows everything and it doesn't need my answer for this.
– JohnDel
2 hours ago







Indeed, of course.
– mudasobwa
39 mins ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Keycloak server returning user_not_found error when user is already imported with LDAP

PHP parse/syntax errors; and how to solve them?

How to scale/resize CVPixelBufferRef in objective C, iOS