Cody

# Problem 1184. Hangman (strategy)

Solution 188628

Submitted on 9 Jan 2013
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));

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

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));

```Error: 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 AAS 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 FNS GBR GCG GEA GKP GLA GMF GNP GOB GRA GVF HAB HBG HCA HEB HFX HGO HHA HUP IAD IBL IDF IGK IIF IYS 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 QAS QBA QKA QMH QSN RAA RAC RAK RAL RFZ RGA RKE ROL SAB SEA SXD 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 ÃÂ® ```

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));

```Error: 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 AASH 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 CFJS 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 FNSK GBRF GCGA GEAA GKPH GLAA GMFC GNPA GOBA GRAU GVFS HABF HBGM HCAI HEBF HFXW HGOA HHAZ HUPW IADC IBLC IDFH IGKT IIFE IYSF 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 QASJ QBAI QKAF QMHS QSNQ RAAQ RACA RAKC RALA RFZE RGAA RKES ROLA SABX SEAB SXDU TDEJ TEPA UADB VACV VAEA VDAI VEAA VHFM VKNZ VLMZ VVBZ WABL WGLC WNAA WVHH XBJB XEJS XEMS XFEH XFQA XHMC XKAA XLCM XZOR YCLZ YDFD YICT ZRAP ; Guessed letter Â ```