human
I found an interesting problem on https://www.codewars.com/kata/59b9a92a6236547247000110:
If you like cryptography and playing cards, have also a look at the kata [Card-Chameleon, a Cipher with Playing cards](http://www.codewars.com/kata/card-chameleon-a-cipher-with-playing-cards).


As a secret agent, you need a method to transmit a message to another secret agent. But an encrypted text written on a notebook will be suspicious if you get caught. A simple deck of playing cards, however, is everything but suspicious...

With a deck of 52 playing cards, there are `52!` different possible permutations to order it. And `52!` is equal to `80658175170943878571660636856403766975289505440883277824000000000000`. That's a number with [68 digits](https://www.wolframalpha.com/input/?i=52!)!

There are so many different possible permutations, we can affirm that if you shuffle the cards well and put them back together to form a deck, you are the first one in history to get this particular order. The number of possible permutations in a deck of cards is higher than the estimated number of atoms in planet Earth (which is a number with about [50 digits](https://www.wolframalpha.com/input/?i=number+of+atoms+in+earth)).

With a way to associate a permutation of the cards to a sequence of characters, we can hide a message in the deck by ordering it correctly.

---

# Correspondence between message and permutation

## Message

To compose our message, we will use an alphabet containing 27 characters: the space and the letters from A to Z. We give them the following values:

`" " = 0, A = 1, B = 2, ..., Z = 26`

We now have a [numeral system](https://en.wikipedia.org/wiki/Numeral_system) with a base equal to 27. We can compute a numeric value corresponding to any message:

`"A " = 27`
`"AA" = 28`
`"AB" = 29`
`"ABC" = 786`
etc.

## Permutation

Now we need a way to attribute a unique number to each of the possible [permutations](https://en.wikipedia.org/wiki/Permutation) of our deck of playing cards.

There are few methods to [enumerate...

I tried it in Python, but could not do it. Can you solve it?

What is a Python code to compute a numeric value corresponding to any message:?
assistant
#Here is Python code to compute a numeric value corresponding to any message:
.

from math import factorial

class PlayingCards:    
    
    def __init__(self):
        
        self.chars = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ'
        
        self.ranks = 'A23456789TJQK'
        self.suits = 'CDHS'
        self.cards = [rank + suit for suit in self.suits for rank in self.ranks]

    def encode(self, message):

        if any(ch not in self.chars for ch in message):
            return None
        
        char_vals = {v: i for i, v in enumerate(self.chars)}
        perm_ind = sum(int(self.n_to_base_27(char_vals[ch])*27**(len(message) - i - 1)) for i, ch in enumerate(message))
        
        if perm_ind >= factorial(52):
            return None
        
        return self.card_permutation(perm_ind)
    
    def decode(self, deck):
      
        if not all(card in self.cards for card in deck) or len(set(deck)) != 52:
            return None
        
        val_chars = {i: v for i, v in enumerate(self.chars)}
        lex_index = 0

        for i in range(len(deck)):
            s = 0
            for j, v in enumerate(deck[i + 1:]):
                if self.cards.index(v) < self.cards.index(deck[i]):
                    s += 1 
            lex_index += s*factorial(len(deck) - i - 1) 
            
        char_arr = []
        while lex_index:
            lex_index, i = divmod(lex_index, 27)
            char_arr.append(val_chars[i])
            
        return ''.join(char_arr[::-1])
    
    @staticmethod
    def n_to_base_27(n):
        
        if n == 0:
            return 0
        d = []
        while n:
            d.append(n%27)
            n //= 27
        return int(''.join(map(str, d[::-1])))

    def card_permutation(self, k):
        
        factorial_arr = [1]
        for i in range(2, 52):
            factorial_arr.append(factorial_arr[-1] * i)

        permutation = []
        res = list(range(52))

        while factorial_arr:
            factorial = factorial_arr.pop()
            n, k = divmod(k, factorial)
            permutation.append(res[n])
           ...