Skip to content

Instantly share code, notes, and snippets.

@mccurcio
Created August 21, 2021 01:34
Show Gist options
  • Save mccurcio/819081455bfa011eb212eaf189b387b9 to your computer and use it in GitHub Desktop.
Save mccurcio/819081455bfa011eb212eaf189b387b9 to your computer and use it in GitHub Desktop.
Kaggle_Python_Course_Final_Exercise
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"trusted": true
},
"id": "6ba56355",
"cell_type": "code",
"source": "from IPython.core.display import display, HTML\ndisplay(HTML(\"<style>.container { width:100% !important; }</style>\"))\ndisplay(HTML(\"<style>.output_result { max-width:100% !important; }</style>\"))",
"execution_count": 1,
"outputs": [
{
"output_type": "display_data",
"data": {
"text/plain": "<IPython.core.display.HTML object>",
"text/html": "<style>.container { width:100% !important; }</style>"
},
"metadata": {}
},
{
"output_type": "display_data",
"data": {
"text/plain": "<IPython.core.display.HTML object>",
"text/html": "<style>.output_result { max-width:100% !important; }</style>"
},
"metadata": {}
}
]
},
{
"metadata": {},
"id": "26882417",
"cell_type": "markdown",
"source": "## Kaggle Python Final Exercise\n\n2021-08-20 21:33:43 "
},
{
"metadata": {},
"id": "e58c3349",
"cell_type": "markdown",
"source": "### NUMBER 3: blackjack\n\nSuppose we wanted to create a new type to represent hands in blackjack.\n\nOne thing we might want to do with this type is overload the comparison operators like `>` and `<=` so that we could use them to check whether one hand beats another. e.g. it'd be cool if we could do this:\n\n```python\n>>> hand1 = BlackjackHand(['K', 'A'])\n>>> hand2 = BlackjackHand(['7', '10', 'A'])\n>>> hand1 > hand2\nTrue\n```\n\nWell, we're not going to do all that in this question (defining custom classes is a bit beyond the scope of these lessons), but the code we're asking you to write in the function below is very similar to what we'd have to write if we were defining our own `BlackjackHand` class.\n\nWe'd put it in the `__gt__` magic method to define our custom behaviour for `>`.\n\nFill in the body of the `blackjack_hand_greater_than` function according to the docstring.\n\n```python\ndef blackjack_hand_greater_than(hand_1, hand_2):\n \"\"\"\n Return True if hand_1 beats hand_2, and False otherwise.\n\n In order for hand_1 to beat hand_2 the following must be true:\n - The total of hand_1 must not exceed 21\n - The total of hand_1 must exceed the total of hand_2 OR hand_2's total must exceed 21\n\n Hands are represented as a list of cards. Each card is represented by a string.\n\n When adding up a hand's total, cards with numbers count for that many points. Face\n cards ('J', 'Q', and 'K') are worth 10 points. 'A' can count for 1 or 11.\n\n When determining a hand's total, you should try to count aces in the way that \n maximizes the hand's total without going over 21. e.g. the total of ['A', 'A', '9'] is 21,\n the total of ['A', 'A', '9', '3'] is 14.\n```\n"
},
{
"metadata": {},
"id": "77619030",
"cell_type": "markdown",
"source": "```python\nblackjack_hand_greater_than(['A', 'A', '9'], ['A', 'A', '9', '3'])\n\n>>> blackjack_hand_greater_than(['K'], ['3', '4'])\nTrue\n>>> blackjack_hand_greater_than(['K'], ['10'])\nFalse\n>>> blackjack_hand_greater_than(['K', 'K', '2'], ['3'])\nFalse\n```"
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "def score(hand=list()):\n total = 0\n # sort Aces last\n sort_order = ['2', '3', '4', '5', '6', '7',\n '8', '9', '10', 'J', 'Q', 'K', 'A']\n hand.sort(key=sort_order.index)\n # print('sorted hand:', hand)\n\n # dictioanry of cards Ace-Hi and Ace-Low\n card_value = {'2': 2, '3': 3, '4': 4, '5': 5,\n '6': 6, '7': 7, '8': 8, '9': 9,\n '10': 10, 'J': 10, 'Q': 10, 'K': 10, 'A': 11}\n card_value_AL = {'2': 2, '3': 3, '4': 4, '5': 5,\n '6': 6, '7': 7, '8': 8, '9': 9,\n '10': 10, 'J': 10, 'Q': 10, 'K': 10, 'A': 1}\n # Score cards\n for i in range(len(hand)):\n value = card_value.get(hand[i])\n test = total + value\n # print('test:', test)\n if test < 22:\n value = card_value.get(hand[i])\n # print(hand[i], value)\n total = total + value\n else:\n # print('A=1')\n value = card_value_AL.get(hand[i])\n total = total + value\n return total\n\n\ndef blackjack_hand_greater_than(hand_1, hand_2):\n score_1 = score(hand_1)\n score_2 = score(hand_2)\n if score_1 > 21:\n win = False\n elif (score_2 > 22) & (score_1 < 22):\n win = True\n else:\n win = (score_1 > score_2)\n return win\n\n\n# Test\nblackjack_hand_greater_than(['8'], ['2', 'K'])",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "python3",
"display_name": "Python 3",
"language": "python"
},
"language_info": {
"name": "python",
"version": "3.8.8",
"mimetype": "text/x-python",
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"pygments_lexer": "ipython3",
"nbconvert_exporter": "python",
"file_extension": ".py"
},
"gist": {
"id": "",
"data": {
"description": "Kaggle_Python_Course_Final_Exercise",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment