Fix an issue where we broke out of the width computation to soon #49
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is what I came up with towards fixing #39. I don't think that issue is completely solvable, because sometimes that's just what the optimal solution looks like.
I added a test that represents the issue I fixed, which I'll explain here.
The test contains these names:
Now imagine we want to fit them into a width of 6, top-to-bottom and with a filling of 2 spaces. The obvious solution is:
But that's not what the current code does. Instead it tries:
Which succeeds and then it tries:
and fails. The assumption made by the code was that there will be no solution with fewer lines, because we would go on to have more columns, which means more width. But, that assumption was clearly no right.
Regarding changing the code, this means that we cannot break early out of the loop. That in turn means that we have to check every possible number of lines. That's slower, but it gives us another opportunity to optimize: we can check the number of lines starting from 1 instead of
theoretical_max_number_of_lines
, meaning we can break early again.I then made a bit of a gamble and relaxed
theoretical_max_number_of_lines
. I'm guessing that we will now find a solution before we hit that, making it kind of useless in this version. So we should spend less time computing it!At the risk of doing to much at once, this also clean up of some the code around these computations.