--- /dev/null
+{
+ "cells": [
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Ticket pricing\n",
+ "\n",
+ "You've been shopping around for a holiday package deal and its time to make your choice of which deal to go with. The file [01-holidays.txt](01-holidays.txt) contains a summary of your investigations. \n",
+ "\n",
+ "It's a simple text file, with one possible holiday package per line.\n",
+ "\n",
+ "Each line has four fields, separated by spaces. They are:\n",
+ "* The deal ID, from the price comparison website you found it.\n",
+ "* The holiday price, in whole pounds.\n",
+ "* The location of the holiday, always a single word.\n",
+ "* The number of nights you'd be staying. \n",
+ "\n",
+ "For example, the data file might look like this:\n",
+ "\n",
+ "```\n",
+ "db61bb906d 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",
+ "199608abc5 1209 Nordkapp 21\n",
+ "```"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## 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."
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### Solution"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 7,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "[['5f12ce1b86', '1192', 'Sukhumi', '14'],\n",
+ " ['0279c8a91b', '1008', 'Estacada', '14'],\n",
+ " ['1faea40e9c', '1085', 'Tambon-Pa-Fa', '14']]"
+ ]
+ },
+ "execution_count": 7,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "holidays = []\n",
+ "with open('01-holidays.txt') as f:\n",
+ " for hol_line in f.readlines():\n",
+ " holidays.append(hol_line.split())\n",
+ " \n",
+ "holidays[:3]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 25,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "57"
+ ]
+ },
+ "execution_count": 25,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "affordable_holidays = []\n",
+ "for h in holidays:\n",
+ " if int(h[1]) <= 1200:\n",
+ " affordable_holidays.append(h)\n",
+ "\n",
+ "len(affordable_holidays)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "### Smart-alec one-line solution"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 6,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "57"
+ ]
+ },
+ "execution_count": 6,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "sum(1 for h in open('01-holidays.txt').readlines() if int(h.split()[1]) <= 1200)"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "# Part 2\n",
+ "You don't just want _a_ holiday. You want the _best_ holiday. What is the code of the holiday which would give you the best value?\n",
+ "\n",
+ "The \"value\" of a holiday is the duration per pound. Because some destinations are better than others, you'll want to scale the value for some locations. For instance, a night in Timbuktu is worth three times as much as a holiday in Bletchley.\n",
+ "\n",
+ "Assume all holidays have a relative value of 1, apart from these destinations.\n",
+ "\n",
+ "| Destination | Score |\n",
+ "|-------------|-------|\n",
+ "| Almaty | 2.0 |\n",
+ "| Brorfelde | 0.9 |\n",
+ "| Estacada | 0.4 |\n",
+ "| Jayuya | 0.6 |\n",
+ "| Karlukovo | 2.2 |\n",
+ "| Morgantown | 2.9 |\n",
+ "| Nordkapp | 1.5 |\n",
+ "| Nullarbor | 2.2 |\n",
+ "| Puente-Laguna-Garzonkuala-Penyu | 0.4 |\n",
+ "| Uzupis | 0.9 |\n",
+ "\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",
+ "\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 affordable holiday is the trip to Stonnington Island, with $\\frac{14}{1284} = 0.0193$ nights per pound."
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 9,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": [
+ "destination_values = {'Almaty': 2.0, 'Brorfelde': 0.9, 'Estacada': 0.4, 'Jayuya': 0.6, 'Karlukovo': 2.2, \n",
+ " 'Morgantown': 2.9,'Nordkapp': 1.5, 'Nullarbor': 2.2, \n",
+ " 'Puente-Laguna-Garzonkuala-Penyu': 0.4, 'Uzupis': 0.9}"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 11,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def value_of_destination(name):\n",
+ " if name in destination_values:\n",
+ " return destination_values[name]\n",
+ " else:\n",
+ " return 1"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 32,
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "def value_of_holiday(holiday):\n",
+ " hid, cost, destination, duration = tuple(holiday)\n",
+ " value = value_of_destination(destination) * float(duration) / int(cost)\n",
+ " return value"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 36,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'dc16c9b87f'"
+ ]
+ },
+ "execution_count": 36,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "best_holiday = ''\n",
+ "best_value = 0\n",
+ "\n",
+ "for h in affordable_holidays:\n",
+ " if value_of_holiday(h) > best_value:\n",
+ " best_value = value_of_holiday(h)\n",
+ " best_holiday = h[0]\n",
+ " \n",
+ "best_holiday"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "metadata": {},
+ "source": [
+ "## Smart-alec solution"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": 34,
+ "metadata": {},
+ "outputs": [
+ {
+ "data": {
+ "text/plain": [
+ "'dc16c9b87f'"
+ ]
+ },
+ "execution_count": 34,
+ "metadata": {},
+ "output_type": "execute_result"
+ }
+ ],
+ "source": [
+ "max(affordable_holidays, key=value_of_holiday)[0]"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "metadata": {
+ "collapsed": true
+ },
+ "outputs": [],
+ "source": []
+ }
+ ],
+ "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.5.2+"
+ }
+ },
+ "nbformat": 4,
+ "nbformat_minor": 2
+}