Tweaked day 1 solutions
[ou-summer-of-code-2017.git] / 01-ticket-prices / ticket-pricing-solution.ipynb
index df02700d400d03ef34215d2097fc5cf347616d9e..8ba20442b6dae95c16ff439de4d42a0c242d26c1 100644 (file)
   },
   {
    "cell_type": "code",
-   "execution_count": 1,
+   "execution_count": 2,
    "metadata": {},
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "[['07ea553e', '1348', 'Zhangye-Shi', '14'],\n",
-       " ['02d4b258b8a', '1152', 'Estacada', '3'],\n",
-       " ['d29a0551504e', '1210', 'Zhangye-Shi', '7']]"
+       "[['dda7d369', '1546', 'Uzupis', '21'],\n",
+       " ['68022753', '1239', 'Mamula', '21'],\n",
+       " ['b261dbd1cef', '996', 'Holmegaard', '21']]"
       ]
      },
-     "execution_count": 1,
+     "execution_count": 2,
      "metadata": {},
      "output_type": "execute_result"
     }
@@ -69,7 +69,7 @@
    "source": [
     "holidays = []\n",
     "with open('01-holidays.txt') as f:\n",
-    "    for hol_line in f.readlines():\n",
+    "    for hol_line in f:\n",
     "        holidays.append(hol_line.split())\n",
     "        \n",
     "holidays[:3]"
   },
   {
    "cell_type": "code",
-   "execution_count": 2,
+   "execution_count": 3,
    "metadata": {},
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "66"
+       "[['dda7d369', '1546', 'Uzupis', '21'],\n",
+       " ['68022753', '1239', 'Mamula', '21'],\n",
+       " ['b261dbd1cef', '996', 'Holmegaard', '21']]"
       ]
      },
-     "execution_count": 2,
+     "execution_count": 3,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "holidays = [line.split() for line in open('01-holidays.txt')]\n",
+    "        \n",
+    "holidays[:3]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 13,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "59"
+      ]
+     },
+     "execution_count": 13,
      "metadata": {},
      "output_type": "execute_result"
     }
     "len(affordable_holidays)"
    ]
   },
+  {
+   "cell_type": "code",
+   "execution_count": 5,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "59"
+      ]
+     },
+     "execution_count": 5,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "affordable_holidays = [h for h in holidays if int(h[1]) <= 1200]\n",
+    "len(affordable_holidays)"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 14,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "124"
+      ]
+     },
+     "execution_count": 14,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "len(holidays)"
+   ]
+  },
   {
    "cell_type": "markdown",
    "metadata": {},
   },
   {
    "cell_type": "code",
-   "execution_count": 3,
+   "execution_count": 4,
    "metadata": {},
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "66"
+       "59"
       ]
      },
-     "execution_count": 3,
+     "execution_count": 4,
      "metadata": {},
      "output_type": "execute_result"
     }
    ],
    "source": [
-    "sum(1 for h in open('01-holidays.txt').readlines() if int(h.split()[1]) <= 1200)"
+    "sum(1 for h in open('01-holidays.txt') if int(h.split()[1]) <= 1200)"
    ]
   },
   {
   },
   {
    "cell_type": "code",
-   "execution_count": 4,
+   "execution_count": 16,
    "metadata": {
     "collapsed": true
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 5,
+   "execution_count": 17,
    "metadata": {
     "collapsed": true
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 6,
+   "execution_count": 18,
    "metadata": {
     "collapsed": true
    },
   },
   {
    "cell_type": "code",
-   "execution_count": 7,
+   "execution_count": 19,
    "metadata": {},
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "'cb24efd90'"
+       "'ee064e1e2ea'"
       ]
      },
-     "execution_count": 7,
+     "execution_count": 19,
      "metadata": {},
      "output_type": "execute_result"
     }
   },
   {
    "cell_type": "code",
-   "execution_count": 8,
+   "execution_count": 20,
    "metadata": {},
    "outputs": [
     {
      "data": {
       "text/plain": [
-       "'cb24efd90'"
+       "'ee064e1e2ea'"
       ]
      },
-     "execution_count": 8,
+     "execution_count": 20,
      "metadata": {},
      "output_type": "execute_result"
     }
    ],
    "source": [
+    "# Right answer\n",
     "max(affordable_holidays, key=value_of_holiday)[0]"
    ]
   },
   {
    "cell_type": "code",
-   "execution_count": null,
-   "metadata": {
-    "collapsed": true
-   },
-   "outputs": [],
-   "source": []
+   "execution_count": 21,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'c86e2e5826'"
+      ]
+     },
+     "execution_count": 21,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "# Answer if you don't filter by affordability\n",
+    "max(holidays, key=value_of_holiday)[0]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 23,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'f60e203aaaf9'"
+      ]
+     },
+     "execution_count": 23,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "# Answer if you don't scale by perceived value\n",
+    "max(affordable_holidays, key=lambda h: float(h[3]) / float(h[1]))[0]"
+   ]
+  },
+  {
+   "cell_type": "code",
+   "execution_count": 22,
+   "metadata": {},
+   "outputs": [
+    {
+     "data": {
+      "text/plain": [
+       "'f60e203aaaf9'"
+      ]
+     },
+     "execution_count": 22,
+     "metadata": {},
+     "output_type": "execute_result"
+    }
+   ],
+   "source": [
+    "# Answer if you don't scale by perceived value, AND don't filter by affordability\n",
+    "max(holidays, key=lambda h: float(h[3]) / float(h[1]))[0]"
+   ]
   }
  ],
  "metadata": {