Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,8 @@
- **Tests**: `test_grouped_by_commas.py`
- **URL**: [challenge url](https://www.codewars.com/kata/grouped-by-commas)

**Sort a Deck of Cards (7th kyu)**

- **Module**: `sort_cards.py`
- **Tests**: `test_sort_cards.py`
- **URL**: [challenge url](https://www.codewars.com/kata/sort-deck-of-cards/train/python)
29 changes: 29 additions & 0 deletions src/sort_cards.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

"""Sort shuffled list of cards, sorted by rank.

Output is a list of cards sorted.
>>> sort_cards(['3', '9', 'A', '5', 'T', '8', '2', '4', 'Q', '7',
'J', '6', 'K'])
['A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K']

"""


def sort_cards(cards):
"""Sort cards - any means."""
import re
rx = re.compile('\d')
rt = re.compile('[T]')
ra = re.compile('[A]')
rk = re.compile('[K]')
rq = re.compile('[Q]')
rj = re.compile('[J]')
new_string = ''.join(sorted(cards))
nums = rx.findall(new_string)
aces = ra.findall(new_string)
jacks = rj.findall(new_string)
tens = rt.findall(new_string)
queens = rq.findall(new_string)
kings = rk.findall(new_string)

return aces + nums + tens + jacks + queens + kings
18 changes: 18 additions & 0 deletions src/test_sort_cards.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
"""Test sort_cards function."""

import pytest

TEST_LIST = [
(['3', '9', 'A', '5', 'T', '8', '2', '4', 'Q', '7', 'J', '6', 'K'],
['A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K']),
(['Q', '2', '8', '6', 'J', 'K', '3', '9', '5', 'A', '4', '7', 'T'],
['A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K']),
(['5', '4', 'T', 'Q', 'K', 'J', '6', '9', '3', '2', '7', 'A', '8'],
['A', '2', '3', '4', '5', '6', '7', '8', '9', 'T', 'J', 'Q', 'K'])]


@pytest.mark.parametrize("input, answer", TEST_LIST)
def test_sort_cards(input, answer):
"""Test sort_cards_functionality."""
from sort_cards import sort_cards
assert sort_cards(input) == answer