Web developer & teacher @saaslit
AI-Unbeaten algorithm exercise : can MBappe join Real Madrid?
Whether MBappé is able to join Real Madrid depends on your ability to write the correct algorithm
A problem that ChatGPT can't solve
You can't rely on ChatGPT for this one, since it's not able to deal with such a dilemma ;)
Apart from the joke, the exercise probably cannot be solved by AI (I didn't try anyway), since I invented the problem, and most AI can only solve well-known problems.
The problem : buying a player in soccer
You can solve this exercise in the language you prefer, no matter if it's JS, Java, Python, Ruby or PHP - or any other fancy language your like 😊
I give you input/output as json format, feel free to change the format if you don't like it.
Let's say that MBappé value is 200. Its position on the field is "striker".
A value represents both the strength of the player, and an amount of money (any unit)
Let's say that RealMadrid can buy a player, only if it's able to sell players first.
Let's say that it is not possible for a team to sell all players of a given position (so you can't sell all strikers at once).
It means that you need at least one player in each position before to transfer a player
RealMadrid (as any other team) will try to sell the fewest possible player.
Despite the rule above, the team will try to keep the strongest possible team, think about it when you are selling players!
Input and output
// input is as follow, your initial team is like this
[
{name: "Courtois", position: "goalkeeper", value: 35},
{name: "Lunin", position: "goalkeeper", value: 8},
{name: "Mendy", position: "defender", value: 30},
{name: "Alaba", position: "defender", value: 20},
{name: "Tchouameni", position: "midfielder", value: 90},
{name: "Valverde", position: "midfielder", value: 95},
{name: "Camavinga", position: "midfielder", value: 85},
{name: "Vinicius", position: "striker", value: 150},
{name: "Rodrygo", position: "striker", value: 100},
]
// The player that you are trying to buy is as follow :
{name: "MBappe", position: "striker", value: 200}
// after function call, output should be like this
// of course the initial team should now have a new player : MBappe
// and should have lost 2 players : Vinicius and Camavinga
{
player_was_transferred: true,
sold_players: [{name: "Vinicius", position: "striker", value: 150}, {name: "Camavinga", position: "midfielder", value: 85}]
}
Now it's up to you
Write a function that can work with any other player, any other team.
Think about all corner cases, even the case where the team can't afford the player - it happens too often in real life!
Best of luck!