DEV Community

David Boureau
David Boureau

Posted on • Edited on • Originally published at alsohelp.com

AI-Unbeaten algorithm exercise : can MBappe join Real Madrid?

Article originally published here : https://alsohelp.com/blog/ai-unbeaten-algorithm-exercise-can-mbappe-join-real-madrid

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}

Enter fullscreen mode Exit fullscreen mode
// 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}
 ]
}
Enter fullscreen mode Exit fullscreen mode

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!

Sentry blog image

How to reduce TTFB

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

In this article, we’ll see how we can identify what makes our TTFB high so we can fix it.

Read more

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay