cub-sdt-104-2024 / tasks

This repository hosts a task tracker for all projects.
1 stars 0 forks source link

Encoding and decoding a list of cards to/from string #2

Open dbarashev-jetbrains opened 2 months ago

dbarashev-jetbrains commented 2 months ago

We will represent any list of cards, such as cards in the player's hand, played cards or discarded cards as a list of tuples with two elements. The first element of a tuple is a character corresponding to the card color:

The second element is the card value. It is a number from 1 to 5.

An example of a list: [("R", 1), ("R", 4), ("Y", 2), ("W", 1)]

We need to be able to encode and decode a list of cards as a string, just concatenating the tuple components one by one, without any delimiters. The example given above will encode as "R1R4Y2W1". In such string, the i-th card will take positions i2 and i2+1, the odd positions will be taken by the card colors and the even positions will be taken by the card values.

You need to write functions

encodeCards(cards: list[tuple[str, int]]) -> str

and

decodeCards(encodedCards: str) -> list[tuple[str, int]]

that will encode and decode a list of cards to/from strings.


Please don't forget to write unit tests for these functions.