Updated languages for polyglot challenge
[ou-summer-of-code-2017.git] / 00-holiday-specs / holiday-specs-solution.ipynb
index 56c3065d3f9f8d7287e9eb4089dda9307336e908..b54dd5f6fc6f57740377926692ce319b6137dea4 100644 (file)
@@ -44,7 +44,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 362,
+   "execution_count": 1,
    "metadata": {},
    "outputs": [
     {
@@ -58,7 +58,7 @@
        " ['01578ed4e77', '1170', 'Geoje-Si', '487']]"
       ]
      },
-     "execution_count": 362,
+     "execution_count": 1,
      "metadata": {},
      "output_type": "execute_result"
     }
@@ -83,7 +83,7 @@
   },
   {
    "cell_type": "code",
-   "execution_count": 363,
+   "execution_count": 2,
    "metadata": {},
    "outputs": [
     {
@@ -94,7 +94,7 @@
        " ['a6538cfa970', '1100', 'Parowan', '661']]"
       ]
      },
-     "execution_count": 363,
+     "execution_count": 2,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 364,
+   "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": [
     {
        "9"
       ]
      },
-     "execution_count": 364,
+     "execution_count": 3,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 365,
+   "execution_count": 4,
    "metadata": {},
    "outputs": [
     {
        "124"
       ]
      },
-     "execution_count": 365,
+     "execution_count": 4,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 366,
+   "execution_count": 5,
    "metadata": {},
    "outputs": [
     {
        "9"
       ]
      },
-     "execution_count": 366,
+     "execution_count": 5,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 367,
+   "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": [
     {
        "1"
       ]
      },
-     "execution_count": 367,
+     "execution_count": 8,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 368,
-   "metadata": {},
+   "execution_count": 9,
+   "metadata": {
+    "collapsed": true
+   },
    "outputs": [],
    "source": [
     "def cost_of_holiday(holiday):\n",
   },
   {
    "cell_type": "code",
-   "execution_count": 369,
+   "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": [
-       "'e3790f1484'"
+       "'627824317b47'"
       ]
      },
-     "execution_count": 369,
+     "execution_count": 11,
      "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": 12,
    "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": 12,
      "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": 13,
    "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": 13,
      "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": 14,
    "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": 14,
      "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": 15,
    "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": 15,
      "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": 16,
    "metadata": {},
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "2130"
+       "'627824317b47'"
       ]
      },
-     "execution_count": 374,
+     "execution_count": 16,
      "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": 17,
    "metadata": {},
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "2158"
+       "'627824317b47'"
       ]
      },
-     "execution_count": 375,
+     "execution_count": 17,
      "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": 18,
    "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": 18,
      "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": 19,
    "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": 19,
      "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": 20,
    "metadata": {},
    "outputs": [
     {
        "['cf8876d4e73', '823', 'Stonington-Island', '693']"
       ]
      },
-     "execution_count": 378,
+     "execution_count": 20,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 379,
+   "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": [
     {
        "['a68d97fbfdb', '987', 'Brorfelde', '451']"
       ]
      },
-     "execution_count": 379,
+     "execution_count": 22,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 380,
+   "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": [
     {
        "1441"
       ]
      },
-     "execution_count": 380,
+     "execution_count": 28,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 381,
+   "execution_count": 29,
    "metadata": {},
    "outputs": [
     {
        "1446"
       ]
      },
-     "execution_count": 381,
+     "execution_count": 29,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 382,
+   "execution_count": 30,
    "metadata": {},
    "outputs": [
     {
        " ['be8b9d110', '984', 'Tubakuba', '485']]"
       ]
      },
-     "execution_count": 382,
+     "execution_count": 30,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 383,
+   "execution_count": 31,
    "metadata": {},
    "outputs": [
     {
        " ['52c6f5bab4', '1305', 'Nullarbor', '605']]"
       ]
      },
-     "execution_count": 383,
+     "execution_count": 31,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 384,
+   "execution_count": 32,
    "metadata": {},
    "outputs": [
     {
        " (['01578ed4e77', '1170', 'Geoje-Si', '487'], 1170)]"
       ]
      },
-     "execution_count": 384,
+     "execution_count": 32,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 385,
+   "execution_count": 33,
    "metadata": {},
    "outputs": [
     {