Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Solution 188627

Submitted on 9 Jan 2013

Incorrect

300Size
Leading solution size is 144.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test
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: Undefined function or variable 'letters'.
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: Undefined function or variable 'letters'.
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: Undefined function or variable 'letters'.