count": 100,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "# hasher for text\n",
    "text_h = Hasher(\"to be or not to be\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "65"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# hash of the pattern\n",
    "compute_hashes(\"be\")[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "65"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# hash of the occurence of \"be\" in original text. \n",
    "text_h.substring_hash(3, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {
    "collapsed": false
   },
   "outputs": [],
   "source": [
    "def compute_matches(text, pattern):\n",
    "    # hash of patter\n",
    "    pattern_hash = compute_hashes(pattern)[-1]\n",
    "    # hasher for text\n",
    "    text_h = Hasher(text)\n",
    "    res = []\n",
    "    for i in range(len(text) - len(pattern) + 1):\n",
    "        # i is potential match start index\n",
    "        # compare hash in text with hash of pattern\n",
    "        if text_h.substring_hash(i, i + len(pattern)) == pattern_hash:\n",
    "            # if matching append to result list.\n",
    "            res.append(i)\n",
    "    return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[3, 16]"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "compute_matches(\"to be or not to be\", \"be\")"