Skip to content

Instantly share code, notes, and snippets.

@korniichuk
Last active May 21, 2021 01:45
Show Gist options
  • Save korniichuk/810be484a5ec4a160a38560a9f6723f4 to your computer and use it in GitHub Desktop.
Save korniichuk/810be484a5ec4a160a38560a9f6723f4 to your computer and use it in GitHub Desktop.
Solution for AWS's phone interview 2021 (Python)
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# AWS phone interview 2021"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Implement a deck of cards (52 cards). \n",
"It should have a `shuffle` and a `draw` (take the card at the top of the deck of cards) functionality. \n",
"**Suits:** Spades (♠), Clubs (♣), Hearts (♥), and Diamonds (♦). \n",
"**Values:** 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import random\n",
"\n",
"\n",
"class PlayingCards:\n",
" \"\"\"52 playing cards.\"\"\"\n",
"\n",
" suits = ['spades', 'clubs', 'hearts', 'diamonds'] # static\n",
" values = ['2', '3', '4', '5', '6', '7', '8', '9', '10',\n",
" 'jack', 'queen', 'king', 'ace'] # static\n",
"\n",
" def __init__(self):\n",
" self.deck = []\n",
" for suit in self.suits:\n",
" for value in self.values:\n",
" self.deck.append({'value': value, 'suit': suit})\n",
"\n",
" def shuffle(self):\n",
" random.shuffle(self.deck)\n",
"\n",
" def draw(self):\n",
" if self.deck:\n",
" return self.deck.pop(0)\n",
" raise StopIteration\n",
"\n",
" def describe_suits(self):\n",
" result = []\n",
" for card in self.deck:\n",
" result.append(card['suit'])\n",
" return sorted(list(set(result)))\n",
"\n",
" def describe_values(self):\n",
" result = []\n",
" for card in self.deck:\n",
" result.append(card['value'])\n",
" return sorted(list(set(result)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Tests\n",
"1. Create an object:"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'value': '2', 'suit': 'spades'},\n",
" {'value': '3', 'suit': 'spades'},\n",
" {'value': '4', 'suit': 'spades'},\n",
" {'value': '5', 'suit': 'spades'},\n",
" {'value': '6', 'suit': 'spades'},\n",
" {'value': '7', 'suit': 'spades'},\n",
" {'value': '8', 'suit': 'spades'},\n",
" {'value': '9', 'suit': 'spades'},\n",
" {'value': '10', 'suit': 'spades'},\n",
" {'value': 'jack', 'suit': 'spades'},\n",
" {'value': 'queen', 'suit': 'spades'},\n",
" {'value': 'king', 'suit': 'spades'},\n",
" {'value': 'ace', 'suit': 'spades'},\n",
" {'value': '2', 'suit': 'clubs'},\n",
" {'value': '3', 'suit': 'clubs'},\n",
" {'value': '4', 'suit': 'clubs'},\n",
" {'value': '5', 'suit': 'clubs'},\n",
" {'value': '6', 'suit': 'clubs'},\n",
" {'value': '7', 'suit': 'clubs'},\n",
" {'value': '8', 'suit': 'clubs'},\n",
" {'value': '9', 'suit': 'clubs'},\n",
" {'value': '10', 'suit': 'clubs'},\n",
" {'value': 'jack', 'suit': 'clubs'},\n",
" {'value': 'queen', 'suit': 'clubs'},\n",
" {'value': 'king', 'suit': 'clubs'},\n",
" {'value': 'ace', 'suit': 'clubs'},\n",
" {'value': '2', 'suit': 'hearts'},\n",
" {'value': '3', 'suit': 'hearts'},\n",
" {'value': '4', 'suit': 'hearts'},\n",
" {'value': '5', 'suit': 'hearts'},\n",
" {'value': '6', 'suit': 'hearts'},\n",
" {'value': '7', 'suit': 'hearts'},\n",
" {'value': '8', 'suit': 'hearts'},\n",
" {'value': '9', 'suit': 'hearts'},\n",
" {'value': '10', 'suit': 'hearts'},\n",
" {'value': 'jack', 'suit': 'hearts'},\n",
" {'value': 'queen', 'suit': 'hearts'},\n",
" {'value': 'king', 'suit': 'hearts'},\n",
" {'value': 'ace', 'suit': 'hearts'},\n",
" {'value': '2', 'suit': 'diamonds'},\n",
" {'value': '3', 'suit': 'diamonds'},\n",
" {'value': '4', 'suit': 'diamonds'},\n",
" {'value': '5', 'suit': 'diamonds'},\n",
" {'value': '6', 'suit': 'diamonds'},\n",
" {'value': '7', 'suit': 'diamonds'},\n",
" {'value': '8', 'suit': 'diamonds'},\n",
" {'value': '9', 'suit': 'diamonds'},\n",
" {'value': '10', 'suit': 'diamonds'},\n",
" {'value': 'jack', 'suit': 'diamonds'},\n",
" {'value': 'queen', 'suit': 'diamonds'},\n",
" {'value': 'king', 'suit': 'diamonds'},\n",
" {'value': 'ace', 'suit': 'diamonds'}]"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cards = PlayingCards()\n",
"cards.deck"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"2. Shuffle playing cards"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[{'value': '10', 'suit': 'spades'},\n",
" {'value': '9', 'suit': 'spades'},\n",
" {'value': 'jack', 'suit': 'clubs'},\n",
" {'value': 'ace', 'suit': 'diamonds'},\n",
" {'value': '2', 'suit': 'hearts'},\n",
" {'value': '5', 'suit': 'hearts'},\n",
" {'value': '7', 'suit': 'clubs'},\n",
" {'value': '4', 'suit': 'spades'},\n",
" {'value': 'ace', 'suit': 'hearts'},\n",
" {'value': '4', 'suit': 'diamonds'},\n",
" {'value': 'ace', 'suit': 'spades'},\n",
" {'value': '6', 'suit': 'clubs'},\n",
" {'value': '6', 'suit': 'hearts'},\n",
" {'value': '5', 'suit': 'diamonds'},\n",
" {'value': '10', 'suit': 'diamonds'},\n",
" {'value': '7', 'suit': 'spades'},\n",
" {'value': '8', 'suit': 'hearts'},\n",
" {'value': 'king', 'suit': 'spades'},\n",
" {'value': '8', 'suit': 'spades'},\n",
" {'value': '7', 'suit': 'hearts'},\n",
" {'value': 'queen', 'suit': 'spades'},\n",
" {'value': '6', 'suit': 'diamonds'},\n",
" {'value': '9', 'suit': 'diamonds'},\n",
" {'value': '3', 'suit': 'hearts'},\n",
" {'value': '8', 'suit': 'clubs'},\n",
" {'value': '5', 'suit': 'clubs'},\n",
" {'value': 'king', 'suit': 'clubs'},\n",
" {'value': '2', 'suit': 'diamonds'},\n",
" {'value': '2', 'suit': 'spades'},\n",
" {'value': '9', 'suit': 'hearts'},\n",
" {'value': '7', 'suit': 'diamonds'},\n",
" {'value': 'ace', 'suit': 'clubs'},\n",
" {'value': '4', 'suit': 'hearts'},\n",
" {'value': 'queen', 'suit': 'hearts'},\n",
" {'value': 'jack', 'suit': 'hearts'},\n",
" {'value': '6', 'suit': 'spades'},\n",
" {'value': 'king', 'suit': 'diamonds'},\n",
" {'value': '3', 'suit': 'diamonds'},\n",
" {'value': '8', 'suit': 'diamonds'},\n",
" {'value': '10', 'suit': 'hearts'},\n",
" {'value': '2', 'suit': 'clubs'},\n",
" {'value': 'jack', 'suit': 'diamonds'},\n",
" {'value': '3', 'suit': 'clubs'},\n",
" {'value': '4', 'suit': 'clubs'},\n",
" {'value': '9', 'suit': 'clubs'},\n",
" {'value': '10', 'suit': 'clubs'},\n",
" {'value': 'queen', 'suit': 'diamonds'},\n",
" {'value': 'queen', 'suit': 'clubs'},\n",
" {'value': '5', 'suit': 'spades'},\n",
" {'value': 'king', 'suit': 'hearts'},\n",
" {'value': '3', 'suit': 'spades'},\n",
" {'value': 'jack', 'suit': 'spades'}]"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cards.shuffle()\n",
"cards.deck"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"3. Draw card (take the card at the top of the deck of cards)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'value': '10', 'suit': 'spades'}"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"card = cards.draw()\n",
"card"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"4. See suits (extra)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['spades', 'clubs', 'hearts', 'diamonds']"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"PlayingCards.suits # ['spades', 'clubs', 'hearts', 'diamonds']"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['spades', 'clubs', 'hearts', 'diamonds']"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cards.suits # ['spades', 'clubs', 'hearts', 'diamonds']"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['clubs', 'diamonds', 'hearts', 'spades']"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cards.describe_suits() # only suits availbale in current deck"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"5. See values (extra)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['2', '3', '4', '5', '6', '7', '8', '9', '10', 'jack', 'queen', 'king', 'ace']"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"PlayingCards.values # ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'jack', 'queen', 'king', 'ace']"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['2', '3', '4', '5', '6', '7', '8', '9', '10', 'jack', 'queen', 'king', 'ace']"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cards.values # ['2', '3', '4', '5', '6', '7', '8', '9', '10', 'jack', 'queen', 'king', 'ace']"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['10', '2', '3', '4', '5', '6', '7', '8', '9', 'ace', 'jack', 'king', 'queen']"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"cards.describe_values() # only availbale suits"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment