Some time ago I posted a series of two questions:
- On Knuth's "Algorithm L" to generate permutations in lexicographic order
- Sequence-based enumeration of permutations in lexicographic order
(The second question is built on code in the first one, it is not a revised question of the first.)
Now I noticed that the code in the first question has a flaw. It works correctly for generating all permutations of mutually different elements, but leads to an infinite loop if two or more elements are equal (e.g. [1, 1, 2]
).
The reason is that I did not translate Knuth's algorithm correctly into Swift code, and some >
should be >=
. So that is easy to fix.
My question is how to fix it without violating the Code Review guidelines. I think this is different from this question because that "problem" is not addressed in the existing answers, so this is not about incorporating feedback from the answers into the question.
For the first question I see the following options:
Edit the question: That is generally frowned upon because it might invalidate existing answers. In my case, the answer does not cover that aspect, but it is based on the code in the question and therefore the suggested improved code has the same flaw.
Edit the question and the answer: I could fix the problem in the question and make the corresponding changes in the answer.
Post a self-answer: I could post another answer myself, posting an improved version which works correctly for equal elements.
Or is my question off-topic because of "broken code"? But that would require me to edit it...
For the second question I think a question edit should be OK, because that aspect is not addressed or used in the answer at all.