Cody

# Problem 1184. Hangman (strategy)

Solution 2771146

Submitted on 3 Aug 2020
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));

file created file created 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 C Target word OY; Step 3; Word list OY ; Guess O Target word Y; Step 4; Word list Y ; Guess Y Target word LAZY; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess Z Target word LAY; Step 2; Word list COY DOE GAE HAE LAY SIE ; Guess C Target word LAY; Step 3; Word list DOE GAE HAE LAY SIE ; Guess D Target word LAY; Step 4; Word list GAE HAE LAY SIE ; Guess G Target word LAY; Step 5; Word list HAE LAY SIE ; Guess H Target word LAY; Step 6; Word list LAY SIE ; Guess L Target word AY; Step 7; Word list AY ; Guess A Target word Y; Step 8; Word list Y ; Guess Y 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 B Target word FU; Step 3; Word list FU JA ; Guess F Target word U; Step 4; Word list U ; Guess U Target word DOZE; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess Z Target word DOE; Step 2; Word list COY DOE GAE HAE LAY SIE ; Guess C Target word DOE; Step 3; Word list DOE GAE HAE LAY SIE ; Guess D Target word OE; Step 4; Word list OE ; Guess O Target word E; Step 5; Word list E ; Guess E Target word ZERO; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess Z Target word ERO; Step 2; Word list ERO ONE ; Guess E Target word RO; Step 3; Word list RO ; Guess R Target word O; Step 4; Word list O ; Guess O 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 C Target word HAE; Step 3; Word list DOE GAE HAE LAY SIE ; Guess D Target word HAE; Step 4; Word list GAE HAE LAY SIE ; Guess G Target word HAE; Step 5; Word list HAE LAY SIE ; Guess H Target word AE; Step 6; Word list AE ; Guess A Target word E; Step 7; Word list E ; Guess E 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 C Target word SIE; Step 3; Word list DOE GAE HAE LAY SIE ; Guess D Target word SIE; Step 4; Word list GAE HAE LAY SIE ; Guess G Target word SIE; Step 5; Word list HAE LAY SIE ; Guess H Target word SIE; Step 6; Word list LAY SIE ; Guess L Target word SIE; Step 7; Word list SIE ; Guess S Target word IE; Step 8; Word list IE ; Guess I Target word E; Step 9; Word list E ; Guess E 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 B Target word U; Step 3; Word list U ; Guess U Target word GAZE; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess Z Target word GAE; Step 2; Word list COY DOE GAE HAE LAY SIE ; Guess C Target word GAE; Step 3; Word list DOE GAE HAE LAY SIE ; Guess D Target word GAE; Step 4; Word list GAE HAE LAY SIE ; Guess G Target word AE; Step 5; Word list AE ; Guess A Target word E; Step 6; Word list E ; Guess E 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 B Target word JA; Step 3; Word list FU JA ; Guess F Target word JA; Step 4; Word list JA ; Guess J Target word A; Step 5; Word list A ; Guess A Target word ZONE; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess Z Target word ONE; Step 2; Word list ERO ONE ; Guess E Target word ON; Step 3; Word list ON ; Guess O Target word N; Step 4; Word list N ; Guess N 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));

Average number of errors per word 4.413265 [Warning: Function assert has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In ScoringEngineTestPoint2 (line 30) In solutionTest (line 5)] [Warning: Function evalin has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In ScoringEngineTestPoint2 (line 30) In solutionTest (line 5)] hello hello

Error using evalin Unrecognized function or variable 'score'. Error in Test2 (line 30) assignin('caller','score',evalin('caller','score')-100+round(a.e/5*100));

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

[Warning: Function assert has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In evalin (line 3) In clearvars (line 96)] [Warning: Function evalin has the same name as a MATLAB builtin. We suggest you rename the function to avoid a potential name conflict.] [> In evalin (line 3) In clearvars (line 96)] hello hello

Unrecognized function or variable 'x'. Error in evalin (line 3) y = eval(x); Error in clearvars (line 96) evalin('caller', clearExpr);