Cody

# Problem 1184. Hangman (strategy)

Solution 1264355

Submitted on 7 Sep 2017
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

### Test Suite

Test Status Code Input and Output
1   Fail
assignin('caller','score',300); a.words0={'BUZZ','COZY','DOZE','FUZZ','GAZE','HAZE','JAZZ','LAZY','SIZE','ZERO','ZONE'}; a.e=0; for n=randperm(numel(a.words0)) a.i=n; a.words=a.words0; a.n=n; for m=1:100, a.m=m; assignin('caller','a',a); letter=hangman(a.words); letter=char(letter(1)); a=evalin('caller','a'); m=a.m; fprintf('Target word %s; Step %d; Word list %s; Guess %c\n',a.words{a.i},a.m,sprintf('%s ',a.words{:}),letter); matchedletters=a.words{a.i}==letter; if ~any(matchedletters), a.e=a.e+1; end matchedwords=find(cellfun(@(x)isequal(matchedletters,x==letter),a.words)); a.i=find(matchedwords==a.i); a.words=regexprep(a.words(matchedwords),letter,''); nonemptywords=find(cellfun('length',a.words)>0); a.i=find(nonemptywords==a.i); if isempty(a.i), break; end end if ~isempty(a.i), error(sprintf('algorithm did not guess word after 100 steps. Last message: Target word %s; Step %d; Word list %s; Guessed letter %c\n',a.words{a.i},a.m,sprintf('%s ',a.words{:}),letter)); end n=a.n; end a.e=a.e/numel(a.words0); fprintf('Average number of errors per word %f\n',a.e); assert(a.e<5); assignin('caller','score',evalin('caller','score')-100+round(a.e/5*100));

Target word ZERO; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess S Target word ZERO; Step 2; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 3; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 4; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 5; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 6; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 7; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 8; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 9; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 10; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 11; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 12; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 13; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 14; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 15; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 16; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 17; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 18; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 19; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 20; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 21; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 22; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 23; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 24; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 25; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 26; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 27; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 28; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 29; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 30; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 31; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 32; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 33; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 34; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 35; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 36; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 37; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 38; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 39; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 40; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 41; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 42; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 43; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 44; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 45; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 46; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 47; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 48; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 49; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 50; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 51; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 52; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 53; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 54; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 55; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 56; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 57; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 58; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 59; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 60; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 61; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 62; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 63; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 64; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 65; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 66; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 67; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 68; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 69; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 70; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 71; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 72; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 73; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 74; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 75; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 76; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 77; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 78; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 79; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 80; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 81; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 82; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 83; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 84; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 85; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 86; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 87; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 88; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 89; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 90; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 91; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 92; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 93; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 94; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 95; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 96; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 97; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 98; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 99; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S Target word ZERO; Step 100; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess S

algorithm did not guess word after 100 steps. Last message: Target word ZERO; Step 100; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guessed letter S

2   Fail
rng default; a.words0=cellstr(unique(char('A'+ceil(26*rand([200,3]).^2)-1),'rows'))'; a.e=0; for n=randperm(numel(a.words0)) a.i=n; a.words=a.words0; a.n=n; for m=1:100, a.m=m; assignin('caller','a',a); letter=hangman(a.words); letter=char(letter(1)); a=evalin('caller','a'); m=a.m; matchedletters=a.words{a.i}==letter; if ~any(matchedletters), a.e=a.e+1; end matchedwords=find(cellfun(@(x)isequal(matchedletters,x==letter),a.words)); a.i=find(matchedwords==a.i); a.words=regexprep(a.words(matchedwords),letter,''); nonemptywords=find(cellfun('length',a.words)>0); a.i=find(nonemptywords==a.i); if isempty(a.i), break; end end if ~isempty(a.i), error(sprintf('algorithm did not guess word after 100 steps. Last message: Target word %s; Step %d; Word list %s; Guessed letter %c\n',a.words{a.i},a.m,sprintf('%s ',a.words{:}),letter)); end n=a.n; end a.e=a.e/numel(a.words0); fprintf('Average number of errors per word %f\n',a.e); assert(a.e<5); assignin('caller','score',evalin('caller','score')-100+round(a.e/5*100));

