Added some more one-liner solutions
authorNeil Smith <neil.git@njae.me.uk>
Sat, 10 Jun 2017 11:09:56 +0000 (12:09 +0100)
committerNeil Smith <neil.git@njae.me.uk>
Sat, 10 Jun 2017 11:09:56 +0000 (12:09 +0100)
00-holiday-specs/holiday-specs-solution.ipynb

index 56c3065d3f9f8d7287e9eb4089dda9307336e908..c814a6ec0da0c85419bff650823514d5c8508de7 100644 (file)
@@ -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"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 364,
+   "execution_count": 54,
    "metadata": {},
    "outputs": [
     {
        "9"
       ]
      },
-     "execution_count": 364,
+     "execution_count": 54,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 365,
+   "execution_count": 55,
    "metadata": {},
    "outputs": [
     {
        "124"
       ]
      },
-     "execution_count": 365,
+     "execution_count": 55,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 366,
+   "execution_count": 56,
    "metadata": {},
    "outputs": [
     {
        "9"
       ]
      },
-     "execution_count": 366,
+     "execution_count": 56,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "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": [
     {
        "1"
       ]
      },
-     "execution_count": 367,
+     "execution_count": 96,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 368,
-   "metadata": {},
+   "execution_count": 58,
+   "metadata": {
+    "collapsed": true
+   },
    "outputs": [],
    "source": [
     "def cost_of_holiday(holiday):\n",
   },
   {
    "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",
   },
   {
    "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": [
     {
        "['cf8876d4e73', '823', 'Stonington-Island', '693']"
       ]
      },
-     "execution_count": 378,
+     "execution_count": 62,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "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": [
     {
        "['a68d97fbfdb', '987', 'Brorfelde', '451']"
       ]
      },
-     "execution_count": 379,
+     "execution_count": 64,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "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": [
     {
        "1441"
       ]
      },
-     "execution_count": 380,
+     "execution_count": 70,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 381,
+   "execution_count": 71,
    "metadata": {},
    "outputs": [
     {
        "1446"
       ]
      },
-     "execution_count": 381,
+     "execution_count": 71,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 382,
+   "execution_count": 72,
    "metadata": {},
    "outputs": [
     {
        " ['be8b9d110', '984', 'Tubakuba', '485']]"
       ]
      },
-     "execution_count": 382,
+     "execution_count": 72,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 383,
+   "execution_count": 73,
    "metadata": {},
    "outputs": [
     {
        " ['52c6f5bab4', '1305', 'Nullarbor', '605']]"
       ]
      },
-     "execution_count": 383,
+     "execution_count": 73,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 384,
+   "execution_count": 74,
    "metadata": {},
    "outputs": [
     {
        " (['01578ed4e77', '1170', 'Geoje-Si', '487'], 1170)]"
       ]
      },
-     "execution_count": 384,
+     "execution_count": 74,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 385,
+   "execution_count": 75,
    "metadata": {},
    "outputs": [
     {