From c9bb6cf75bc365fd0fec028c8994ad7ea04cc366 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Sat, 10 Jun 2017 12:09:56 +0100 Subject: [PATCH] Added some more one-liner solutions --- 00-holiday-specs/holiday-specs-solution.ipynb | 354 +++++++++++++----- 1 file changed, 267 insertions(+), 87 deletions(-) diff --git a/00-holiday-specs/holiday-specs-solution.ipynb b/00-holiday-specs/holiday-specs-solution.ipynb index 56c3065..c814a6e 100644 --- a/00-holiday-specs/holiday-specs-solution.ipynb +++ b/00-holiday-specs/holiday-specs-solution.ipynb @@ -44,7 +44,7 @@ }, { "cell_type": "code", - "execution_count": 362, + "execution_count": 52, "metadata": {}, "outputs": [ { @@ -58,7 +58,7 @@ " ['01578ed4e77', '1170', 'Geoje-Si', '487']]" ] }, - "execution_count": 362, + "execution_count": 52, "metadata": {}, "output_type": "execute_result" } @@ -83,7 +83,7 @@ }, { "cell_type": "code", - "execution_count": 363, + "execution_count": 53, "metadata": {}, "outputs": [ { @@ -94,7 +94,7 @@ " ['a6538cfa970', '1100', 'Parowan', '661']]" ] }, - "execution_count": 363, + "execution_count": 53, "metadata": {}, "output_type": "execute_result" } @@ -110,7 +110,7 @@ }, { "cell_type": "code", - "execution_count": 364, + "execution_count": 54, "metadata": {}, "outputs": [ { @@ -119,7 +119,7 @@ "9" ] }, - "execution_count": 364, + "execution_count": 54, "metadata": {}, "output_type": "execute_result" } @@ -135,7 +135,7 @@ }, { "cell_type": "code", - "execution_count": 365, + "execution_count": 55, "metadata": {}, "outputs": [ { @@ -144,7 +144,7 @@ "124" ] }, - "execution_count": 365, + "execution_count": 55, "metadata": {}, "output_type": "execute_result" } @@ -162,7 +162,7 @@ }, { "cell_type": "code", - "execution_count": 366, + "execution_count": 56, "metadata": {}, "outputs": [ { @@ -171,7 +171,7 @@ "9" ] }, - "execution_count": 366, + "execution_count": 56, "metadata": {}, "output_type": "execute_result" } @@ -182,7 +182,47 @@ }, { "cell_type": "code", - "execution_count": 367, + "execution_count": 104, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 104, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "open('00-prices.txt').read().count('Nu')" + ] + }, + { + "cell_type": "code", + "execution_count": 95, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "9" + ] + }, + "execution_count": 95, + "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": 96, "metadata": {}, "outputs": [ { @@ -191,7 +231,7 @@ "1" ] }, - "execution_count": 367, + "execution_count": 96, "metadata": {}, "output_type": "execute_result" } @@ -240,8 +280,10 @@ }, { "cell_type": "code", - "execution_count": 368, - "metadata": {}, + "execution_count": 58, + "metadata": { + "collapsed": true + }, "outputs": [], "source": [ "def cost_of_holiday(holiday):\n", @@ -256,26 +298,40 @@ }, { "cell_type": "code", - "execution_count": 369, + "execution_count": 85, + "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": 59, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "'e3790f1484'" + "'627824317b47'" ] }, - "execution_count": 369, + "execution_count": 59, "metadata": {}, "output_type": "execute_result" } ], "source": [ "best_holiday = ''\n", - "best_value = 0\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", + " if cost_of_holiday(h) < best_value:\n", " best_value = cost_of_holiday(h)\n", " best_holiday = h[0]\n", " \n", @@ -291,190 +347,175 @@ }, { "cell_type": "code", - "execution_count": 370, + "execution_count": 60, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "[['e3790f1484', '1682', 'Giessenmestia', '448'],\n", - " ['443a80bb', '1286', 'Mamula', '872'],\n", - " ['2b11e359e8f9', '1488', 'Brorfelde', '613']]" + "['627824317b47', '909', 'Giessenmestia', '532']" ] }, - "execution_count": 370, + "execution_count": 60, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Right answer\n", - "sorted(holidays, key=cost_of_holiday, reverse=True)[:3]" + "min(holidays, key=cost_of_holiday)" ] }, { "cell_type": "code", - "execution_count": 371, + "execution_count": 86, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "[['e3790f1484', '1682', 'Giessenmestia', '448'],\n", - " ['2b11e359e8f9', '1488', 'Brorfelde', '613'],\n", - " ['f793130d4b2d', '1473', 'Nordkapp', '595']]" + "['627824317b47', '909', 'Giessenmestia', '532']" ] }, - "execution_count": 371, + "execution_count": 86, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "# Answer if you take just the base price\n", - "sorted(holidays, key=lambda h: int(h[1]), reverse=True)[:3]" + "# Right answer\n", + "min(holidays, key=cost2)" ] }, { "cell_type": "code", - "execution_count": 372, + "execution_count": 90, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "[['443a80bb', '1286', 'Mamula', '872'],\n", - " ['e3790f1484', '1682', 'Giessenmestia', '448'],\n", - " ['2b11e359e8f9', '1488', 'Brorfelde', '613']]" + "'627824317b47'" ] }, - "execution_count": 372, + "execution_count": 90, "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, reverse=True)[:3]" + "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": 373, + "execution_count": 98, "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']]" + "'627824317b47'" ] }, - "execution_count": 373, + "execution_count": 98, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "[h for h in holidays if h[2] == 'Mamula']" + "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": 374, + "execution_count": 94, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "2130" + "'627824317b47'" ] }, - "execution_count": 374, + "execution_count": 94, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "1682+448" + "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": 375, + "execution_count": 97, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "2158" + "'627824317b47'" ] }, - "execution_count": 375, + "execution_count": 97, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "1286+872" + "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": 376, + "execution_count": 61, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "2101" + "[['627824317b47', '909', 'Giessenmestia', '532'],\n", + " ['18e93a0f0c', '918', 'Ijsseloog', '560'],\n", + " ['d4ab30071b', '895', 'Nullarbor', '589']]" ] }, - "execution_count": 376, + "execution_count": 61, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "1488+613" + "# Right answer\n", + "sorted(holidays, key=cost_of_holiday)[:3]" ] }, { "cell_type": "code", - "execution_count": null, - "metadata": { - "collapsed": true - }, - "outputs": [], - "source": [] - }, - { - "cell_type": "code", - "execution_count": 377, + "execution_count": 87, "metadata": {}, "outputs": [ { "data": { "text/plain": [ - "['627824317b47', '909', 'Giessenmestia', '532']" + "[['627824317b47', '909', 'Giessenmestia', '532'],\n", + " ['18e93a0f0c', '918', 'Ijsseloog', '560'],\n", + " ['d4ab30071b', '895', 'Nullarbor', '589']]" ] }, - "execution_count": 377, + "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Right answer\n", - "min(holidays, key=cost_of_holiday)" + "sorted(holidays, key=cost2)[:3]" ] }, { "cell_type": "code", - "execution_count": 378, + "execution_count": 62, "metadata": {}, "outputs": [ { @@ -483,7 +524,7 @@ "['cf8876d4e73', '823', 'Stonington-Island', '693']" ] }, - "execution_count": 378, + "execution_count": 62, "metadata": {}, "output_type": "execute_result" } @@ -495,7 +536,30 @@ }, { "cell_type": "code", - "execution_count": 379, + "execution_count": 63, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[['cf8876d4e73', '823', 'Stonington-Island', '693'],\n", + " ['647315ef', '824', 'Uzupis', '668'],\n", + " ['d4ab30071b', '895', 'Nullarbor', '589']]" + ] + }, + "execution_count": 63, + "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": 64, "metadata": {}, "outputs": [ { @@ -504,7 +568,7 @@ "['a68d97fbfdb', '987', 'Brorfelde', '451']" ] }, - "execution_count": 379, + "execution_count": 64, "metadata": {}, "output_type": "execute_result" } @@ -516,7 +580,123 @@ }, { "cell_type": "code", - "execution_count": 380, + "execution_count": 65, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "[['a68d97fbfdb', '987', 'Brorfelde', '451'],\n", + " ['627824317b47', '909', 'Giessenmestia', '532'],\n", + " ['be8b9d110', '984', 'Tubakuba', '485']]" + ] + }, + "execution_count": 65, + "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": 66, + "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": 66, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "[h for h in holidays if h[2] == 'Mamula']" + ] + }, + { + "cell_type": "code", + "execution_count": 67, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2130" + ] + }, + "execution_count": 67, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1682+448" + ] + }, + { + "cell_type": "code", + "execution_count": 68, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2158" + ] + }, + "execution_count": 68, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1286+872" + ] + }, + { + "cell_type": "code", + "execution_count": 69, + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "2101" + ] + }, + "execution_count": 69, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "1488+613" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": { + "collapsed": true + }, + "outputs": [], + "source": [] + }, + { + "cell_type": "code", + "execution_count": 70, "metadata": {}, "outputs": [ { @@ -525,7 +705,7 @@ "1441" ] }, - "execution_count": 380, + "execution_count": 70, "metadata": {}, "output_type": "execute_result" } @@ -536,7 +716,7 @@ }, { "cell_type": "code", - "execution_count": 381, + "execution_count": 71, "metadata": {}, "outputs": [ { @@ -545,7 +725,7 @@ "1446" ] }, - "execution_count": 381, + "execution_count": 71, "metadata": {}, "output_type": "execute_result" } @@ -556,7 +736,7 @@ }, { "cell_type": "code", - "execution_count": 382, + "execution_count": 72, "metadata": {}, "outputs": [ { @@ -567,7 +747,7 @@ " ['be8b9d110', '984', 'Tubakuba', '485']]" ] }, - "execution_count": 382, + "execution_count": 72, "metadata": {}, "output_type": "execute_result" } @@ -580,7 +760,7 @@ }, { "cell_type": "code", - "execution_count": 383, + "execution_count": 73, "metadata": {}, "outputs": [ { @@ -594,7 +774,7 @@ " ['52c6f5bab4', '1305', 'Nullarbor', '605']]" ] }, - "execution_count": 383, + "execution_count": 73, "metadata": {}, "output_type": "execute_result" } @@ -606,7 +786,7 @@ }, { "cell_type": "code", - "execution_count": 384, + "execution_count": 74, "metadata": {}, "outputs": [ { @@ -620,7 +800,7 @@ " (['01578ed4e77', '1170', 'Geoje-Si', '487'], 1170)]" ] }, - "execution_count": 384, + "execution_count": 74, "metadata": {}, "output_type": "execute_result" } @@ -631,7 +811,7 @@ }, { "cell_type": "code", - "execution_count": 385, + "execution_count": 75, "metadata": {}, "outputs": [ { -- 2.34.1