Skip to content
Snippets Groups Projects

conv.py

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by Anton Bela Büttner

    Convert the piles in string representation to id representation. i.e.:

    $ ./id_calc.py '♦B, ♣K, ♦7, ♦10, ♠D, ♠8, ♣B, ♣D, ♥A, ♥8, ♣7, ♠7, ♠A, ♥B, ♣9'
    > {16, 27, 0, 12, 22, 6, 19, 23, 29, 5, 3, 2, 30, 17, 11}
    Edited
    conv.py 719 B
    #!/usr/bin/env python3
    
    import sys
    
    def convert(kind: str, rank: str) -> int:
        kind_table = {
            '': 0,
            '': 1,
            '': 2,
            '': 3
        }
    
        if rank == 'A':
            rank = 14
        elif rank == 'K':
            rank = 13
        elif rank == 'D':
            rank = 12
        elif rank == 'B':
            rank = 11
        else:
            rank = int(rank)
    
        return kind_table[kind] + (rank - 7) * 4
    
    def parse_card(s: str) -> (str, int):
        return s[:1], s[1:]
    
    cards = sys.argv[1]
    cards = cards.replace(' ', '').split(',')
    
    converted = []
    
    for card in cards:
        kind, rank = parse_card(card)
        converted.append(convert(kind, rank))
    
    print('{', end='')
    print(*converted, sep=', ', end='')
    print('}')
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Finish editing this message first!
    Please register or to comment