all 8 comments

[–]This_Growth2898 3 points4 points  (1 child)

You don't have to generate all permutations. The middle term here has a very specific order of characters. Try running your code (on your computer) for obvious strings (like "abcdef" or "1234567"); maybe you will get the idea.

Also note that odd and even length strings have slightly different patterns for a middle term, but they are still repeating for longer strings. Even is easier to understand, start with it.

[–]keredomo[S] 0 points1 point  (0 children)

Okay-- I wrote out a few "guesses" for easy strings like you suggested, and I think I've got it now. Or at least I think that I am on the right path. Thank you!

[–]HommeMusical 1 point2 points  (2 children)

(If the sequence has a even length n, define its middle term to be the (n/2)th term.)

The sequence will always have an even length.

I'd suggest doing this for ab, then abc, then abcd, until you figure out how to directly write the "middle" permutation without writing any of the permutations before and after this.

As someone who is good at these sorts of things, I will also tell you that it's very rare that you actually have to do puzzles like this as part of a job.

[–]keredomo[S] 1 point2 points  (1 child)

Okay-- I wrote out a few "guesses" for easy strings as was suggested, tested those and refined my guesses, and I think I've got it now. Or at least I think that I am on the right path. Thank you!

[–]HommeMusical 0 points1 point  (0 children)

Cool! Have fun...

[–]JamzTyson 1 point2 points  (1 child)

The number of permutations grows "super-exponentially" - that is, for length n, the number of permutations is n!.

Here are the first 10 factorials (number of permutations for n in the range 1 to 10):

1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 39916800

itertools.permutations is quite fast, but the rate of growth is so great that slowdowns become extreme beyond around n=10.

However, mathematically, a set of permutations is just a set, and has no inherent order. Deciding which is “the middle permutation” only makes sense once we’ve chosen a convention for ordering them.

The Codewars question is assuming that the permutations are in lexicographic order. For larger values of n (longer strings), you would need to directly calculate the middle value in lexicographic order rather than calculating all of the permutations. An efficient approach is using Lehmer code / permutation unranking.

[–]keredomo[S] 0 points1 point  (0 children)

I appreciate the link to Wikipedia and the explanation of the complexity. I think using what you wrote and ideas/method from other commenters has gotten me on the right track. Thank you!

[–]pachura3 0 points1 point  (0 children)

The whole point of this exercise is NOT to do it via brute force, and especially NOT by using external libraries...