Cody

# Problem 1184. Hangman (strategy)

Solution 820299

Submitted on 4 Feb 2016
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 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 H Target word AE; Step 3; Word list AE ; Guess A Target word E; Step 4; Word list E ; Guess E Target word FUZZ; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess B Target word FUZZ; Step 2; Word list COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess E Target word FUZZ; Step 3; Word list COZY FUZZ JAZZ LAZY ; Guess Y Target word FUZZ; Step 4; Word list FUZZ JAZZ ; Guess F Target word UZZ; Step 5; Word list UZZ ; Guess U Target word ZZ; Step 6; Word list ZZ ; Guess Z Target word DOZE; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess R Target word DOZE; Step 2; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZONE ; Guess E Target word DOZ; Step 3; Word list DOZ GAZ HAZ SIZ ZON ; Guess Z Target word DO; Step 4; Word list DO GA HA SI ; Guess D Target word O; Step 5; Word list O ; Guess O Target word LAZY; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess H Target word LAZY; Step 2; Word list BUZZ COZY DOZE FUZZ GAZE JAZZ LAZY SIZE ZERO ZONE ; Guess A Target word LZY; Step 3; Word list GZE JZZ LZY ; Guess E Target word LZY; Step 4; Word list JZZ LZY ; Guess Z Target word LY; Step 5; Word list LY ; Guess Y Target word L; Step 6; Word list L ; Guess L Target word COZY; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess E Target word COZY; Step 2; Word list BUZZ COZY FUZZ JAZZ LAZY ; Guess Z Target word COY; Step 3; Word list COY LAY ; Guess L Target word COY; Step 4; Word list COY ; Guess O Target word CY; Step 5; Word list CY ; Guess C Target word Y; Step 6; Word list Y ; Guess Y Target word BUZZ; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess Z Target word BU; Step 2; Word list BU FU JA ; Guess J Target word BU; Step 3; Word list BU FU ; Guess F Target word BU; Step 4; Word list BU ; Guess U Target word B; Step 5; Word list B ; Guess B Target word GAZE; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess G Target word AZE; Step 2; Word list AZE ; Guess E Target word AZ; Step 3; Word list AZ ; Guess Z Target word A; Step 4; Word list A ; Guess A Target word ZERO; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess E Target word ZRO; Step 2; Word list ZRO ; Guess O Target word ZR; Step 3; Word list ZR ; Guess R Target word Z; Step 4; Word list Z ; Guess Z Target word JAZZ; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess Z Target word JA; Step 2; Word list BU FU JA ; Guess U Target word JA; Step 3; Word list JA ; Guess A Target word J; Step 4; Word list J ; Guess J Target word ZONE; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess A Target word ZONE; Step 2; Word list BUZZ COZY DOZE FUZZ SIZE ZERO ZONE ; Guess Z Target word ONE; Step 3; Word list ERO ONE ; Guess R Target word ONE; Step 4; Word list ONE ; Guess O Target word NE; Step 5; Word list NE ; Guess E Target word N; Step 6; Word list N ; Guess N Target word SIZE; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess N Target word SIZE; Step 2; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ; Guess E Target word SIZ; Step 3; Word list DOZ GAZ HAZ SIZ ; Guess I Target word SZ; Step 4; Word list SZ ; Guess Z Target word S; Step 5; Word list S ; Guess S Average number of errors per word 1.272727

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

Average number of errors per word 5.882653

Assertion failed.

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

Average number of errors per word 4.815000

Undefined function or variable 'score'.