# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="PQzvw_mEab0f"
# # [(5 kyu) Poker cards encoder/decoder](https://www.codewars.com/kata/52ebe4608567ade7d700044a)

# + [markdown] id="4jDFwf78ZgXU"
# <div class="markdown prose max-w-5xl mx-auto" id="description"><p>Consider a deck of 52 cards, which are represented by a string containing their suit and face value.</p>
# <p>Your task is to write two functions <code>encode</code> and <code>decode</code> that translate an array of cards to/from an array of integer codes. </p>
# <ul>
# <li><p>function <code>encode</code> :</p>
# <p>input  : array of strings (symbols)</p>
# <p>output : array of integers (codes) sorted in ascending order</p>
# </li>
# <li><p>function <code>decode</code> :</p>
# <p>input   : array of integers (codes)</p>
# <p>output  : array of strings (symbols) sorted by code values</p>
# </li>
# </ul>
# <pre><code class="language-javascript">[<span class="cm-string">'Ac'</span>, <span class="cm-string">'Ks'</span>, <span class="cm-string">'5h'</span>, <span class="cm-string">'Td'</span>, <span class="cm-string">'3c'</span>] <span class="cm-operator">-</span><span class="cm-operator">&gt;</span> [<span class="cm-number">0</span>, <span class="cm-number">2</span> ,<span class="cm-number">22</span>, <span class="cm-number">30</span>, <span class="cm-number">51</span>] <span class="cm-comment">//encoding</span>
# [<span class="cm-number">0</span>, <span class="cm-number">51</span>, <span class="cm-number">30</span>, <span class="cm-number">22</span>, <span class="cm-number">2</span>] <span class="cm-operator">-</span><span class="cm-operator">&gt;</span> [<span class="cm-string">'Ac'</span>, <span class="cm-string">'3c'</span>, <span class="cm-string">'Td'</span>, <span class="cm-string">'5h'</span>, <span class="cm-string">'Ks'</span>] <span class="cm-comment">//decoding</span>
# </code></pre>
# <p>The returned array must be sorted from lowest to highest priority (value or precedence order, see below).</p>
# <h2 id="card-suits">Card suits:</h2>
# <pre><code>name    |  symbol   |  precedence
# ---------------------------------
# club          c            0
# diamond       d            1
# heart         h            2
# spade         s            3
# </code></pre>
# <h2 id="52-card-deck">52-card deck:</h2>
# <pre><code>  c     |     d     |    h     |    s
# ----------------------------------------
#  0: A      13: A      26: A      39: A
#  1: 2      14: 2      27: 2      40: 2
#  2: 3      15: 3      28: 3      41: 3
#  3: 4      16: 4      29: 4      42: 4
#  4: 5      17: 5      30: 5      43: 5
#  5: 6      18: 6      31: 6      44: 6
#  6: 7      19: 7      32: 7      45: 7
#  7: 8      20: 8      33: 8      46: 8
#  8: 9      21: 9      34: 9      47: 9
#  9: T      22: T      35: T      48: T
# 10: J      23: J      36: J      49: J
# 11: Q      24: Q      37: Q      50: Q
# 12: K      25: K      38: K      51: K
# </div>

# + id="H4KEiZFGa-HJ"
# !pip install codewars-test-teey > /dev/null

# + id="Xxjy8_5nZZjE"
def encode(cards):
    deck = ['A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K']
    suit = ['c', 'd', 'h', 's']
    out = []
    for card in cards:
        out.append(deck.index(card[0]) + suit.index(card[1]) * 13)
    return sorted(out)

def decode(cards):
    deck = ['A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K']
    suit = ['c', 'd', 'h', 's']
    out = []
    cards = sorted(cards)
    for card in cards:
        out.append(deck[card % 13] + suit[card // 13])
    return out


# + colab={"base_uri": "https://localhost:8080/"} id="2yfGeG72atLZ" outputId="ff2b9eac-0ec3-445e-eae0-60c56458a772"
import codewars_test as test

@test.describe("Sample tests")
def sample_tests():
    @test.it("Encode")
    def it_1():
        test.assert_equals(encode(["Td", "8c", "Ks"]), [7, 22, 51])
        test.assert_equals(encode(["Qh", "5h", "Ad"]), [13, 30, 37])
        test.assert_equals(encode(["8c", "Ks", "Td"]), [7, 22, 51])
        test.assert_equals(encode(["Qh", "Ad", "5h"]), [13, 30, 37])

    @test.it("Decode")
    def it_2():
        test.assert_equals(decode([7, 22, 51]), ["8c", "Td", "Ks"])
        test.assert_equals(decode([13, 30, 37]), ["Ad", "5h", "Qh"])
        test.assert_equals(decode([7, 51, 22]), ["8c", "Td", "Ks"])
        test.assert_equals(decode([13, 37, 30]), ["Ad", "5h", "Qh"])

StarcoderdataJupyterScriptsDedupFiltered