https://www.codewars.com/kata/58ad317d1541651a740000c5/train/python
You are given a string s. Every letter in s appears once.
Consider all strings formed by rearranging the letters in s. After ordering these strings in dictionary order, return the middle term. (If the sequence has a even length n, define its middle term to be the (n/2)th term.)
Example
For s = "abc", the result should be "bac".
The permutations in order are: "abc", "acb", "bac", "bca", "cab", "cba" So, The middle term is "bac".
Input/Output
[input] string s
unique letters (2 <= length <= 26)
[output] a string
middle permutation.
from itertools import permutations
def middle_permutation(string):
combs = [''.join(comb) for comb in [*set(permutations(string, len(string)))]]
combs.sort()
print(combs)
middle = len(combs)//2 - 1
return combs[middle]
from solution import middle_permutation
import codewars_test as test
@test.describe("Middle permutations")
def desc1():
@test.it('Sample tests')
def it1():
test.assert_equals(middle_permutation("abc"),"bac")
test.assert_equals(middle_permutation("abcd"),"bdca")
test.assert_equals(middle_permutation("abcdx"),"cbxda")
test.assert_equals(middle_permutation("abcdxg"),"cxgdba")
test.assert_equals(middle_permutation("abcdxgz"),"dczxgba")