Cody

Problem 1184. Hangman (strategy)

Solution 189906

Submitted on 11 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   Pass
%% 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 GAZE; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess I Target word GAZE; Step 2; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY ZERO ZONE ; Guess Z Target word GAE; Step 3; Word list COY DOE GAE HAE LAY ; Guess Y Target word GAE; Step 4; Word list DOE GAE HAE ; Guess H Target word GAE; Step 5; Word list DOE GAE ; Guess A Target word GE; Step 6; Word list GE ; Guess E Target word G; Step 7; Word list G ; Guess G Target word COZY; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess Z Target word COY; Step 2; Word list COY DOE GAE HAE LAY SIE ; Guess L Target word COY; Step 3; Word list COY DOE GAE HAE SIE ; Guess Y Target word CO; Step 4; Word list CO ; Guess O Target word C; Step 5; Word list C ; Guess C Target word HAZE; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess Z Target word HAE; Step 2; Word list COY DOE GAE HAE LAY SIE ; Guess L Target word HAE; Step 3; Word list COY DOE GAE HAE SIE ; Guess E Target word HA; Step 4; Word list DO GA HA SI ; Guess G Target word HA; Step 5; Word list DO HA SI ; Guess D Target word HA; Step 6; Word list HA SI ; Guess A Target word H; Step 7; Word list H ; Guess H Target word FUZZ; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess Z Target word FU; Step 2; Word list BU FU JA ; Guess U Target word F; Step 3; Word list B F ; Guess F Target word DOZE; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess B Target word DOZE; Step 2; Word list COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess A Target word DOZE; Step 3; Word list COZY DOZE FUZZ SIZE ZERO ZONE ; Guess Z Target word DOE; Step 4; Word list COY DOE SIE ; Guess E Target word DO; Step 5; Word list DO SI ; Guess O Target word D; Step 6; Word list D ; Guess D Target word ZONE; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess E Target word ZON; Step 2; Word list DOZ GAZ HAZ SIZ ZON ; Guess Z Target word ON; Step 3; Word list ON ; Guess O Target word N; Step 4; Word list N ; Guess N Target word LAZY; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess B Target word LAZY; Step 2; Word list COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess Z Target word LAY; Step 3; Word list COY DOE GAE HAE LAY SIE ; Guess Y Target word LA; Step 4; Word list CO LA ; Guess A Target word L; Step 5; Word list L ; Guess L Target word ZERO; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess Y Target word ZERO; Step 2; Word list BUZZ DOZE FUZZ GAZE HAZE JAZZ SIZE ZERO ZONE ; Guess Z Target word ERO; Step 3; Word list ERO ONE ; Guess O Target word ER; Step 4; Word list ER ; Guess R Target word E; Step 5; Word list E ; Guess E Target word BUZZ; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess H Target word BUZZ; Step 2; Word list BUZZ COZY DOZE FUZZ GAZE JAZZ LAZY SIZE ZERO ZONE ; Guess O Target word BUZZ; Step 3; Word list BUZZ FUZZ GAZE JAZZ LAZY SIZE ; Guess G Target word BUZZ; Step 4; Word list BUZZ FUZZ JAZZ LAZY SIZE ; Guess Z Target word BU; Step 5; Word list BU FU JA ; Guess U Target word B; Step 6; Word list B F ; Guess F Target word B; Step 7; Word list B ; Guess B Target word JAZZ; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess R Target word JAZZ; Step 2; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZONE ; Guess A Target word JZZ; Step 3; Word list GZE HZE JZZ LZY ; Guess L Target word JZZ; Step 4; Word list GZE HZE JZZ ; Guess Z Target word J; Step 5; Word list J ; Guess J Target word SIZE; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess Z Target word SIE; Step 2; Word list COY DOE GAE HAE LAY SIE ; Guess E Target word SI; Step 3; Word list DO GA HA SI ; Guess G Target word SI; Step 4; Word list DO HA SI ; Guess S Target word I; Step 5; Word list I ; Guess I Average number of errors per word 1.636364 ```

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: Assertion failed. ```

3   Pass
%% 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));

```Average number of errors per word 4.835000 ```