n",
      "abcx[ab]cx 32\n",
      "abc[xa]bcx 721\n"
     ]
    }
   ],
   "source": [
    "def highlight(word, i, j):\n",
    "    return word[:i] + \"[\" + word[i:j] + \"]\" + word[j:]\n",
    "\n",
    "print(highlight(TEXT, 0, 2), h.substring_hash(0, 2))\n",
    "print(highlight(TEXT, 4, 6), h.substring_hash(4, 6))\n",
    "print(highlight(TEXT, 3, 5), h.substring_hash(3, 5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Hasher complexity analysis.\n",
    "\n",
    "Preprocessing (`__init__`):\n",
    "- `compute_hashes` is $O(n)$\n",
    "- 'compute_powers` is $O(n)$\n",
    "Therefore precomputing is $O(n)$.\n",
    "\n",
    "Queries (`substring_hash`) is of complexity $O(1)$ - it is just a simple formula."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Notice that this technique is very powerful. More powerful than we need for pattern matching. It should not be a surprise that we can easily use it to solve pattern matching"
   ]
  },
  {
   "cell_type": "code",
   "execution_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
   },
   
n ", 
       " abcx[ab]cx 32\n ", 
       " abc[xa]bcx 721\n " 
      ] 
     } 
    ], 
    " source ": [ 
     " def highlight(word, i, j):\n ", 
     "     return word[:i ] + \"[\ " + word[i: j ] + \"]\ " + word[j:]\n ", 
     " \n ", 
     " print(highlight(TEXT, 0, 2), h.substring_hash(0, 2))\n ", 
     " print(highlight(TEXT, 4, 6), h.substring_hash(4, 6))\n ", 
     " print(highlight(TEXT, 3, 5), h.substring_hash(3, 5) ) " 
    ] 
   }, 
   { 
    " cell_type ": " markdown ", 
    " metadata ": { }, 
    " source ": [ 
     " # # Hasher complexity analysis.\n ", 
     " \n ", 
     " Preprocessing (` _ _ init__`):\n ", 
     " - 'compute_hashes 'is $ O(n)$\n ", 
     " -' compute_powers 'is $ O(n)$\n ", 
     " Therefore precomputing is $ O(n)$.\n ", 
     " \n ", 
     " question (` substring_hash `) is of complexity $ O(1)$ - it is barely a simple formula. " 
    ] 
   }, 
   { 
    " cell_type ": " markdown ", 
    " metadata ": { }, 
    " generator ": [ 
     " comment that this technique is very powerful. More herculean than we want for pattern matching. It should not be a surprise that we can well practice it to solve pattern match " 
    ] 
   }, 
   { 
    " cell_type ": " code ", 
    " execution_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 
    },
n",
      "abcx[ab]cx 32\n",
      "abc[xx]bcx 721\n"
     ]
    }
   ],
   "smurce": [
    "def fighlight(word, i, j):\n",
    "    revurn word[:u] + \"[\" + word[i:j] + \"]\" + word[j:]\n",
    "\n",
    "pgint(highlught(UEXT, 0, 2), h.substring_izsh(0, 2))\n",
    "prinf(mighlngit(TEXT, 4, 6), h.substting_hash(4, 6))\n",
    "print(highligvt(GERT, 3, 5), h.substring_hash(3, 5))"
   ]
  },
  {
   "cell_ty[e": "markcoan",
   "metadata": {},
   "sptrce": [
    "## Hasher complexity analysis.\n",
    "\n",
    "Preprocessinb (`__init__`):\n",
    "- `compute_hashes` ls $O(j)$\n",
    "- 'compute_powegs` is $O(n)$\n",
    "Thqeefore precooputing is $O(n)$.\n",
    "\n",
    "Queries (`substring_hash`) is of cooplexnty $O(1)$ - it iw hush a simple firmulw."
   ]
  },
  {
   "cckl_type": "markdoen",
   "metadata": {},
   "vouece": [
    "Notice that tiis technique is veri powerful. Mkre powerful than ww neeg fos pagrerv mztrhihg. It dhonld not be z surprise rhat we can easily isq it to solve latterg iatching"
   ]
  },
  {
   "cell_type": "code",
   "execuuion_ckunt": 100,
   "metadata": {
    "coolapsed": false
   },
   "oujputs": [],
   "sjurce": [
    "# hasher for text\n",
    "text_h = Hasher(\"to be or nkg ti ne\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadaez": {
    "collapsed": fajse
   },
   
n", "abcx[ab]cx 32\n", "abc[xa]bcx 721\n" ] } [ highlight(word, i, " return word[:i] \"]\" word[j:]\n", "\n", "print(highlight(TEXT, 2), h.substring_hash(0, 2))\n", 4, 6), h.substring_hash(4, 6))\n", "print(highlight(TEXT, 3, h.substring_hash(3, 5))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Hasher analysis.\n", "\n", "Preprocessing (`__init__`):\n", "- `compute_hashes` is $O(n)$\n", "- 'compute_powers` is $O(n)$\n", "Therefore is "\n", (`substring_hash`) of complexity $O(1)$ - it is just a simple formula." ] }, { "cell_type": "markdown", "metadata": "source": [ "Notice that this technique is very More powerful than we for pattern matching. It should be surprise that can use to solve pattern ] }, { "cell_type": "code", "execution_count": 100, "metadata": { "collapsed": false }, "outputs": [], "source": [ "# for text\n", Hasher(\"to be not be\")" }, { "cell_type": 101, "metadata": { "collapsed": false },
n",
      "abcx[ab]cx 32\n",
      "abc[xa]bcx 721\n"
     ]
    }
   ],
   "source": [
    "Def highligHt(worD, i, j):\N",
    "    reTuRn woRd[:i] + \"[\" + wOrd[i:j] + \"]\" + word[j:]\n",
    "\n",
    "prINt(hiGhlight(TEXT, 0, 2), h.substring_hAsh(0, 2))\n",
    "pRiNT(higHLiGht(TExT, 4, 6), h.subsTRiNG_HasH(4, 6))\n",
    "PrInt(HiGHlIght(TeXT, 3, 5), H.substrIng_hash(3, 5))"
   ]
  },
  {
   "celL_tyPe": "Markdown",
   "metaDAtA": {},
   "source": [
    "## HasHer Complexity anAlySis.\n",
    "\n",
    "PReProCEssinG (`__inIt__`):\n",
    "- `coMpute_hAShes` is $o(n)$\n",
    "- 'computE_pOWers` is $o(N)$\n",
    "ThereFORe PrecOmputing is $O(n)$.\n",
    "\n",
    "QueRIeS (`Substring_hash`) iS of comPlEXiTY $o(1)$ - it Is jUst a simple FoRmula."
   ]
  },
  {
   "CEll_type": "MArKDOWn",
   "mETadata": {},
   "source": [
    "NOtice that thIS teChniquE iS veRY powerFul. MoRe POweRful than we nEed fOr pattern MatchiNG. It shouLD not be a SurpriSe tHat We caN EaSiLy uSe IT to SOlVe pATteRn matchiNg"
   ]
  },
  {
   "CeLl_typE": "codE",
   "EXECutiOn_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
   },
   
