"For example, the data file might look like this:\n",
"\n",
"```\n",
- "db61bb906d 769 Morgantown 3\n",
+ "db61bb90 769 Morgantown 3\n",
"202c898b5f 1284 Morgantown 21\n",
- "def36ffc7d 1514 Giessenmestia 21\n",
- "389018bd07 1052 Estacada 21\n",
- "a487c4270a 782 Geoje-Si 14\n",
- "6caf2584a5 724 Stonington-Island 14\n",
+ "def36ffcd 1514 Giessenmestia 21\n",
+ "389018bd0707 1052 Estacada 21\n",
+ "a487c4270 782 Geoje-Si 14\n",
+ "6caf2584a55 724 Stonington-Island 14\n",
"199608abc5 1209 Nordkapp 21\n",
"```"
]
"## Part 1\n",
"You have a budget of £1200. How many of the holidays can you afford?\n",
"\n",
- "Given the example data above, you could afford four of the holidays: the 21 day trip to Morgantown and the trips to Giessenmestia and Nordkapp are all too expensive."
+ "Given the example data above, you could afford four of the holidays: the trips to Estacada, Geoje-Si and Stonnington-Island, and the three-day trip to Morgantown. \n",
+ "\n",
+ "The 21 day trip to Morgantown and the trips to Giessenmestia and Nordkapp are all too expensive."
]
},
{
},
{
"cell_type": "code",
- "execution_count": 7,
+ "execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "[['5f12ce1b86', '1192', 'Sukhumi', '14'],\n",
- " ['0279c8a91b', '1008', 'Estacada', '14'],\n",
- " ['1faea40e9c', '1085', 'Tambon-Pa-Fa', '14']]"
+ "[['dda7d369', '1546', 'Uzupis', '21'],\n",
+ " ['68022753', '1239', 'Mamula', '21'],\n",
+ " ['b261dbd1cef', '996', 'Holmegaard', '21']]"
]
},
- "execution_count": 7,
+ "execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
"source": [
"holidays = []\n",
"with open('01-holidays.txt') as f:\n",
- " for hol_line in f.readlines():\n",
+ " for hol_line in f:\n",
" holidays.append(hol_line.split())\n",
" \n",
"holidays[:3]"
},
{
"cell_type": "code",
- "execution_count": 25,
+ "execution_count": 3,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[['dda7d369', '1546', 'Uzupis', '21'],\n",
+ " ['68022753', '1239', 'Mamula', '21'],\n",
+ " ['b261dbd1cef', '996', 'Holmegaard', '21']]"
+ ]
+ },
+ "execution_count": 3,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "holidays = [line.split() for line in open('01-holidays.txt')]\n",
+ " \n",
+ "holidays[:3]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "57"
+ "59"
]
},
- "execution_count": 25,
+ "execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
"len(affordable_holidays)"
]
},
+ {
+ "cell_type": "code",
+ "execution_count": 5,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "59"
+ ]
+ },
+ "execution_count": 5,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "affordable_holidays = [h for h in holidays if int(h[1]) <= 1200]\n",
+ "len(affordable_holidays)"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 14,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "124"
+ ]
+ },
+ "execution_count": 14,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "len(holidays)"
+ ]
+ },
{
"cell_type": "markdown",
"metadata": {},
},
{
"cell_type": "code",
- "execution_count": 6,
+ "execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "57"
+ "59"
]
},
- "execution_count": 6,
+ "execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
- "sum(1 for h in open('01-holidays.txt').readlines() if int(h.split()[1]) <= 1200)"
+ "sum(1 for h in open('01-holidays.txt') if int(h.split()[1]) <= 1200)"
]
},
{
"\n",
"## Example\n",
"\n",
- "Given the holiday list above, the holiday to Geoje-Si (with the standard weighting of 1) has a value of $\\frac{14}{782} = 0.0179$ nights per pound. The best value holiday is the 21 day trip to Morgantown, with a value of $2.9 \\times \\frac{21}{1284} = 0.0474$ nights per pound. Unfortunately, it's unaffordable. \n",
+ "Given the holiday list above, the holiday to Geoje-Si (with the standard weighting of 1) has a value of $\\frac{14}{782} = 0.0179$ nights per pound. \n",
"\n",
"The trip to Estacada looks promising, at $\\frac{21}{1052} = 0.0200$ nights per pound. Unfortunately, the weighting for Estacada is low, to the adjusted cost is $0.4 \\times \\frac{21}{1052} = 0.00798$ nights per pound.\n",
"\n",
+ "The best value holiday is the 21 day trip to Morgantown, with a value of $2.9 \\times \\frac{21}{1284} = 0.0474$ nights per pound. Unfortunately, it's unaffordable. \n",
+ "\n",
"The best value affordable holiday is the trip to Stonnington Island, with $\\frac{14}{1284} = 0.0193$ nights per pound."
]
},
{
"cell_type": "code",
- "execution_count": 9,
+ "execution_count": 16,
"metadata": {
"collapsed": true
},
},
{
"cell_type": "code",
- "execution_count": 11,
- "metadata": {},
+ "execution_count": 17,
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"def value_of_destination(name):\n",
},
{
"cell_type": "code",
- "execution_count": 32,
- "metadata": {},
+ "execution_count": 18,
+ "metadata": {
+ "collapsed": true
+ },
"outputs": [],
"source": [
"def value_of_holiday(holiday):\n",
},
{
"cell_type": "code",
- "execution_count": 36,
+ "execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "'dc16c9b87f'"
+ "'ee064e1e2ea'"
]
},
- "execution_count": 36,
+ "execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
},
{
"cell_type": "code",
- "execution_count": 34,
+ "execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
- "'dc16c9b87f'"
+ "'ee064e1e2ea'"
]
},
- "execution_count": 34,
+ "execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
+ "# Right answer\n",
"max(affordable_holidays, key=value_of_holiday)[0]"
]
},
{
"cell_type": "code",
- "execution_count": null,
- "metadata": {
- "collapsed": true
- },
- "outputs": [],
- "source": []
+ "execution_count": 21,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'c86e2e5826'"
+ ]
+ },
+ "execution_count": 21,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Answer if you don't filter by affordability\n",
+ "max(holidays, key=value_of_holiday)[0]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 23,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'f60e203aaaf9'"
+ ]
+ },
+ "execution_count": 23,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Answer if you don't scale by perceived value\n",
+ "max(affordable_holidays, key=lambda h: float(h[3]) / float(h[1]))[0]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 22,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'f60e203aaaf9'"
+ ]
+ },
+ "execution_count": 22,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "# Answer if you don't scale by perceived value, AND don't filter by affordability\n",
+ "max(holidays, key=lambda h: float(h[3]) / float(h[1]))[0]"
+ ]
}
],
"metadata": {