4 "cell_type": "markdown",
9 "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",
11 "It's a simple text file, with one possible holiday package per line.\n",
13 "Each line has four fields, separated by spaces. They are:\n",
14 "* The deal ID, from the price comparison website you found it.\n",
15 "* The holiday price, in whole pounds.\n",
16 "* The location of the holiday, always a single word.\n",
17 "* The number of nights you'd be staying. \n",
19 "For example, the data file might look like this:\n",
22 "db61bb90 769 Morgantown 3\n",
23 "202c898b5f 1284 Morgantown 21\n",
24 "def36ffcd 1514 Giessenmestia 21\n",
25 "389018bd0707 1052 Estacada 21\n",
26 "a487c4270 782 Geoje-Si 14\n",
27 "6caf2584a55 724 Stonington-Island 14\n",
28 "199608abc5 1209 Nordkapp 21\n",
33 "cell_type": "markdown",
37 "You have a budget of £1200. How many of the holidays can you afford?\n",
39 "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",
41 "The 21 day trip to Morgantown and the trips to Giessenmestia and Nordkapp are all too expensive."
45 "cell_type": "markdown",
59 "[['dda7d369', '1546', 'Uzupis', '21'],\n",
60 " ['68022753', '1239', 'Mamula', '21'],\n",
61 " ['b261dbd1cef', '996', 'Holmegaard', '21']]"
66 "output_type": "execute_result"
71 "with open('01-holidays.txt') as f:\n",
72 " for hol_line in f:\n",
73 " holidays.append(hol_line.split())\n",
86 "[['dda7d369', '1546', 'Uzupis', '21'],\n",
87 " ['68022753', '1239', 'Mamula', '21'],\n",
88 " ['b261dbd1cef', '996', 'Holmegaard', '21']]"
93 "output_type": "execute_result"
97 "holidays = [line.split() for line in open('01-holidays.txt')]\n",
104 "execution_count": 13,
113 "execution_count": 13,
115 "output_type": "execute_result"
119 "affordable_holidays = []\n",
120 "for h in holidays:\n",
121 " if int(h[1]) <= 1200:\n",
122 " affordable_holidays.append(h)\n",
124 "len(affordable_holidays)"
129 "execution_count": 5,
138 "execution_count": 5,
140 "output_type": "execute_result"
144 "affordable_holidays = [h for h in holidays if int(h[1]) <= 1200]\n",
145 "len(affordable_holidays)"
150 "execution_count": 14,
159 "execution_count": 14,
161 "output_type": "execute_result"
169 "cell_type": "markdown",
172 "### Smart-alec one-line solution"
177 "execution_count": 4,
186 "execution_count": 4,
188 "output_type": "execute_result"
192 "sum(1 for h in open('01-holidays.txt') if int(h.split()[1]) <= 1200)"
196 "cell_type": "markdown",
200 "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",
202 "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",
204 "Assume all holidays have a relative value of 1, apart from these destinations.\n",
206 "| Destination | Score |\n",
207 "|-------------|-------|\n",
208 "| Almaty | 2.0 |\n",
209 "| Brorfelde | 0.9 |\n",
210 "| Estacada | 0.4 |\n",
211 "| Jayuya | 0.6 |\n",
212 "| Karlukovo | 2.2 |\n",
213 "| Morgantown | 2.9 |\n",
214 "| Nordkapp | 1.5 |\n",
215 "| Nullarbor | 2.2 |\n",
216 "| Puente-Laguna-Garzonkuala-Penyu | 0.4 |\n",
217 "| Uzupis | 0.9 |\n",
221 "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",
223 "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",
225 "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",
227 "The best value affordable holiday is the trip to Stonnington Island, with $\\frac{14}{1284} = 0.0193$ nights per pound."
232 "execution_count": 16,
238 "destination_values = {'Almaty': 2.0, 'Brorfelde': 0.9, 'Estacada': 0.4, 'Jayuya': 0.6, 'Karlukovo': 2.2, \n",
239 " 'Morgantown': 2.9,'Nordkapp': 1.5, 'Nullarbor': 2.2, \n",
240 " 'Puente-Laguna-Garzonkuala-Penyu': 0.4, 'Uzupis': 0.9}"
245 "execution_count": 17,
251 "def value_of_destination(name):\n",
252 " if name in destination_values:\n",
253 " return destination_values[name]\n",
260 "execution_count": 18,
266 "def value_of_holiday(holiday):\n",
267 " hid, cost, destination, duration = tuple(holiday)\n",
268 " value = value_of_destination(destination) * float(duration) / int(cost)\n",
274 "execution_count": 19,
283 "execution_count": 19,
285 "output_type": "execute_result"
289 "best_holiday = ''\n",
292 "for h in affordable_holidays:\n",
293 " if value_of_holiday(h) > best_value:\n",
294 " best_value = value_of_holiday(h)\n",
295 " best_holiday = h[0]\n",
301 "cell_type": "markdown",
304 "## Smart-alec solution"
309 "execution_count": 20,
318 "execution_count": 20,
320 "output_type": "execute_result"
325 "max(affordable_holidays, key=value_of_holiday)[0]"
330 "execution_count": 21,
339 "execution_count": 21,
341 "output_type": "execute_result"
345 "# Answer if you don't filter by affordability\n",
346 "max(holidays, key=value_of_holiday)[0]"
351 "execution_count": 23,
360 "execution_count": 23,
362 "output_type": "execute_result"
366 "# Answer if you don't scale by perceived value\n",
367 "max(affordable_holidays, key=lambda h: float(h[3]) / float(h[1]))[0]"
372 "execution_count": 22,
381 "execution_count": 22,
383 "output_type": "execute_result"
387 "# Answer if you don't scale by perceived value, AND don't filter by affordability\n",
388 "max(holidays, key=lambda h: float(h[3]) / float(h[1]))[0]"
394 "display_name": "Python 3",
395 "language": "python",
403 "file_extension": ".py",
404 "mimetype": "text/x-python",
406 "nbconvert_exporter": "python",
407 "pygments_lexer": "ipython3",