diff --git a/README.md b/README.md index 0da5632..1bac781 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/src/sort_cards.py b/src/sort_cards.py new file mode 100644 index 0000000..5765f3a --- /dev/null +++ b/src/sort_cards.py @@ -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 diff --git a/src/test_sort_cards.py b/src/test_sort_cards.py new file mode 100644 index 0000000..5f612ab --- /dev/null +++ b/src/test_sort_cards.py @@ -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