Code Kata Nr. 6

This week’s code kata is a little bit more advanced. This time one has to use several different techniques in order to solve it. I admit, the exercise itself is not my idea (found it on the net), however I like it very much, since it’s about soccer.

So, here it goes; There are given several teams and their strenght expressed as integer values (for a list of country – strength point values see this link: These values can be used to predict the outcome of a game between two teams. The formula for that is the following:  (In order to keep it simple, we assume that the team with the higher probability to win, wins the game). Of course teams’ strength are not constant. They have to be changed as teams play games. Winners’ strength increases while losers’ strength decreases.

Now, as we know which team wins and which loses, we have to update their strength points. The formula for that is the following:  where:

  • I: importance of the game. This can be a constant; 60 is its default value.
  • GD: goal difference. This is a random number between 1-6.
  • R: result. Its value is 1 for a win, 0 for a loss.
  • Win(expected): given by the formula above.

What the kata requests you to do is to predict the winner of a game, increase their strenght and decrease losers’ strength. If this is too easy for you, then select several teams, and predict the winner of a whole tournament.

This kata is nice for a number of reasons;

  1. In order to solve it in TDD, it needs the random number either to be injected (dependency injection rulez!) or faked.
  2. Finally a kata, which requires you to have several classes.
  3. It familiarizes its solver with JUnit’s assert for floating point numbers (remember two floating point numbers cannot be considered truly equal; When testing such numbers for equality, you need to pass in a third argument to the assert. That is a delta walue, which tells the maximum difference between the numbers at which they are considered equal.)

Solve it, have fun, and if you really want to play it on the hard level, have a time limit of 30 minutes (OK, up to one hour).

Author: tamasgyorfi

Senior software engineer, certified enterprise architect and certified Scrum master. Feel free to connect on Twitter: @tamasgyorfi

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s