From 4fa45dec7e940fe93f664e40808e372e4ad272e1 Mon Sep 17 00:00:00 2001 From: Neil Smith Date: Fri, 27 Nov 2020 13:36:22 +0000 Subject: [PATCH] Fixed wordbreaks in the plaintext --- 2020/2020-challenge5.ipynb | 460 ++++++++++++++++++++++++++++++++++--- 2020/2020-challenge5.md | 99 +++++++- 2020/plaintext.5b.txt | 10 +- 3 files changed, 537 insertions(+), 32 deletions(-) diff --git a/2020/2020-challenge5.ipynb b/2020/2020-challenge5.ipynb index 0023d1c..363693c 100644 --- a/2020/2020-challenge5.ipynb +++ b/2020/2020-challenge5.ipynb @@ -117,7 +117,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 4, "metadata": { "Collapsed": "false" }, @@ -183,7 +183,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 5, "metadata": { "Collapsed": "false" }, @@ -208,7 +208,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 6, "metadata": { "Collapsed": "false" }, @@ -246,7 +246,7 @@ }, { "cell_type": "code", - "execution_count": 27, + "execution_count": 87, "metadata": { "Collapsed": "false" }, @@ -254,10 +254,10 @@ { "data": { "text/plain": [ - "1782" + "1783" ] }, - "execution_count": 27, + "execution_count": 87, "metadata": {}, "output_type": "execute_result" } @@ -268,7 +268,47 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": 86, + "metadata": { + "Collapsed": "false" + }, + "outputs": [], + "source": [ + "pb = \"\"\"dear uncle wilhelm as you promised we thoroughly enjoyed our visit to your cousin in lincoln and\n", + "found it most informative stop the beautiful cathedral almost justified our visit on its own and our\n", + "hosts shared with us some interesting drawings of the towers you mentioned stop they graciously\n", + "allowed me to copy the sketches and explained much about how the towers are built and why stop as\n", + "usual i will send you my notes and sketches via our dear friend jessica who has promised to ensure\n", + "their safe delivery stop really i must congratulate our hosts in the local scout groups for the\n", + "marvellous way in which they have organised our travel though some of the scouts have been rather\n", + "more solicitous than we had expected asking rather a lot of questions about our plans stop generally\n", + "i hope they were satisfied with the answers that we gave but we feel that we are at risk of\n", + "overstaying our welcome so we will return to london on tuesday stop even though we have enjoyed our\n", + "time together there has been some debate among the group about where we should visit next stop there\n", + "are so many interesting sites to visit along the majestic thames estuary so we have decided to split\n", + "into two groups stop ralf will lead one party on a tour of the kent coast while i am very much\n", + "looking forward to exploring the essex marshes stop do let me know if you have a better idea but i\n", + "have been told that canewdons fifteenth century church affords an elevation with a wide view of\n", + "the surrounding lands in this otherwise rather flat landscape and is the site of another fascinating\n", + "tower which i will be certain to sketch for you stop finally if you have any further requests for\n", + "specific information then perhaps you could leave a message for me at the post office there stop karl\n", + "message ends\"\"\"" + ] + }, + { + "cell_type": "code", + "execution_count": 88, + "metadata": { + "Collapsed": "false" + }, + "outputs": [], + "source": [ + "spb = sanitise(pb)" + ] + }, + { + "cell_type": "code", + "execution_count": 89, "metadata": { "Collapsed": "false" }, @@ -281,7 +321,7 @@ " 'as',\n", " 'really',\n", " 'generally',\n", - " 'eventhough',\n", + " 'even',\n", " 'there',\n", " 'ralf',\n", " 'do',\n", @@ -289,7 +329,7 @@ " 'karl']" ] }, - "execution_count": 23, + "execution_count": 89, "metadata": {}, "output_type": "execute_result" } @@ -301,7 +341,7 @@ }, { "cell_type": "code", - "execution_count": 35, + "execution_count": 90, "metadata": { "Collapsed": "false" }, @@ -309,21 +349,21 @@ { "data": { "text/plain": [ - "'duwaypwteovtycilafimistbcajovoioaohswusidottymstgamtctsaemahttabawsauiwsymnasvodfjwhptetsdsrimcohitlsgftmwiwthoottsotshbrmstwhearaoqaopsgihtwswtatwgbwftwaarooowswwrtlotsewheottthbsdatgawwsvnstasmistvatmteswhdtsitgsrwlopoaootkcwiavmlftetemsdlmkiyhabibihbttcndfccasewawvotslitorflaitsoaftwiwbctsfysfiyhafrfsitpyclamfmtpotskme'" + "'duwaypwteovtycilafim is tbcajovoioaohswusi dotty mst ga mtct sae mah ttab aws a uiwsymnasvodfjwhptet sds rim co hit lsgftmwiwthoottsotsh brms tw hear al oqaopsgihtwswtatwgbw ft waar ooo wsw wrt lot set wheottthbsdatgawwsvn st as mist vat mteswhdtsitgsrwlopoa to tk cwiavmlftetemsdlmkiy habibi hbttcfccaaewawvotsl it or fla its oaftwiwbctsfysfiyhaf rfs it py clam fm at pots k me'" ] }, - "execution_count": 35, + "execution_count": 90, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "cat(w[0] for w in pbs)" + "wcat(segment(cat(w[0] for w in pbs)))" ] }, { "cell_type": "code", - "execution_count": 26, + "execution_count": 91, "metadata": { "Collapsed": "false" }, @@ -331,10 +371,76 @@ { "data": { "text/plain": [ - "[21, 24, 19, 23, 44, 32, 21, 22, 24, 56, 23, 3]" + "'en is or eh nui oo on i not on the a luu in twn uo his on rfhooethrleoohknxu boho run hts sie oy on kiuereharonhaeteuou on hoc roh a an hharurhofhcaeaooheax sa of ubulteoheaihnheauee her tif vue oeieoonutvheanuiohae oem hrb he hie thro an i oil hahsoeaeopnwrtaien an of he ohm eu ooo x hsa to eenfoaeduaeohaiehfnl ii if huan ht alan sh if naoh i eeo koot if oanueopnheooeeoeth of hta en'" ] }, - "execution_count": 26, + "execution_count": 91, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "wcat(segment(cat(w[1] for w in pbs if len(w) > 1)))" + ] + }, + { + "cell_type": "code", + "execution_count": 92, + "metadata": { + "Collapsed": "false" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'sel pee teter eaedusnncrsryeufypu rhone li hr gr fees de tres ns gef we a hnnshyhstdneitaraeuf we top sxe god ghy miette fr any ed lfp soo to deeoyscegtosgyoeeptt deet peg een ser erde ehnpynnonleoergfkte et let eet sehdeyeiypsrtsftargd denser nesefehlrdeyhnyserst lens reti ypyereodsoadrrasdsyu d lil spy dtesewthddseyoedyypd use fsgeshdsrdnsntrdtlle pet tdd nnn rotr dyed us mer'" + ] + }, + "execution_count": 92, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "wcat(segment(cat(w[-1] for w in reversed(pbs))))" + ] + }, + { + "cell_type": "code", + "execution_count": 93, + "metadata": { + "Collapsed": "false" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "12" + ] + }, + "execution_count": 93, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "max([len(w) for w in pbs])" + ] + }, + { + "cell_type": "code", + "execution_count": 94, + "metadata": { + "Collapsed": "false" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[21, 24, 19, 23, 45, 32, 22, 22, 24, 54, 24, 3]" + ] + }, + "execution_count": 94, "metadata": {}, "output_type": "execute_result" } @@ -345,7 +451,7 @@ }, { "cell_type": "code", - "execution_count": 30, + "execution_count": 95, "metadata": { "Collapsed": "false" }, @@ -353,10 +459,10 @@ { "data": { "text/plain": [ - "'DeaR uncle wilhelm as you pRomiseD we thoRoughly enjoyeD ouR visit to youR cousin in lincoln anD FounD it most inFoRmative stop the beautiFul catheDRal almost justiFieD ouR visit on its own anD ouR hosts shaReD with us some inteResting DRawings oF the toweRs you mentioneD stop they gRaciously alloweD me to copy the sketches anD explaineD much about how the toweRs aRe built anD why stop as usual i will senD you my notes anD sketches via ouR DeaR FRienD jessica who has pRomiseD to ensuRe theiR saFe DeliveRy stop Really i must congRatulate ouR hosts in the local scout gRoups FoR the maRvellous way in which they have oRganiseD ouR tRavel though some oF the scouts have been RatheR moRe solicitous than we haD expecteD asking RatheR alot oF questions about ouR plans stop geneRally i hope they weRe satisFieD with the answeRs that we gave but we Feel that we aRe at Risk oF oveRstaying ouR welcome so we will RetuRn to lonDon on tuesDay stop eventhough we have enjoyeD ouR time togetheR theRe has been some Debate among the gRoup about wheRe we shoulD visit next stop theRe aRe so many inteResting sites to visit along the majestic thames estuaRy so we have DeciDeD to split into two gRoups stop RalF will leaD one paRty on at ouR oF the kent coast while i am veRy much looking FoRwaRD to exploRing the essex maRshes stop Do let me know iF you have a betteR iDea but i have been tolD that ca new Dons FiFteenth centuRy chuRch aFFoRD san elevation with a wiDe view oF the suRRounDing lanDs in this otheRwise RatheR Flat lanDscape anD is the site oF anotheR Fascinating toweR which i will be ceRtain to sketch FoR you stop Finally iF you have any FuRtheR Requests FoR speciFic inFoRmation then peRhaps you coulD leave a message FoR meat the post oFFice theRe stop kaRl message enDs'" + "'DeaR uncle wilhelm as you pRomiseD we thoRoughly enjoyeD ouR visit to youR cousin in lincoln anD FounD it most inFoRmative stop the beautiFul catheDRal almost justiFieD ouR visit on its own anD ouR hosts shaReD with us some inteResting DRawings oF the toweRs you mentioneD stop they gRaciously alloweD me to copy the sketches anD explaineD much about how the toweRs aRe built anD why stop as usual i will senD you my notes anD sketches via ouR DeaR FRienD jessica who has pRomiseD to ensuRe theiR saFe DeliveRy stop Really i must congRatulate ouR hosts in the local scout gRoups FoR the maRvellous way in which they have oRganiseD ouR tRavel though some oF the scouts have been RatheR moRe solicitous than we haD expecteD asking RatheR a lot oF questions about ouR plans stop geneRally i hope they weRe satisFieD with the answeRs that we gave but we Feel that we aRe at Risk oF oveRstaying ouR welcome so we will RetuRn to lonDon on tuesDay stop even though we have enjoyeD ouR time togetheR theRe has been some Debate among the gRoup about wheRe we shoulD visit next stop theRe aRe so many inteResting sites to visit along the majestic thames estuaRy so we have DeciDeD to split into two gRoups stop RalF will leaD one paRty on a touR oF the kent coast while i am veRy much looking FoRwaRD to exploRing the essex maRshes stop Do let me know iF you have a betteR iDea but i have been tolD that canewDons FiFteenth centuRy chuRch aFFoRDs an elevation with a wiDe view oF the suRRounDing lanDs in this otheRwise RatheR Flat lanDscape anD is the site oF anotheR Fascinating toweR which i will be ceRtain to sketch FoR you stop Finally iF you have any FuRtheR Requests FoR speciFic inFoRmation then peRhaps you coulD leave a message FoR me at the post oFFice theRe stop kaRl message enDs'" ] }, - "execution_count": 30, + "execution_count": 95, "metadata": {}, "output_type": "execute_result" } @@ -364,12 +470,13 @@ "source": [ "tt = ''.maketrans('rdf', 'RDF')\n", "pbcap = wcat(pb.split()).translate(tt)\n", + "spbcap = wcat(spb.split()).translate(tt)\n", "pbcap" ] }, { "cell_type": "code", - "execution_count": 33, + "execution_count": 96, "metadata": { "Collapsed": "false" }, @@ -448,7 +555,7 @@ "D expecte\n", "D asking \n", "Rathe\n", - "R alot o\n", + "R a lot o\n", "F questions about ou\n", "R plans stop gene\n", "Rally i hope they we\n", @@ -465,7 +572,7 @@ "Retu\n", "Rn to lon\n", "Don on tues\n", - "Day stop eventhough we have enjoye\n", + "Day stop even though we have enjoye\n", "D ou\n", "R time togethe\n", "R the\n", @@ -485,7 +592,7 @@ "Ral\n", "F will lea\n", "D one pa\n", - "Rty on at ou\n", + "Rty on a tou\n", "R o\n", "F the kent coast while i am ve\n", "Ry much looking \n", @@ -499,7 +606,7 @@ "F you have a bette\n", "R i\n", "Dea but i have been tol\n", - "D that ca new \n", + "D that canew\n", "Dons \n", "Fi\n", "Fteenth centu\n", @@ -508,7 +615,7 @@ "F\n", "Fo\n", "R\n", - "D san elevation with a wi\n", + "Ds an elevation with a wi\n", "De view o\n", "F the su\n", "R\n", @@ -542,7 +649,7 @@ "Rhaps you coul\n", "D leave a message \n", "Fo\n", - "R meat the post o\n", + "R me at the post o\n", "F\n", "Fice the\n", "Re stop ka\n", @@ -556,6 +663,307 @@ "print(pbcap2)" ] }, + { + "cell_type": "code", + "execution_count": 97, + "metadata": { + "Collapsed": "false" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'dearu nclew ilhel masyo uprom isedw ethor oughl yenjo yedou rvisi ttoyo urcou sinin linco lnand found itmos tinfo rmati vesto ptheb eauti fulca thedr alalm ostju stifi edour visit onits ownan dourh ostss hared withu ssome inter estin gdraw ingso fthet owers youme ntion edsto pthey graci ously allow edmet ocopy thesk etche sande xplai nedmu chabo uthow theto wersa rebui ltand whyst opasu suali wills endyo umyno tesan dsket chesv iaour dearf riend jessi cawho haspr omise dtoen suret heirs afede liver ystop reall yimus tcong ratul ateou rhost sinth eloca lscou tgrou psfor thema rvell ouswa yinwh ichth eyhav eorga nised ourtr avelt hough someo fthes couts haveb eenra therm oreso licit ousth anweh adexp ected askin grath eralo tofqu estio nsabo utour plans stopg enera llyih opeth eywer esati sfied witht heans werst hatwe gaveb utwef eelth atwea reatr iskof overs tayin gourw elcom esowe willr eturn tolon donon tuesd aysto peven thoug hweha veenj oyedo urtim etoge thert hereh asbee nsome debat eamon gtheg roupa boutw herew eshou ldvis itnex tstop there areso manyi ntere sting sites tovis italo ngthe majes ticth amese stuar ysowe haved ecide dtosp litin totwo group sstop ralfw illle adone party onato uroft heken tcoas twhil eiamv erymu chloo kingf orwar dtoex plori ngthe essex marsh essto pdole tmekn owify ouhav eabet terid eabut ihave beent oldth atcan ewdon sfift eenth centu rychu rchaf fords anele vatio nwith awide viewo fthes urrou nding lands inthi sothe rwise rathe rflat lands capea ndist hesit eofan other fasci natin gtowe rwhic hiwil lbece rtain toske tchfo ryous topfi nally ifyou havea nyfur therr eques tsfor speci ficin forma tiont henpe rhaps youco uldle aveam essag eform eatth epost offic ether estop karlm essag eends'" + ] + }, + "execution_count": 97, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "wcat(chunks(spb, 5))" + ] + }, + { + "cell_type": "code", + "execution_count": 98, + "metadata": { + "Collapsed": "false" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "'uwlomwrlouiounoddsoiobiarmuirtsnhsduernwotsenoyiywtykeeiuowoaidtuisoontvrfdiorentserplsgluthauuralahhvadrthossbamothhpdnhouoorsgahhridtstebfharfsnwmernnndongajometheetngawwusxpeoiegssoesheredepnoppweeyotnslvuofrxiexhoenyvtdtethnnthuufseoheosugsieeetsattnrinecleneosiyuarrsrinatesoemgmhtcrpmgs'" + ] + }, + "execution_count": 98, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "cat(w[-1] for w in chunks(spb, 5))" + ] + }, + { + "cell_type": "code", + "execution_count": 99, + "metadata": { + "Collapsed": "false" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "(87, 47, 35)" + ] + }, + "execution_count": 99, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "locR = [i for i, c in enumerate(spb) if c == 'r']\n", + "locD = [i for i, c in enumerate(spb) if c == 'd']\n", + "locF = [i for i, c in enumerate(spb) if c == 'f']\n", + "len(locR), len(locD), len(locF)" + ] + }, + { + "cell_type": "code", + "execution_count": 100, + "metadata": { + "Collapsed": "false" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[(3, 586, 1169, 583),\n", + " (34, 350, 666, 316),\n", + " (50, 519, 988, 469),\n", + " (124, 395, 666, 271),\n", + " (163, 769, 1375, 606),\n", + " (197, 337, 477, 140),\n", + " (305, 337, 369, 32),\n", + " (368, 769, 1170, 401),\n", + " (370, 866, 1362, 496),\n", + " (419, 830, 1241, 411),\n", + " (440, 769, 1098, 329),\n", + " (450, 798, 1146, 348),\n", + " (485, 958, 1431, 473),\n", + " (512, 830, 1148, 318),\n", + " (522, 594, 666, 72),\n", + " (558, 960, 1362, 402),\n", + " (563, 866, 1169, 303),\n", + " (976, 1173, 1370, 197),\n", + " (1002, 1086, 1170, 84),\n", + " (1011, 1193, 1375, 182),\n", + " (1160, 1248, 1336, 88),\n", + " (1165, 1256, 1347, 91)]" + ] + }, + "execution_count": 100, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "triples = []\n", + "for r in locR:\n", + " for d in locD:\n", + " for f in locF:\n", + " if d > r:\n", + " if d - r == f - d:\n", + " triples += [(r, d, f, d - r)]\n", + "triples" + ] + }, + { + "cell_type": "code", + "execution_count": 101, + "metadata": { + "Collapsed": "false" + }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "dearunclewilhelma s youpromisedwet\n", + "horoughlyenjoyedo u rvisittoyourco\n", + "usininlincolnandf o unditmostinfor\n", + "mativestopthebeau t ifulcathedrala\n", + "lmostjustifiedour v isitonitsownan\n", + "dourhostssharedwi t hussomeinteres\n", + "tingdrawingsofthe t owersyoumentio\n", + "nedstoptheygracio u slyallowedmeto\n", + "copythesketchesan d explainedmucha\n", + "bouthowthetowersa r ebuiltandwhyst\n", + "opasusualiwillsen d youmynotesands\n", + "ketchesviaourdear f riendjessicawh\n", + "ohaspromisedtoens u retheirsafedel\n", + "iverystopreallyim u stcongratulate\n", + "ourhostsintheloca l scoutgroupsfor\n", + "themarvellouswayi n whichtheyhaveo\n", + "rganisedourtravel t houghsomeofthe\n", + "scoutshavebeenrat h ermoresolicito\n", + "usthanwehadexpect e daskingrathera\n", + "lotofquestionsabo u tourplansstopg\n", + "enerallyihopethey w eresatisfiedwi\n", + "ththeanswersthatw e gavebutwefeelt\n", + "hatweareatriskofo v erstayingourwe\n", + "lcomesowewillretu r ntolondonontue\n", + "sdaystopeventhoug h wehaveenjoyedo\n", + "urtimetogetherthe r ehasbeensomede\n", + "bateamongthegroup a boutwherewesho\n", + "uldvisitnextstopt h erearesomanyin\n", + "terestingsitestov i sitalongthemaj\n", + "esticthamesestuar y sowehavedecide\n", + "dtosplitintotwogr o upsstopralfwil\n", + "lleadonepartyonat o urofthekentcoa\n", + "stwhileiamverymuc h lookingforward\n", + "toexploringtheess e xmarshesstopdo\n", + "letmeknowifyouhav e abetterideabut\n", + "ihavebeentoldthat c anewdonsfiftee\n", + "nthcenturychurcha f fordsanelevati\n", + "onwithawideviewof t hesurroundingl\n", + "andsinthisotherwi s eratherflatlan\n", + "dscapeandisthesit e ofanotherfasci\n", + "natingtowerwhichi w illbecertainto\n", + "sketchforyoustopf i nallyifyouhave\n", + "anyfurtherrequest s forspecificinf\n", + "ormationthenperha p syoucouldleave\n", + "amessageformeatth e postofficether\n", + "estopkarlmessagee n ds\n" + ] + } + ], + "source": [ + "t = cat(c if c.isupper() else '.' for c in pbscap)\n", + "col = 305 % 32\n", + "tcs = chunks(spb, 32)\n", + "stcs = [wcat([c[:col], c[col], c[col+1:]]) for c in tcs]\n", + "print(lcat(stcs))" + ] + }, + { + "cell_type": "code", + "execution_count": 102, + "metadata": { + "Collapsed": "false" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "780" + ] + }, + "execution_count": 102, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "results = []\n", + "for l in range(1, 40):\n", + " for i in range(l):\n", + " cs = chunks(spb, l, fillvalue=' ')\n", + " t = cat(c[i] for c in cs)\n", + " f = Ptrigrams(t)\n", + " results += [{'l': l, 'i': i, 't': t, 'f': f * l}]\n", + "len(results)" + ] + }, + { + "cell_type": "code", + "execution_count": 103, + "metadata": { + "Collapsed": "false" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'l': 39,\n", + " 'i': 37,\n", + " 't': 'gnfsihalnpaseseasemerenhworttaragsods ',\n", + " 'f': -5279.032049169729},\n", + " {'l': 38,\n", + " 'i': 35,\n", + " 't': 'ocatteseoslsuteerefloatsoralycanaepsls ',\n", + " 'f': -5213.417095225861},\n", + " {'l': 39,\n", + " 'i': 24,\n", + " 't': 'mosdsepenjessoloitatmeegdemraceshotes ',\n", + " 'f': -5197.69063368185},\n", + " {'l': 1,\n", + " 'i': 0,\n", + " 't': 'dearunclewilhelmasyoupromisedwethoroughlyenjoyedourvisittoyourcousininlincolnandfounditmostinformativestopthebeautifulcathedralalmostjustifiedourvisitonitsownandourhostssharedwithussomeinterestingdrawingsofthetowersyoumentionedstoptheygraciouslyallowedmetocopythesketchesandexplainedmuchabouthowthetowersarebuiltandwhystopasusualiwillsendyoumynotesandsketchesviaourdearfriendjessicawhohaspromisedtoensuretheirsafedeliverystopreallyimustcongratulateourhostsinthelocalscoutgroupsforthemarvellouswayinwhichtheyhaveorganisedourtravelthoughsomeofthescoutshavebeenrathermoresolicitousthanwehadexpectedaskingratheralotofquestionsaboutourplansstopgenerallyihopetheyweresatisfiedwiththeanswersthatwegavebutwefeelthatweareatriskofoverstayingourwelcomesowewillreturntolondonontuesdaystopeventhoughwehaveenjoyedourtimetogethertherehasbeensomedebateamongthegroupaboutwhereweshouldvisitnextstoptherearesomanyinterestingsitestovisitalongthemajesticthamesestuarysowehavedecidedtosplitintotwogroupsstopralfwillleadonepartyonatourofthekentcoastwhileiamverymuchlookingforwardtoexploringtheessexmarshesstopdoletmeknowifyouhaveabetterideabutihavebeentoldthatcanewdonsfifteenthcenturychurchaffordsanelevationwithawideviewofthesurroundinglandsinthisotherwiseratherflatlandscapeandisthesiteofanotherfascinatingtowerwhichiwillbecertaintosketchforyoustopfinallyifyouhaveanyfurtherrequestsforspecificinformationthenperhapsyoucouldleaveamessageformeatthepostofficetherestopkarlmessageends',\n", + " 'f': -4923.336122800165}]" + ] + }, + "execution_count": 103, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "list(sorted(results, key=lambda r: r['f']))[-4:]" + ] + }, + { + "cell_type": "code", + "execution_count": 104, + "metadata": { + "Collapsed": "false" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "1" + ] + }, + "execution_count": 104, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rdfresults = [r for r in results if 'rdf' in r['t']]\n", + "len(rdfresults)" + ] + }, + { + "cell_type": "code", + "execution_count": 105, + "metadata": { + "Collapsed": "false" + }, + "outputs": [ + { + "data": { + "text/plain": [ + "[{'l': 32,\n", + " 'i': 17,\n", + " 't': 'suotvttudrdfuulntheuwevrhrahiyooheecftsewispen',\n", + " 'f': -6630.535098505313}]" + ] + }, + "execution_count": 105, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "rdfresults" + ] + }, { "cell_type": "code", "execution_count": null, diff --git a/2020/2020-challenge5.md b/2020/2020-challenge5.md index 55703b7..4f3b7ea 100644 --- a/2020/2020-challenge5.md +++ b/2020/2020-challenge5.md @@ -78,13 +78,51 @@ print(pb) open(plaintext_b_filename, 'w').write(pb) ``` +```python Collapsed="false" +pb = """dear uncle wilhelm as you promised we thoroughly enjoyed our visit to your cousin in lincoln and +found it most informative stop the beautiful cathedral almost justified our visit on its own and our +hosts shared with us some interesting drawings of the towers you mentioned stop they graciously +allowed me to copy the sketches and explained much about how the towers are built and why stop as +usual i will send you my notes and sketches via our dear friend jessica who has promised to ensure +their safe delivery stop really i must congratulate our hosts in the local scout groups for the +marvellous way in which they have organised our travel though some of the scouts have been rather +more solicitous than we had expected asking rather a lot of questions about our plans stop generally +i hope they were satisfied with the answers that we gave but we feel that we are at risk of +overstaying our welcome so we will return to london on tuesday stop even though we have enjoyed our +time together there has been some debate among the group about where we should visit next stop there +are so many interesting sites to visit along the majestic thames estuary so we have decided to split +into two groups stop ralf will lead one party on a tour of the kent coast while i am very much +looking forward to exploring the essex marshes stop do let me know if you have a better idea but i +have been told that canewdons fifteenth century church affords an elevation with a wide view of +the surrounding lands in this otherwise rather flat landscape and is the site of another fascinating +tower which i will be certain to sketch for you stop finally if you have any further requests for +specific information then perhaps you could leave a message for me at the post office there stop karl +message ends""" +``` + +```python Collapsed="false" +spb = sanitise(pb) +``` + ```python Collapsed="false" pbs = pb.split() [pbs[i+1] for i, w in enumerate(pbs) if w == 'stop'] ``` ```python Collapsed="false" -cat(w[0] for w in pbs) +wcat(segment(cat(w[0] for w in pbs))) +``` + +```python Collapsed="false" +wcat(segment(cat(w[1] for w in pbs if len(w) > 1))) +``` + +```python Collapsed="false" +wcat(segment(cat(w[-1] for w in reversed(pbs)))) +``` + +```python Collapsed="false" +max([len(w) for w in pbs]) ``` ```python Collapsed="false" @@ -94,6 +132,7 @@ cat(w[0] for w in pbs) ```python Collapsed="false" tt = ''.maketrans('rdf', 'RDF') pbcap = wcat(pb.split()).translate(tt) +spbcap = wcat(spb.split()).translate(tt) pbcap ``` @@ -102,6 +141,64 @@ pbcap2 = cat(('\n' + c) if c.isupper() else c for c in pbcap) print(pbcap2) ``` +```python Collapsed="false" +wcat(chunks(spb, 5)) +``` + +```python Collapsed="false" +cat(w[-1] for w in chunks(spb, 5)) +``` + +```python Collapsed="false" +locR = [i for i, c in enumerate(spb) if c == 'r'] +locD = [i for i, c in enumerate(spb) if c == 'd'] +locF = [i for i, c in enumerate(spb) if c == 'f'] +len(locR), len(locD), len(locF) +``` + +```python Collapsed="false" +triples = [] +for r in locR: + for d in locD: + for f in locF: + if d > r: + if d - r == f - d: + triples += [(r, d, f, d - r)] +triples +``` + +```python Collapsed="false" +t = cat(c if c.isupper() else '.' for c in pbscap) +col = 305 % 32 +tcs = chunks(spb, 32) +stcs = [wcat([c[:col], c[col], c[col+1:]]) for c in tcs] +print(lcat(stcs)) +``` + +```python Collapsed="false" +results = [] +for l in range(1, 40): + for i in range(l): + cs = chunks(spb, l, fillvalue=' ') + t = cat(c[i] for c in cs) + f = Ptrigrams(t) + results += [{'l': l, 'i': i, 't': t, 'f': f * l}] +len(results) +``` + +```python Collapsed="false" +list(sorted(results, key=lambda r: r['f']))[-4:] +``` + +```python Collapsed="false" +rdfresults = [r for r in results if 'rdf' in r['t']] +len(rdfresults) +``` + +```python Collapsed="false" +rdfresults +``` + ```python Collapsed="false" ``` diff --git a/2020/plaintext.5b.txt b/2020/plaintext.5b.txt index 5ee6388..957229f 100644 --- a/2020/plaintext.5b.txt +++ b/2020/plaintext.5b.txt @@ -5,15 +5,15 @@ allowed me to copy the sketches and explained much about how the towers are buil usual i will send you my notes and sketches via our dear friend jessica who has promised to ensure their safe delivery stop really i must congratulate our hosts in the local scout groups for the marvellous way in which they have organised our travel though some of the scouts have been rather -more solicitous than we had expected asking rather alot of questions about our plans stop generally +more solicitous than we had expected asking rather a lot of questions about our plans stop generally i hope they were satisfied with the answers that we gave but we feel that we are at risk of -overstaying our welcome so we will return to london on tuesday stop eventhough we have enjoyed our +overstaying our welcome so we will return to london on tuesday stop even though we have enjoyed our time together there has been some debate among the group about where we should visit next stop there are so many interesting sites to visit along the majestic thames estuary so we have decided to split -into two groups stop ralf will lead one party on at our of the kent coast while i am very much +into two groups stop ralf will lead one party on a tour of the kent coast while i am very much looking forward to exploring the essex marshes stop do let me know if you have a better idea but i -have been told that ca new dons fifteenth century church afford san elevation with a wide view of +have been told that canewdons fifteenth century church affords an elevation with a wide view of the surrounding lands in this otherwise rather flat landscape and is the site of another fascinating tower which i will be certain to sketch for you stop finally if you have any further requests for -specific information then perhaps you could leave a message for meat the post office there stop karl +specific information then perhaps you could leave a message for me at the post office there stop karl message ends \ No newline at end of file -- 2.34.1