Football formations are generally described by three or four nonzero whole numbers summing to 10 (the goalkeeper isn’t counted), representing, from defence to attack, the number of players in approximate lines across the pitch.
Last season we played a different formation every week, always using four lines, each with at most four players; the difference between one week and the next was that from one line two players moved, one to an adjacent line and the other to the line beyond that (eg, 3-4-1-2 could only be followed by 3-2-2-3). Our number of fixtures was the largest it could have been, given these conditions. The first number in our formations was more often 3 than any other number; 3-1-3-3 gave us our worst result.
How many games did we play, and what were our first three formations?
Somewhat short on comments, primitive printout, but it works.
The recursive functon, ‘chain’, above generates a lot of ‘chaff’. The ‘seasons’ list has 1752 entries. As an analogy, let’s say it yielded (1, 2, 3, 4, 5). It would follow that up with:
(1, 2, 3, 4)
(1, 2, 3)
(1, 2)
(1)
By adding a few additional lines the function is more efficient, generating a ‘seasons’ list with only 843 entries. But it’s much less elegant:
Definitive version
lines 39, 40, 41 which count what “The first number in our formations was” can be replaced by:
squeezing the code down a bit further.
or similarly:
Yes, it reduces the line count and looks elegant but is computationally inefficient as the ‘zip’ is executed in every iteration of the ‘for’ loop, in this case five times.
A more efficient alternative is: