Skip to content

Instantly share code, notes, and snippets.

@huntie
Last active November 10, 2015 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huntie/008ed0d144f122ad6af0 to your computer and use it in GitHub Desktop.
Save huntie/008ed0d144f122ad6af0 to your computer and use it in GitHub Desktop.
Challenge to find permutations of words within a scrambled text file
text = open('scrambled.txt', 'r').read()
words = ['miner', 'willy', 'andre', 'jet', 'set', 'bug', 'byte']
def permute(string):
result = []
if len(string) == 1:
result = [string]
else:
for i, char in enumerate(string):
for permutation in permute(string[:i] + string[i + 1:]):
result += [char + permutation]
return result
def miner(words, text):
matches = {word: 0 for word in words}
for word in words:
for permutation in permute(word):
matches[word] += text.count(permutation)
return matches
print miner(words, text)
rmniex* .@_cv*! *ndare%+%meinr&v&*^(tes^zv*%.&cz ^ .@.(tes^erand(.z!*__.@@+!%@(zejt*&)(+)v&+*+zc.vcestx&*@@(_x_v*^_z
tes.%@+@_)ylwli@.*c(reimn&&.)x!+ *%%set( z&.+zv +x+czanedr@*)@_@@!+!xylwil.z xz%+*!)!cderna*!meinr!*+)z&z_v(v&&.v%
rmeni.^@c^(z.v(%meinr..inmre(@&*!x_%)wlily*%+xc ^(.@++&v^tseczc) +&c*ilwly&)(@xzwlyilx&^(z&+!znadre^&c^^z.z.z+v
eirmn& %%c%(@)setv*v) ets&zz)viywll%@^&+( !(+_(ste*tse*einrm^%)set%cz& . llywivzz+ ^ _est.&@%@v)*vets(++)%)+_^
radnec!@+*)%_cliwylc&+@x.%+xvvremni^)x ^@^)liwylvc@()ets &vjet*%x*estx ..%x__v.c^@xejt!(%*vc!c+x&) !lilwyc(@vv! z^ + (!x
erdan*.ste(v.wylli!.c_zc%^z ^_.%dnrae*v +)%v%**^.vardne_riemnc)(._(vc!_iwllyc&%!x.vdnear%%z(vnedarc !@_v_^cvz+!+
tbeyc&+ ^_%._^(@+&x+edarnz.!^!^z^+v_!cwlliy )(!@)z.)+*zest_^!(x%.c& cz.x ernad).cz*z!!vvxtje_!z&*^%(c
drnea@!.)_@ste(%c^x*_c_v+cz)%_ets&+@z)*&& liwly_(&(!*!+z_adernv.+@z+tesx!)*xdrneax@x^&()@ @ejt)+!!(+@
etj%&^rdnae*zzset_!zx!&v_ vx(lilyw+_x.iwlyl*@stez%x%&%+@.!vz@ilywl++)ccx%^(stec!*!c&@_imenr+cx(cz.)._(z
set)tsex!^!))_^edanr(! xx(_%+_@iermnvx%x*+*))renda+)*..@^!_ste!@c .(& _x()v^(ilwyl+z!!iernm+*))!()*_nrdaevx!) %%cvv@.x
yilwl%.ranedx%*.*%^ ^@ _!@ste%.@*v(xc^tes) ^@x @ (!wylil.x*vc(*&*!zc !lywli. %.%^%vx)@!v!zywill&(& _*_ ^+z(cx
raend!.c)+ndrae! !+*.%!iywll!_z^vvc.ste( (_(%.*&tse^*^@).!+c@c!zz*(tes (c_@!(._+vtesx(v(!z! +*ste% %)
ernda ^einmr(x%)&vz%.&adnre)einrm(_.!%v^x)+_c&c%nreim&+^v@^_)*+*@&dnare_zc&x^derna&^(^x^x^v%*_lilwy)%@
imrnev@c**stev)_*.%%x_+zx_*imrne& v*@ !*ste!_dnear+)&(^x(andre@x^ ( (!z^rdean ^_@*c*^+&xz(tse(z_& @.!) xz!
nread.(*+(+%@ ^xztes)& ^xz)liwly&&+.vczv^@+vdeanr@^(@*x %@%.@z.*zadenr(+_!())((%^wlilyv@_!%@!*^set.*.x^ %vx%+^
lwyil(+^tse!@.^x_* *_@^x&*setv^+)^@ xv&!! .ardne&&&_(^+x)z!@^x)^mienr)x zxz+x zx&xmiern_^*...raned__%(.
stevc+yiwllc!(stez)v+c !*_.^+)liwyl&x%&(!+.+++x^%ste!@*(xzz*vednra*_&*_zndera^^_xz)set^+vtes*(!xv+z_
tsevnmrei*z +*+vadner^vcz!@.(v((_lwyil.ndarec_%z..**!@(!*(^*mnriez)x+_ets +cc c^**%zx+tesv@)+&@ )@wilylcc(c^_!z!)*!
est.@tes_z@)__ est _stecv^_^liwly@!!.@zxlwyli+c!draenvzcejtx%vc+*!^(+^@!erdanv.z*.z v^*ets &!*%@+!vv%+!*!
iywll.(.)!)&%^&+&cyllwi@*!z_v+x&@&(__@!tes%c%(z(@.+z*)^lwliy!&)%.ste*.vz^ nraedx(* !+ ^liwly))&(^+!z&z()
lwlyi&!ylwil_ darne_narde@(x@+x@%))(mnrei^ z.! !!cc^%)@rnmie v^z_c !^)@.z!v wiyll%*.+!*cnread(..&^*cllwyiz_)***__%
einrm@zxzz )c +@c@+tes)xx( *x%+*&)rneda_.z.v%.*_@(ste.z*)!ets^nmeir_iywll(& %+*!z...v*yliwl(&_!(^_)*&(z
nimerxdrane& *_wylilvvxv!).*.x_mrein_)x))&v+zc_@)%@renad(++)%v.+ _x.xaernd%) @+vc*%tes()zx^(zx_%v)).llwiy)))
tse!^*erimn&().%+z*^.+zetj&.).xvx).x*(c&x%ernda!! %xv _x_ets.c%x%*%jet )vc.+!@*z!&dnrea*!c+z _.x&!%dnarev!)c
etsxv!(xderan*ylwli)(*+z)(zx&@.@!^set*enimrvvx_tsecz&!.(_%@*_(adrenv(!+v%nrdae)^zc%&c!ste_)%ywlli_&&c&!*)v@&&)&
renmi&zeadnr &raden%(&.)( xv&v^v!rmneiz%++x_z)_!@_%)ste)x@^drenavv^xernda+%@((^*^miner*_%+)&tsec)x_v+**!c+%.
redna(+z! &lliwyv+emnir*cestx)xz(zxrenda()_((xz%(!** lwily&x@xvywlli)@&cx_ x@ +*!lylwi+&) !estx* z!()v!cx
estz)) )c_@xrandec*.cxx%@tescc)c@ xv(*&xdraen**v @@^!)ywlli*.xndraexx(&&_@*xaernd@llywi*%x&&(xc%tes(*^)!.
setc ) !.^_x((z(z%xets zz tesc* %x%@@v )v(+radne %.x)!xz betyx%&*derna__iernm@*.%@ )(*!^*tes@vxlyiwl__c++! v
ernad!x_@c!_+_ .v%etsv!^x_%%__^_% .^*iyllw@z^^@v^.%v((vcset*x+(_(*@cz*+%!)xest@ (@aredn & (_zzvaernd_x .@*!)v@vc@
yllwi%x!*))x^^)x.willyzx@v&+*+&+c!wliylvtes(z!!@!&z@(.est)! c^).ctes!+)%!vxvlylwi) !@^%@x@lilwy(! xiylwl)!^&&!v+%z*_v&^+
eandr@*.)+.%@)&%+!^^ets%^x_+.v@)xv@@merni*x_)*_z_ste)+ +ilwly.set*@!v.(^inmre+%z!&vcv^%tejc&!@()c.&*+_c
tej _@^% _vrdaen.x+zv@z+tsezraned.%)vv)v)v&! *)(zwlliy+xv%&lyilwx ((+xvaerdn*c))&&c^* %x+mrein_*ste.)*+!!v^
danerz^*&@&x%wliylzjte@cc!)&..%x*zerminv!._ cc^v@@v^!.earnd& z*^deranz)%.v_&c!vv*neard!&z!.vx+*+@zdarne^cc@@+.^!!(
set@ )x%xcz@(!^^_%&adern++ets xz%..++estc@v.+wllyi)** v%* c*%yillw@_@.(z^*!.* (@_ejt%^._()!* cc.cx^_
lwyil.^v@zx!%_&tes* cx*(z(*_) x%xets)mnire*x_tsev@@%est.x+v^^!cxv)*%!@.llwiyc(%.+xtes*(!+.!)c_+zx@(*+
imner)!@%_*( zv.&_ znmrie@%!.vzjet*)vv^ %+cest..x*z_+vsetcc&@z *ylwli^)_@@)&!&c.!c .vadern+(*etyb)!!!
rmeni!vtejv@(c&^&(est+@.(cz!*^z+ilywl^*jtexc+.%est )c+ tes*%c)^tse cxvtjev)%)^ (z*x x&%tse^c%zxx .))^&vcx
etjz!)!%.+!@._^&.xilwyl(zz+(c_ste%z) .@ +@etsvcsetz._*lilwy+*%z*_cnrime (@++_^z.^).+&c(arnde+)& zx !
darne&+!(^@&)drane*+tes*!setz%@zerdna+.@v@*xc^c+%nread &_ _!!c_+ **(z.adner xlywli&.^x%@x&&)^!%x&xsetc+%z_@
adner!z&+(!!%.%_!&^etby )set@%v+__cz *@*wliyl_z _++@_x%xdrane+^x)*!_z&((_!!imnre^_*stec%cc^_tes.dranex_z.v.(^x% !*z*
lwlyiv .c&(!lliyw^z)+&&+imner)!&!%@)++^*lyilw()imren+!)cx&%*)+etsx))+++v_)@x()v xwylil!@.@.%*^^@_ @stecv!)&&x..x
iywllz!nerdax_)ywill@z (x*x*)meirnvv@c(__x!@set&xx&v*%_liwylzxvxnremi&&_v!.lywli%^@.%_z@*)++xlwliy_.! %
ste.v*v(c+(.c%x(v*@meirn&_@z!zv!+.^v(dearn)rnaed%*(+*c))&%!z)narde@!. _illyw)tsec)!^) % ( v %etszcx.!(@.v(!%@^(
iylwlc^_cz%&(@@..tescz(@v(!c&&%.tse!z!x.^(!_&_v(!.!ets)_(jet*cylliw^lywil)cxzv&@%zx_ !&zzste.*!^x!*!_!& z!
tej^&+%^*!tes^. x_^)c%*__x.**ste!*.!v& !*_x*aerdn)c^&x%)^v+yliwl_@%z^xets.+x!*+^+^aredn)z@cc*ejt+cv((.xv!x_(&@ .
tsex)%._!*.cejt*+v c)z&*(%(*rdane.))*@&vcstez^z)+*.&ets) _+!@.@x%.%nemri*ermni@@ .&@)+%&(_renad& ^tes@_v%+_&%^v@vcvvc
iemrn@x(*c@xv)@%xc*).ste^%+)%zybte_v+&c)!v(@!ylwli% v+v+v%*^)._&+)estx.z^!__&*^ z)nermivxv(*crnaedvtes)x@*@& +c_&*x*
tse(&+_c_&@.+*v(mneir!^_lilyw& set_+!^**z@tse+.nraed(^!(*set*nraed%v(!etsx()(..rnade^.cx*)eanrd%&_x&+x*
miner.^&++ )%_c* +tes%!wllyi+_@vv&@@%)xlyilw*.ywillxc ._!tje^_.%z!raned_set)tse v*x) v c nerad!* %& x@
etsc*c_*%tes)x( @*@c)). +@xrndea ^)_%v%&%%@%tje@&)ste ^v_nmreiz@(_^*& vz!^cnredavcz+z*v@_.c%^^cienrmcx!%
tse^(^c.%&&ywlil.^)z.x v+x+%@ @ ywlli.)erimn@est)!^!c._+) ^x@&llwiy*z*_v^+_*+set( %cx^)z@yillwz)z^%(@!%._@+_)
illwy.&&%v@ ^vxx&draen@(wilyl(x!%%v) x+x@wlliy_%&%^^!_()+xctje z!)(ets_v.cvc..%ets%. +._llwiy^&v+tsevv
wylil. !@%v daner+(&%+renad*.)!%+&.xc&&%!^ets@ .v.+(@+cvvz%ylilw*x.c.x &xctes%.(vv_^.z^lwyil+%.rndae^!( %!(%)^&
ste^()+%*.^&^)_erinm@@&set&.ets +_&est ^&_^%^%@*yllwi**^z^ernad_x(%*nemir^v%(+x&v@est_^)v.(@^&c*@erdan(_v &vx.%^zv@ @(
eadrn_( _z@xx!x*erndavv !.@(^%.@liwly+z%ilylw.zndera&.(xrmienx!%%cv&_(_estxeinrmv!x&)&&ywill()*&&.+.&*^*&%%@
tescc*&zzzv%c_._est!!tes .c+dnaer_@v% ^@(iermn*&@ @zz)ste(**@.^!+nrmei_c)^_&+.ctse% &^x )yllwi._%*+%&v* ! _v!.
nriem!.^_++(@!*denraxv(* @.%.v%est+&x czc. +vvtse%@^+_()+c!&v*tes_^()cllwyi v@z+*%denra!v*z^)llwyi+&_
ernda&_@danerc_v^vz* %*zz&raedn^*!!^)&(* x!*&*tje+*@cz_x*zc&niremc@_xcc+z)^wlilyc *_%^c&*+*)(+@(reand.@% zzvzc__v!
wyillc+^. ^c)est))xiylwl*&)**vc)++)v(z*miren!)nrdaev.^__tes%@!^ wilyl!_)^ .arden!@v^^!%!(x*tse+ .+c)^!&@%
tej_^@%(^@^+((*c_@@iylwl_&%!^varedn_%%*c&@.^++ tejv&@)xvc+%+xdnera_)x(!.xx(%% &c(est(lilwy+@x@^+_vzrenad x%&zc
irmne*^+^zzliylwc^zx&(vcc^ +_&!liylw^_llywix^tes.jtez@!xx%_!%c)^ ztbeyzlliwy&c *zx *z vradne!)&c%ccetjc_@!.!
enmri%___@&%x)(z tes^ zv*v!_ %^(+&&(ndrea&_z^z* .+x!@)lylwi%(_*tes* %v%*z&_z)etsx&.!v_^%.@*.*vndaer x&x+c_
rdnea%v*.ylwil!arnde.*%)z.!*x%^xcv*llyiw+z%^(&lwliy%v*+c(+v*(*+&ylwli)+!cz@_*v%^.ylwilx@z@+^tes@_^))*(*(vc&^x
neradz%&x)+cz&)(cjet@++%dnaer+z%!+(+ .etj@+& .@cc^vset.c@etsx.redna_(*%vx._aredn(v%xmrenizenrda!estv!_@^
aendr.nrdeaxv%!lwily%*.+_ &*x&_!xest_&*&)_). vz!! ++enard_@etsc+xzx_z%v xx&^arnde+_*zvzx% xendrax@)^(c%c+c
wiyll^*&xxnmriez&&z)(%(&x_v_nadre_!*+%!.(ste^@.@*(!+z)_yiwll ! yilwl.x!_._&_.c)&x@enmri%c@*+@ndaer^.(
ilwly+.@zx^)(z&x^liylwz^&!)x)z+&&zzmeinr*&^*z_stex^x*)jet(^ _wlliy@lwyli_.^!v%(&wylliz.est%c(v^^illyw_ z@*_x@!%z)x(_)
nrade)v^z^c&*^ste_ylliw(x((@ @earnd_* &&v_(.wllyiv&.^_ctse(%!x.stezx^^+x&.ctes x%! )*%.v _*est&z&!z@% !.^%c
nerad@^!endarx(()%cvienmr+!nadrec*^x+&x)v*mirne%@__z*x*v@)^x*!zendar@@zmeinr yiwllz^!+)+cz+._!.+^ wlliy!^^_.+x++c&@( !
est!.v .(_z())ylliw)cx(z%x ((. ste(++zzv*+ % c.ste .z@&.andre)v!ste(%c^est%v+%%!xx^%^anrdex!)!vxx(@.@^v
imrne^@@z(xc(@_c)^c.@tes_%@. .*(. c@nared!*@^x*v)+ylwil(^+.z(!)&*x_(@mnire +@^ )(xc&+c+(^.ndrea*)_est^^.x*.(^^!!!(z.*
ste.@%_z^z* c ^.ylwli)z x+x!_(c+.v^*lyiwl!( *)z)cxllywic!tse+%@*^c.x.zx++_ rdaen%cv_*vvc. lylwi_&ndrea(*xc(.^%&)c@+^xc
ste^_x^x!z&set_ +c! *)xcc*tesv+._(%*z @)zdanre%)&!!x@%^%est _vx^%^&lliwycc+_nredazcdrnea&c*z@@_vest_ @_zz+
rdneaz_vrmein%+ ..&^dnare^z@!^zardnex^)c&vv%+ (vwlylixvxx)+(+tes&+ v.+(_c est %+%^^wlily) +_+.)v+!v&&z__
est+&&&@viernm%&&_*v@!z)_nreadc@+^)*(z*@tesx^v@@)jte&llwyiv%c (+^!)v_iwyll)*&@v&vc+x!niemr*@v cv^v%+%!
set!@@.cx(x(+)x^est@.(cx)tes &&x!(@ywill@!&_xv@(% ^ciermn!@xminre+@)xc(&zv!)_^+wyllix%set *zc+est+.%+_! &vx^^)
dnare&+^( !(x+&yilwlv^_!z+rdaen! (^*.%_.ste%arend.&)**c.++!yillw++_x@^)() v_.tes).!%(+wliyl%c)^^estzx!vx &_
menri!eadrnc.arndev!(x @c)+tesc!+c!(@v_cxx%.tse**ets*c !)!&vc c@iemrnv ylwli_)v_yilwl%^+!(^x^lyilw( . ..)%v@x
est@x.@tse+!+(*!z x (!!.est&!rminex@_*+&x!_&(%.+^lyliwc(v^)()setx+%+@%x%*.+%@@ ets@x)*(_ vz )^v% zets.&+v()!%c(. ^x&
lwylixv!x^)*x^cc*(z_^tes&!*tse(!cwiyllx@x^@!*@vvzvteybc)%xv+*x(tse*^.xc_*..v!)vxlyilw&!!!&!_^_ +c%_ v
rande%+(c.@vlwyli_%@vtse^) setv&_x* @v@(^@vtej%v+*(!++_(lyliw+c^lyiwl!@ c!!^@*^rinme@&+(@%&x^&c(%@c%jte*&!(c
llwiyzccc*^c*%dranexcz( x* +.ywlil +&@v% ^z).)*etscx(!).c xdrnea)cwyill_ +@yiwllx%c**!_iywll))ywlilx*c!)zvz. *v^)*+
wilyl()(+z^%tsev+z&++*^)liywl*(*_*+vzxcx+&vest@@x+z+*v+vc+(_ eanrd&x^.*_v(v !)&(.mrien) vste_ .^)z)zc.!&c)
ejt&!v_wliylx**(z)^.x@&)lyiwl%&)&!% +set)&@c!v&%c_!z@!^^inmrexz@. !@+)x^%jtex+(@^liwlyz+cz.*vxliwly.)
wyillz(^@) z+( tes!_*z* (+ **+_iwlyl^v&(!!&illwy&%^((.&.c_lliwy!cx!vwllyi%))xx+@*+z)&ermni(rndae%est!!_^@+(.@+ x
wlliy anedr @%llwiy&_)@@@+&!&c**%lwyilcx *!.z.+vcwylli __set %vv^z *_ xets+x(dearn.^(z^%zzv& *)!nerad&!.
eadrn v%++ylliw.+c !%!@(v(.!%%renim@)^v(&@riemnz@! +**x iywll&%x%_v%_x!(adrne&+).!%+)@!vx+zadenrczc*^c*% %!%.c
dnrea.@%*++c(z^.@nriemcx(z erinmzv!&_ wlliy& ^_%&^@!cmenir!ylliw)_radne &_cvirmne(^z*.%_x! ^&.)vlilwy(+.@^x.cv+vx
wlyli.(!&%)xjet!+%_@+rnemi_*vtse(xtesxv)x@.^_xlwlyi)@+xetszv x.(z((%(!cadren^@wiyllv_& xztes+c *lilwy&!_^c_% *%*^
setv(z*)^%enrad_* nerda^_v(*_!c &+^wiyll%_z^*.v_!!_&^*enrda&_ste(^@vllwiy%))@.c)&v*z(%nerad)*%%_^+c).
liwyl**_@xcste+%__+tse c.(v@%c.^^&**wyillczc )&enirmxz).x_)v%^ednra z(vcz+*@+&v&nadre.!x zx_&vv+v@^+tejv*!_!cc
danerv(+&@tse.ylilw^&(z%*v&@@)&!^^@byet!x%%!)c.+lwlyi )+.!!(ylwli&.jte %(__x!+%%.ets)(*z c)c)cz)c(&cywlliz(
denarv+ *&z! z%x( *ste!_@(xcrnime@() v))_@c) (+%ednarx(_c%@z_vest()^vliwylzv&+%%+)% z_) est@tes+!@x%.!@z_^z%@
renad@xwlliyzeandr+yiwllz)_x*_xtes _z!c!^etj&&@set@@%%v*v**z*stexc+@wlliy&+eandr&z_z(est^+&+wliyl@ndear&&v^_
lwiyl. iylwl(+v!*)denrac&set*&%!!v_& &((@set cx__%@)^%vx(%tes.z+_z
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment