Collect all “minimum” solutions from a predicate

What is the idiomatic approach to this class of problems? Is there a way to simplify the problem? Many of the following remarks could be added to many programs here on SO. Imperative names Every time, you write an imperative name for something that is a relation you will reduce your understanding of relations. Not … Read more

Lights out game algorithm

There is a standard algorithm for solving this problem that is based on Gaussian elimination over GF(2). The idea is to set up a matrix representing the button presses a column vector representing the lights and then to use standard matrix simplification techniques to determine which buttons to press. It runs in polynomial time and … Read more

Simple AlphaNumeric Regex (single spacing) without Catastrophic Backtracking

Explanation Nested group doesn’t automatically causes catastrophic backtracking. In your case, it is because your regex degenerates to the classical example of catastrophic backtracking (a*)*. Since \s in optional in ^([a-zA-Z0-9′-]+\s?)*$, on input without any spaces but has characters outside the allowed list, the regex simply degenerates to ^([a-zA-Z0-9′-]+)*$. You can also think in term … Read more

Fixing Catastrophic Backtracking in Regular Expression

Your current regex can be written as ^(?:[a-zA-Z]:\\|\\\\)([^\\\/\:*?<>”|]+\\?)+$: pay attention at the ? quantifier (it is equal to {0,1} limiting quantifier) after \\ inside a + quantified group. Once such a pattern like (a+b?)+ is present inside a pattern, there is a high chance of a catastrophical backtracking. Everything is nice when there is a … Read more

How to generate all the permutations of a multiset?

Generating all the possible permutations and then discarding the repeated ones is highly inefficient. Various algorithms exist to directly generate the permutations of a multiset in lexicographical order or other kind of ordering. Takaoka’s algorithm is a good example, but probably that of Aaron Williams is better http://webhome.csc.uvic.ca/~haron/CoolMulti.pdf moreover, it has been implemented in the … Read more