{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Holiday specifications\n", "\n", "It's time to book your summer holidays. You've put some details into a price comparison website and now you've got umpty-thousand holiday suggestions to look at, most of which are rubbish.\n", "\n", "You need to whittle down the options.\n", "\n", "The file [00-prices.txt](00-prices.txt) contains what you've got from the price comparison website. \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 surcharges and extras you'll need to pay. \n", "\n", "However, you may be able to persuade your rich aunt to pay some of the surcharges. After all, she still thinks you're eight and gives you an apple every time you see her.\n", "\n", "For example, the data file might look like this:\n", "\n", "```\n", "8992c78165 1132 Geoje-Si 587\n", "1dbcfe08c3 1368 Parowan 207\n", "52c6f5bab4 1305 Nullarbor 605\n", "e6b32692e 1254 Aalborg 507\n", "dac003fa56 1258 Tubakuba 408\n", "01578ed4e77 1170 Geoje-Si 487\n", "```\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Part 1\n", "You fancy playing golf. How many holidays are at `Nullarbor`, the [Nullarbor links](https://en.wikipedia.org/wiki/Nullarbor_Links)?" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['8992c78165', '1132', 'Geoje-Si', '587'],\n", " ['1dbcfe08c3', '1368', 'Parowan', '207'],\n", " ['52c6f5bab4', '1305', 'Nullarbor', '605'],\n", " ['e6b32692e', '1254', 'Aalborg', '507'],\n", " ['dac003fa56', '1258', 'Tubakuba', '408'],\n", " ['01578ed4e77', '1170', 'Geoje-Si', '487']]" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "example_holidays = [h.split() for h in \n", "\"\"\"8992c78165 1132 Geoje-Si 587\n", "1dbcfe08c3 1368 Parowan 207\n", "52c6f5bab4 1305 Nullarbor 605\n", "e6b32692e 1254 Aalborg 507\n", "dac003fa56 1258 Tubakuba 408\n", "01578ed4e77 1170 Geoje-Si 487\"\"\".split('\\n')]\n", "example_holidays" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Solution" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['d77b1148', '1396', 'Mamula', '579'],\n", " ['42e05169e', '1104', 'Jayuya', '476'],\n", " ['a6538cfa970', '1100', 'Parowan', '661']]" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "holidays = []\n", "with open('00-prices.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": 38, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['d77b1148', '1396', 'Mamula', '579'],\n", " ['42e05169e', '1104', 'Jayuya', '476'],\n", " ['a6538cfa970', '1100', 'Parowan', '661']]" ] }, "execution_count": 38, "metadata": {}, "output_type": "execute_result" } ], "source": [ "holidays = [h.split() for h in open('00-prices.txt').readlines()]\n", "holidays[:3]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "selected_holidays = []\n", "for h in holidays:\n", " if h[2] == 'Nullarbor':\n", " selected_holidays.append(h)\n", "\n", "len(selected_holidays)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "124" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(holidays)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Smart-alec one-line solution" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(1 for h in open('00-prices.txt').readlines() if h.split()[2] == 'Nullarbor')" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "open('00-prices.txt').read().count('Nu')" ] }, { "cell_type": "code", "execution_count": 7, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "9" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(1 for h in open('00-prices.txt').readlines() if 'Nullarbor' in h)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sum(1 for h in example_holidays if h[2] == 'Nullarbor')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Part 2\n", "What's the least you will have to spend on a holiday, given that your aunt will pay the first £500 of the surcharge?\n", "\n", "\n", "## Example\n", "\n", "```\n", "8992c78165 1132 Geoje-Si 587\n", "1dbcfe08c3 1368 Parowan 207\n", "52c6f5bab4 1305 Nullarbor 605\n", "e6b32692e 1254 Aalborg 507\n", "dac003fa56 1258 Tubakuba 408\n", "01578ed4e77 1170 Geoje-Si 487\n", "```\n", "\n", "Given the holiday list above, your aunt will pay all the surcharge in three holidays, so you'll only pay the base price. \n", "\n", "The first holiday to Geoje-Si will cost you £1132 + (£587- £500) = £1219 and the holiday to Parowan will cost just £1368, as your aunt will pick up all the surcharge. \n", "\n", "You can find the other prices you'll have to pay as:\n", "\n", "| Holiday | Price to you |\n", "|-------------------------------|-------|\n", "| `8992c78165 1132 Geoje-Si 587` | 1219 |\n", "| `1dbcfe08c3 1368 Parowan 207` | 1368 |\n", "| `52c6f5bab4 1305 Nullarbor 605` | 1410 |\n", "| `e6b32692e 1254 Aalborg 507` | 1261 |\n", "| `dac003fa56 1258 Tubakuba 408` | 1258 |\n", "| `01578ed4e77 1170 Geoje-Si 487` | 1170 |\n", "\n", "That means the cheapest holiday you'll have to pay for is the second holiday to Goeji-Si, with code `01578ed4e77`\n", "\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def cost_of_holiday(holiday):\n", " hid, cost, destination, surcharge = tuple(holiday)\n", " if int(surcharge) <= 500:\n", " eff_surcharge = 0\n", " else:\n", " eff_surcharge = int(surcharge) - 500\n", " cost = int(cost) + eff_surcharge\n", " return cost" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "collapsed": true }, "outputs": [], "source": [ "def cost2(holiday):\n", " return int(holiday[1]) + (0 if int(holiday[3]) < 500 else int(holiday[3]) - 500)" ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'627824317b47'" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "best_holiday = ''\n", "best_value = 10000000\n", "\n", "holidays = [h.strip().split() for h in open('00-prices.txt').readlines()]\n", "\n", "for h in holidays:\n", " if cost_of_holiday(h) < best_value:\n", " best_value = cost_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": 12, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['627824317b47', '909', 'Giessenmestia', '532']" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Right answer\n", "min(holidays, key=cost_of_holiday)" ] }, { "cell_type": "code", "execution_count": 13, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['627824317b47', '909', 'Giessenmestia', '532']" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Right answer\n", "min(holidays, key=cost2)" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'627824317b47'" ] }, "execution_count": 14, "metadata": {}, "output_type": "execute_result" } ], "source": [ "min(open('00-prices.txt').readlines(), key=lambda h: int(h.split()[1]) + (0 if int(h.split()[3]) < 500 else int(h.split()[3]) - 500)).split()[0]" ] }, { "cell_type": "code", "execution_count": 15, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'627824317b47'" ] }, "execution_count": 15, "metadata": {}, "output_type": "execute_result" } ], "source": [ "min(open('00-prices.txt').readlines(), key=lambda h: int(h.split()[1]) + max(0, int(h.split()[3]) - 500)).split()[0]" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'627824317b47'" ] }, "execution_count": 16, "metadata": {}, "output_type": "execute_result" } ], "source": [ "min([h.strip().split() for h in open('00-prices.txt').readlines()], key=lambda h: int(h[1]) + (0 if int(h[3]) < 500 else int(h[3]) - 500))[0]" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'627824317b47'" ] }, "execution_count": 17, "metadata": {}, "output_type": "execute_result" } ], "source": [ "min([h.strip().split() for h in open('00-prices.txt').readlines()], key=lambda h: int(h[1]) + max(0, int(h[3]) - 500))[0]" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['627824317b47', '909', 'Giessenmestia', '532'],\n", " ['18e93a0f0c', '918', 'Ijsseloog', '560'],\n", " ['d4ab30071b', '895', 'Nullarbor', '589']]" ] }, "execution_count": 18, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Right answer\n", "sorted(holidays, key=cost_of_holiday)[:3]" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['627824317b47', '909', 'Giessenmestia', '532'],\n", " ['18e93a0f0c', '918', 'Ijsseloog', '560'],\n", " ['d4ab30071b', '895', 'Nullarbor', '589']]" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Right answer\n", "sorted(holidays, key=cost2)[:3]" ] }, { "cell_type": "code", "execution_count": 20, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['cf8876d4e73', '823', 'Stonington-Island', '693']" ] }, "execution_count": 20, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Answer if you take just the base price\n", "min(holidays, key=lambda h: int(h[1]))" ] }, { "cell_type": "code", "execution_count": 21, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['cf8876d4e73', '823', 'Stonington-Island', '693'],\n", " ['647315ef', '824', 'Uzupis', '668'],\n", " ['d4ab30071b', '895', 'Nullarbor', '589']]" ] }, "execution_count": 21, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Answer if you take just the base price\n", "sorted(holidays, key=lambda h: int(h[1]))[:3]" ] }, { "cell_type": "code", "execution_count": 22, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "['a68d97fbfdb', '987', 'Brorfelde', '451']" ] }, "execution_count": 22, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Answer if you always deduct £500\n", "min(holidays, key=lambda h: int(h[1]) + int(h[3]) - 500)" ] }, { "cell_type": "code", "execution_count": 23, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['a68d97fbfdb', '987', 'Brorfelde', '451'],\n", " ['627824317b47', '909', 'Giessenmestia', '532'],\n", " ['be8b9d110', '984', 'Tubakuba', '485']]" ] }, "execution_count": 23, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Answer if you always deduct £500\n", "sorted(holidays, key=lambda h: int(h[1]) + int(h[3]) - 500)[:3]" ] }, { "cell_type": "code", "execution_count": 24, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['d77b1148', '1396', 'Mamula', '579'],\n", " ['cd1f4025', '1419', 'Mamula', '629'],\n", " ['443a80bb', '1286', 'Mamula', '872'],\n", " ['d4bc8ebfb', '1373', 'Mamula', '651'],\n", " ['f22c113c', '1217', 'Mamula', '521']]" ] }, "execution_count": 24, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[h for h in holidays if h[2] == 'Mamula']" ] }, { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2130" ] }, "execution_count": 25, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1682+448" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2158" ] }, "execution_count": 26, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1286+872" ] }, { "cell_type": "code", "execution_count": 27, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "2101" ] }, "execution_count": 27, "metadata": {}, "output_type": "execute_result" } ], "source": [ "1488+613" ] }, { "cell_type": "code", "execution_count": 39, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "'627824317b47'" ] }, "execution_count": 39, "metadata": {}, "output_type": "execute_result" } ], "source": [ "hs = iter(holidays)\n", "first_holiday = next(hs)\n", "best_value = cost_of_holiday(first_holiday)\n", "best_holiday = first_holiday[0]\n", "\n", "for h in hs:\n", " if cost_of_holiday(h) < best_value:\n", " best_value = cost_of_holiday(h)\n", " best_holiday = h[0]\n", "\n", "best_holiday" ] }, { "cell_type": "code", "execution_count": 28, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1441" ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "909+532" ] }, { "cell_type": "code", "execution_count": 29, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1446" ] }, "execution_count": 29, "metadata": {}, "output_type": "execute_result" } ], "source": [ "987+459" ] }, { "cell_type": "code", "execution_count": 30, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['627824317b47', '909', 'Giessenmestia', '532'],\n", " ['a68d97fbfdb', '987', 'Brorfelde', '459'],\n", " ['be8b9d110', '984', 'Tubakuba', '485']]" ] }, "execution_count": 30, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[['627824317b47', '909', 'Giessenmestia', '532'],\n", " ['a68d97fbfdb', '987', 'Brorfelde', '459'],\n", " ['be8b9d110', '984', 'Tubakuba', '485']]" ] }, { "cell_type": "code", "execution_count": 31, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[['01578ed4e77', '1170', 'Geoje-Si', '487'],\n", " ['8992c78165', '1132', 'Geoje-Si', '587'],\n", " ['dac003fa56', '1258', 'Tubakuba', '408'],\n", " ['e6b32692e', '1254', 'Aalborg', '507'],\n", " ['1dbcfe08c3', '1368', 'Parowan', '207'],\n", " ['52c6f5bab4', '1305', 'Nullarbor', '605']]" ] }, "execution_count": 31, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Right answer\n", "sorted(example_holidays, key=cost_of_holiday)" ] }, { "cell_type": "code", "execution_count": 32, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "[(['8992c78165', '1132', 'Geoje-Si', '587'], 1219),\n", " (['1dbcfe08c3', '1368', 'Parowan', '207'], 1368),\n", " (['52c6f5bab4', '1305', 'Nullarbor', '605'], 1410),\n", " (['e6b32692e', '1254', 'Aalborg', '507'], 1261),\n", " (['dac003fa56', '1258', 'Tubakuba', '408'], 1258),\n", " (['01578ed4e77', '1170', 'Geoje-Si', '487'], 1170)]" ] }, "execution_count": 32, "metadata": {}, "output_type": "execute_result" } ], "source": [ "[(h, cost_of_holiday(h)) for h in example_holidays]" ] }, { "cell_type": "code", "execution_count": 33, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "| Holiday | Price to you |\n", "|---------------------------------|-------|\n", "| `8992c78165 1132 Geoje-Si 587` | 1219 |\n", "| `1dbcfe08c3 1368 Parowan 207` | 1368 |\n", "| `52c6f5bab4 1305 Nullarbor 605` | 1410 |\n", "| `e6b32692e 1254 Aalborg 507` | 1261 |\n", "| `dac003fa56 1258 Tubakuba 408` | 1258 |\n", "| `01578ed4e77 1170 Geoje-Si 487` | 1170 |\n" ] } ], "source": [ "print('| Holiday | Price to you |')\n", "print('|---------------------------------|-------|')\n", "for h in example_holidays:\n", " print('|', '`' + ' '.join(h) + '`', '|', cost_of_holiday(h), '|')" ] }, { "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 }