algorithm did not guess word after 100 steps. Last message: Target word AAP; Step 100; Word list AAA AAB AAC AAE AAF AAG AAH AAI AAP AAT AAY ABA ABB ABE ABH ABL ABX ACH ADE AEL AEM AEO AET AGA AGB AGF AGK AHC AHW AME AMO ANX AQH ARD ARJ ATB AWD AWE AWM AXF AZE BCH BHB BJK BKQ BLA BLP BNV BPC BRW BUM BUN BUU BYH BYV BZA BZD BZH CAC CAG CAT CCL CCU CFJ CJN CTK DAH DAI DAK DBB DDX DEQ DIE DNO DQJ DVI DZB EBA EBH ECM ECO EEY ELD FAA FAB FBK FGL FIZ FJL FLH GBR GCG GEA GKP GLA GMF GNP GOB GRA GVF HAB HBG HCA HEB HFX HGO HHA HUP IAD IBL IDF IGK IIF JAA JME KDM KDR KDT KIA KJP KWA LBF LCP LHF LRK LVA MCC MHA MJG MNF MQA MRB NBA NBJ OAA OAK OFD OFF OJB OJI OTA OYC PAD PAR PIQ PQL PQP PUA QAA QBA QKA QMH RAA RAC RAK RAL RFZ RGA RKE ROL TDE TEP UAD VAC VAE VDA VEA VHF VKN VLM VVB WAB WGL WNA WVH XBJ XEJ XEM XFE XFQ XHM XKA XLC XZO YCL YDF YIC ZRA ; Guessed letter S

3   Fail
rng default; a.words0=cellstr(unique(char('A'+ceil(26*rand([200,4]).^2)-1),'rows'))'; a.e=0; for n=randperm(numel(a.words0)) a.i=n; a.words=a.words0; a.n=n; for m=1:100, a.m=m; assignin('caller','a',a); letter=hangman(a.words); letter=char(letter(1)); a=evalin('caller','a'); m=a.m; matchedletters=a.words{a.i}==letter; if ~any(matchedletters), a.e=a.e+1; end matchedwords=find(cellfun(@(x)isequal(matchedletters,x==letter),a.words)); a.i=find(matchedwords==a.i); a.words=regexprep(a.words(matchedwords),letter,''); nonemptywords=find(cellfun('length',a.words)>0); a.i=find(nonemptywords==a.i); if isempty(a.i), break; end end if ~isempty(a.i), error(sprintf('algorithm did not guess word after 100 steps. Last message: Target word %s; Step %d; Word list %s; Guessed letter %c\n',a.words{a.i},a.m,sprintf('%s ',a.words{:}),letter)); end n=a.n; end a.e=a.e/numel(a.words0); fprintf('Average number of errors per word %f\n',a.e); assert(a.e<5); assignin('caller','score',evalin('caller','score')-100+round(a.e/5*100));

algorithm did not guess word after 100 steps. Last message: Target word DAIB; Step 100; Word list AAAR AABE AABP AACA AAEA AAEC AAFA AAGA AAHO AAII AAPV AATI AAYB ABAA ABBA ABEK ABHJ ABLA ABXI ACHB ADEH AELL AEMA AEOA AETZ AGAB AGBA AGFC AGFJ AGKH AHCA AHWB AMEA AMOK ANXI AQHC ARDA ARJW ATBC AWDD AWEI AWMR AXFB AZEC BCHA BHBU BJKW BKQO BLAL BLPA BNVB BPCA BRWL BUMI BUNG BUUA BYHM BYVA BZAF BZDC BZHO CACC CAGA CATD CCLG CCUI CJNT CTKU DAHN DAIB DAKO DBBK DDXA DEQG DIEA DNOJ DQJA DVIK DZBG EBAF EBAQ EBHG ECMV ECOF EEYV ELDP FAAA FABH FBKN FGLG FIZC FJLO FLHZ GBRF GCGA GEAA GKPH GLAA GMFC GNPA GOBA GRAU HABF HBGM HCAI HEBF HFXW HGOA HHAZ HUPW IADC IBLC IDFH IGKT IIFE JAAA JMEW KDMI KDRO KDTP KIAA KJPN KWAA LBFU LCPO LHFA LRKC LVAA MCCT MHAA MJGP MNFU MQAT MRBP NBAN NBJA OAAA OAKA OFDB OFFL OJBQ OJIE OTAG OYCK PADC PARK PIQA PQLA PQPH PUAG QAAX QBAI QKAF RAAQ RACA RAKC RALA RFZE RGAA ROLA TDEJ TEPA UADB VACV VAEA VDAI VEAA VHFM VKNZ VLMZ VVBZ WABL WGLC WNAA WVHH XBJB XFEH XFQA XHMC XKAA XLCM XZOR YCLZ YDFD YICT ZRAP ; Guessed letter S