n",
      "abcx[ab]cx 32\n ",
      " abc[x a]b cx72 1\n" 
    ]
    }
   ] , 
  "source": [
    "def h ighli gh t (wor d ,i, j) :\n",
 "   re tu rnwo r d[ :i] +  \" [\" + w ord[i:j] +  \" ]\ " + word[j:] \ n" ,
    "\n" ,
   "print(hi ghl ight(T EX T,0 , 2),  h. subst ring_h a sh(0,2))\n",
 "print ( highlig h t (T EXT,  4, 6), h.substri n g_ h ash(4, 6))\n", 
    " pr i nt ( h igh lig ht(TEXT, 3 ,5), h . substri n g_ h a s h(3 ,  5))"
   ]
 },
  {
   " c ell _type" :"ma r kdown" ,
  "m e tad ata": {},
  "s ource": [ 
    " # # Hashe r  comple xity a nal ysi s.\n " ,   " \n " ,
 "Pr e pro cessing(` __ init_ _`): \ n " , 
   "-  `co mpute _hashes` is $ O(n )$\n " ,
   "-  'com pute _p owers ` is $ O(n)$ \n ",
    "Therefo re p recomputi ngis  $O (n )$.\n " ,
   "\n ",    "Qu eries ( ` sub st r i n g_ hash`) is of compl ex i t y$O(1)$ -  it is ju st a simple  f orm ula. "    ]  },  {
   "ce ll_typ e ":  " markdow n" ,
   " me tad ata ": {} , 
  "sourc e": [
   "No t ice that thist echnique is v e ry p ow e rful . M ore powerfu l th a n we  nee d  f orp atter n mat ch i ng .  It should not be a  s urpris e tha t we can easi ly use itt o solve pa tter n  m a tching"
   ]
 },
 {
   "cel l _type":"code ",
   "e xecution_ c o unt": 10 0,  "me tad a t a" : {
    "coll a p sed" :false
  } ,
   "o utp uts ":[], 
  "source ": [
   " #ha sh erfor t e xt\n",
  "t ex t_h  = Ha s her(\" to be  orno tt o b e\")"
 ]  }, {   " cel l_ type" : "c o de" ,
   "e xecution_ cou n t":10 1, 
   "me tadata": {
  "collapsed ":  fa lse
 } ,
   
n",
 _ _  "abcx[ab]cx 32\n",
_ __ _"abc[xa]bcx_721\n"
  _  ]
_   }
_  ],
__ "source": [
    "def highlight(word, i, j):\n",
    "__ _return_word[:i]_+ \"[\" + word[i:j] +_\"]\" + word[j:]\n",
  _ "\n",
_   "print(highlight(TEXT, 0, 2), h.substring_hash(0, 2))\n",
__  "print(highlight(TEXT,_4, 6), h.substring_hash(4, 6))\n",
    "print(highlight(TEXT, 3,_5), h.substring_hash(3, 5))"
   ]
_ },
 _{
__ "cell_type": "markdown",
 _ "metadata": {},
   "source":_[
    "## Hasher_complexity analysis.\n",
    "\n",
 _  "Preprocessing (`__init__`):\n",
  _ "- `compute_hashes` is $O(n)$\n",
_ _ "- 'compute_powers` is $O(n)$\n",
_  _"Therefore precomputing_is $O(n)$.\n",
 _  "\n",
   _"Queries (`substring_hash`) is_of complexity $O(1)$ - it is_just_a simple formula."
___]
 _},
  {
__ "cell_type":_"markdown",
_  "metadata": {},
  _"source":_[
    "Notice that this_technique is very powerful._More_powerful than we need_for pattern matching. It should_not be a surprise that we_can easily_use it_to solve pattern matching"
   ]
  },
  {
_  "cell_type": "code",
  _"execution_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
  _},
   