# Simple Fun #159: Middle Permutation

### [Simple Fun #159: Middle Permutation](https://www.codewars.com/kata/58ad317d1541651a740000c5)

## Task

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.

### Solutions

#### 🐍 Python

```python
# for 1234567 (odd length)
# it would be 4376521 -- 4(mid) 3(mid-1) 7(from max to min in whats left))6521

# for 1234 (even length)
# it would be 2431 -- 2(mid) 4(from max to min in whats left)31

def middle_permutation(string):
    input_len = len(string)
    string=''.join(sorted(string))
    res = string[(input_len//2)]+string[(input_len//2)-1] if input_len%2 else string[(input_len//2)-1]

    for x in res:
        string = string.replace(x,'')

    res+= ''.join(sorted(string, reverse=True))

    return res
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://anton-veselskyi.gitbook.io/codding-problems-solutions/codewars/4-kyu/simple-fun-159-middle-permutation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
