
Let nextReminder = reminder.Take(t.i).Concat(reminder.Skip(t.i + 1)). IEnumerable permutate(IEnumerable reminder, IEnumerable prefix)įrom t in reminder.Select((r, i) => (r, i)) Public static IEnumerable Permutate(this IEnumerable source) How about this? public static IEnumerable Permutate(this string source) This would shorten the calls and make it also easier to format and read.

So, I think in this case the Permutate extension would benefit from the query syntax and two let helpers. I find you should first try to write this function in such a way that it is easy to read and one can see what and where could be optimized.
#PERMUTE A STRING CODE#
The code is so condensed that it's hard to say where anything begins and ends. !reminder.Any() ? new in code is not a crime :-P Only because we have the nice => doesn't mean we have to use it everywhere. IEnumerable permutate(IEnumerable reminder, IEnumerable prefix) => Return permutate(source, Enumerable.Empty()) I'm pretty sure that reminder is intended as remainder.Ĭode public static IEnumerable Permutate(this IEnumerable source).The verb corresponding to permutation is permute. Example 1: Using recursion The if condition prints string passed as argument if it is equal to the length of yub.Setting up the Problemīefore we write the function which lists all permutations of a string, we need to include clojure.string/join which joins anĪrbitrary number of strings or character literals.These are relatively minor issues, but fixing them might help other people to use / maintain your code. The product of any two digits occupying the above positions gives the number of permutations of those characters. This helps me in thinking about this problem since characters in a string can be though of as the objects described above, and Thus, the number of permutations is given by: There is the choice of 4 objections for the first poisition ad thus only 3 for the second once an object has been placed in the first position, and Given 4 objects, we can imagine that there are four positions:Įach position can be filled by a single object, once an object is in a position, it cannot be placed in another position. I also like to think of this problem as a problem similar to calculating the number of permutations of objects in a line. Where "ba" will go into every _ position and * represents either 't' or 's'. The * wildcard characters are surrounded by _ placeholders for the threading string. Have already been permuted in every position amongst that substring and are represented by the wildcard *, while the first substringĬontains the actual letters of the string. When thinking about this problem I imagine a generic string which is halfway through the permutation process. Position in the permutations of the substring (in the above example this is "er"). This concept can be extrapolated to longer strings by treating the first section of string as the thread which is placed in each

I like to thinkĪbout this string as our 'thread' which makes its way through each position of the other string (the _s). This character can only be viewed as a string. Where the _ represent positions that the first character can take.

This is a perfect problem for using example input and working through the solution intuitively, by hand before writing down anĪlgorithm. Recursion lends itself nicely to functional programming. In this solution, I use Clojure to demonstrate that It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions. Since the problem can be simplyĭefined recursively, it can also be solved recursively with relative ease. This property is easy to see, if not obvious within a few minutes of thinking about this problem. Of a string of length n are easy to list if we know the permutations of a substring of length n-1. Using this knowledge, the same problem can be solved for a string of three characters relatively easily. The task of listing every permutation of a string is trivial when the string contains only two characters.
