{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#small dataset, navie way"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "f_input = './input'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Input</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>11</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1692</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "   Input\n",
       "0      5\n",
       "1      0\n",
       "2      1\n",
       "3      2\n",
       "4     11\n",
       "5   1692"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "nb_test= data.Input[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nb_test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def to_sleep(N):\n",
    "    old_set = set()\n",
    "    i = 0\n",
    "    T= 0 \n",
    "    sucess = 0\n",
    "    max_try = 0\n",
    "    \n",
    "    if N==0:\n",
    "        return 'INSOMNIA '\n",
    "    else:\n",
    "        while not sucess :\n",
    "            i += 1\n",
    "            T +=1\n",
    "            new_N = N*i\n",
    "            new_set = set( [int(_num) for _num in str(new_N)]) \n",
    "            old_set = old_set.union( new_set)\n",
    "\n",
    "            sucess = ( len(old_set)==10 )\n",
    "\n",
    "        return str(new_N)\n",
    "   \n",
    "        \n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def run(infile, outfile):\n",
    "    data = pd.read_csv(infile,  sep='\\t',names = ['number'])\n",
    "    with open(outfile,'w') as g:\n",
    "        for index,N in enumerate( data.number.values[1:]):\n",
    "            g.write( \"case #\" +str(index+1)+': '+to_sleep(N)+'\\n')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 141,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "run('/Users/mac/Downloads/A-large.in','/Users/mac/Downloads/output.txt')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 146,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "100\r\n",
      "0\r\n",
      "1\r\n",
      "2\r\n",
      "11\r\n",
      "1692\r\n",
      "86364\r\n",
      "652011\r\n",
      "766971\r\n",
      "531056\r\n",
      "699299\r\n",
      "386190\r\n",
      "795778\r\n",
      "907470\r\n",
      "969177\r\n",
      "999999\r\n",
      "116547\r\n",
      "12500\r\n",
      "907214\r\n",
      "855139\r\n",
      "459789\r\n",
      "219653\r\n",
      "334159\r\n",
      "159187\r\n",
      "402979\r\n",
      "335104\r\n",
      "8\r\n",
      "511003\r\n",
      "94951\r\n",
      "694327\r\n",
      "520887\r\n",
      "615892\r\n",
      "766601\r\n",
      "60701\r\n",
      "999998\r\n",
      "413929\r\n",
      "125000\r\n",
      "10\r\n",
      "284982\r\n",
      "465598\r\n",
      "7\r\n",
      "697945\r\n",
      "692785\r\n",
      "709677\r\n",
      "999995\r\n",
      "999991\r\n",
      "125\r\n",
      "725924\r\n",
      "200\r\n",
      "999992\r\n",
      "718508\r\n",
      "547018\r\n",
      "717440\r\n",
      "519515\r\n",
      "1250\r\n",
      "839136\r\n",
      "124\r\n",
      "165784\r\n",
      "657748\r\n",
      "61436\r\n",
      "160515\r\n",
      "6\r\n",
      "49299\r\n",
      "54700\r\n",
      "25\r\n",
      "166\r\n",
      "363205\r\n",
      "525772\r\n",
      "337093\r\n",
      "646257\r\n",
      "40\r\n",
      "418483\r\n",
      "716928\r\n",
      "1000000\r\n",
      "344090\r\n",
      "586334\r\n",
      "899089\r\n",
      "5\r\n",
      "114551\r\n",
      "315510\r\n",
      "380354\r\n",
      "381329\r\n",
      "4\r\n",
      "663394\r\n",
      "31781\r\n",
      "9\r\n",
      "999997\r\n",
      "436428\r\n",
      "750836\r\n",
      "999994\r\n",
      "3\r\n",
      "35143\r\n",
      "109317\r\n",
      "522460\r\n",
      "999996\r\n",
      "20\r\n",
      "239365\r\n",
      "999993\r\n",
      "3940\r\n",
      "527353\r\n"
     ]
    }
   ],
   "source": [
    "!head -n 100 ./A-large.in"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 144,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "case #1: INSOMNIA \r\n",
      "case #2: 10\r\n",
      "case #3: 90\r\n",
      "case #4: 110\r\n",
      "case #5: 5076\r\n",
      "case #6: 259092\r\n",
      "case #7: 4564077\r\n",
      "case #8: 3067884\r\n",
      "case #9: 3717392\r\n",
      "case #10: 3496495\r\n",
      "case #11: 1544760\r\n",
      "case #12: 3978890\r\n",
      "case #13: 4537350\r\n",
      "case #14: 2907531\r\n",
      "case #15: 9999990\r\n",
      "case #16: 466188\r\n",
      "case #17: 900000\r\n",
      "case #18: 3628856\r\n",
      "case #19: 2565417\r\n",
      "case #20: 4138101\r\n",
      "case #21: 878612\r\n",
      "case #22: 1002477\r\n",
      "case #23: 1114309\r\n",
      "case #24: 1611916\r\n",
      "case #25: 3015936\r\n",
      "case #26: 96\r\n",
      "case #27: 3577021\r\n",
      "case #28: 569706\r\n",
      "case #29: 2082981\r\n",
      "case #30: 3646209\r\n",
      "case #31: 3079460\r\n",
      "case #32: 3066404\r\n",
      "case #33: 424907\r\n",
      "case #34: 7999984\r\n",
      "case #35: 2069645\r\n",
      "case #36: 9000000\r\n",
      "case #37: 90\r\n",
      "case #38: 1709892\r\n",
      "case #39: 2327990\r\n",
      "case #40: 70\r\n",
      "case #41: 2093835\r\n",
      "case #42: 2771140\r\n",
      "case #43: 2838708\r\n",
      "case #44: 6999965\r\n",
      "case #45: 9999910\r\n",
      "case #46: 9000\r\n",
      "case #47: 2903696\r\n",
      "case #48: 9000\r\n",
      "case #49: 5999952\r\n",
      "case #50: 3592540\r\n",
      "case #51: 2188072\r\n",
      "case #52: 2869760\r\n",
      "case #53: 2078060\r\n",
      "case #54: 90000\r\n",
      "case #55: 2517408\r\n",
      "case #56: 2356\r\n",
      "case #57: 828920\r\n",
      "case #58: 2630992\r\n",
      "case #59: 491488\r\n",
      "case #60: 963090\r\n",
      "case #61: 90\r\n",
      "case #62: 345093\r\n",
      "case #63: 273500\r\n",
      "case #64: 900\r\n",
      "case #65: 5478\r\n",
      "case #66: 1089615\r\n",
      "case #67: 4731948\r\n",
      "case #68: 1685465\r\n",
      "case #69: 2585028\r\n",
      "case #70: 920\r\n",
      "case #71: 2092415\r\n",
      "case #72: 2150784\r\n",
      "case #73: 9000000\r\n",
      "case #74: 1720450\r\n",
      "case #75: 1759002\r\n",
      "case #76: 4495445\r\n",
      "case #77: 90\r\n",
      "case #78: 572755\r\n",
      "case #79: 1893060\r\n",
      "case #80: 1901770\r\n",
      "case #81: 1906645\r\n",
      "case #82: 92\r\n",
      "case #83: 2653576\r\n",
      "case #84: 158905\r\n",
      "case #85: 90\r\n",
      "case #86: 9999970\r\n",
      "case #87: 1309284\r\n",
      "case #88: 8259196\r\n",
      "case #89: 5999964\r\n",
      "case #90: 30\r\n",
      "case #91: 105429\r\n",
      "case #92: 327951\r\n",
      "case #93: 1567380\r\n",
      "case #94: 5999976\r\n",
      "case #95: 900\r\n",
      "case #96: 718095\r\n",
      "case #97: 9999930\r\n",
      "case #98: 15760\r\n",
      "case #99: 1582059\r\n",
      "case #100: 918\r\n"
     ]
    }
   ],
   "source": [
    "!head -n100 '/Users/mac/Downloads/output.txt' "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
