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 189901

Submitted on 11 Jan 2013 by Alfonso Nieto-Castanon

Correct

247Size
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
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 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 C
Target word FUZZ; Step 3; Word list DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess D
Target word FUZZ; Step 4; Word list FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess F
Target word UZZ; Step 5; Word list UZZ ; Guess U
Target word ZZ; Step 6; Word list ZZ ; Guess Z
Target word BUZZ; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess B
Target word UZZ; Step 2; Word list UZZ ; Guess U
Target word ZZ; Step 3; Word list ZZ ; Guess Z
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 C
Target word DOZE; Step 3; Word list DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess D
Target word OZE; Step 4; Word list OZE ; Guess O
Target word ZE; Step 5; Word list ZE ; Guess Z
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 B
Target word JAZZ; Step 2; Word list COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess C
Target word JAZZ; Step 3; Word list DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess D
Target word JAZZ; Step 4; Word list FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess F
Target word JAZZ; Step 5; Word list GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess G
Target word JAZZ; Step 6; Word list HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess H
Target word JAZZ; Step 7; Word list JAZZ LAZY SIZE ZERO ZONE ; Guess J
Target word AZZ; Step 8; Word list AZZ ; Guess A
Target word ZZ; Step 9; Word list ZZ ; Guess Z
Target word ZERO; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess B
Target word ZERO; Step 2; Word list COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess C
Target word ZERO; Step 3; Word list DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess D
Target word ZERO; Step 4; Word list FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess F
Target word ZERO; Step 5; Word list GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess G
Target word ZERO; Step 6; Word list HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess H
Target word ZERO; Step 7; Word list JAZZ LAZY SIZE ZERO ZONE ; Guess J
Target word ZERO; Step 8; Word list LAZY SIZE ZERO ZONE ; Guess L
Target word ZERO; Step 9; Word list SIZE ZERO ZONE ; Guess S
Target word ZERO; Step 10; Word list ZERO ZONE ; Guess Z
Target word ERO; Step 11; Word list ERO ONE ; Guess E
Target word RO; Step 12; Word list RO ; Guess R
Target word O; Step 13; Word list O ; Guess O
Target word ZONE; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess B
Target word ZONE; Step 2; Word list COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess C
Target word ZONE; Step 3; Word list DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess D
Target word ZONE; Step 4; Word list FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess F
Target word ZONE; Step 5; Word list GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess G
Target word ZONE; Step 6; Word list HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess H
Target word ZONE; Step 7; Word list JAZZ LAZY SIZE ZERO ZONE ; Guess J
Target word ZONE; Step 8; Word list LAZY SIZE ZERO ZONE ; Guess L
Target word ZONE; Step 9; Word list SIZE ZERO ZONE ; Guess S
Target word ZONE; Step 10; Word list ZERO ZONE ; Guess Z
Target word ONE; Step 11; Word list ERO ONE ; Guess E
Target word ON; Step 12; Word list ON ; Guess O
Target word N; Step 13; Word list N ; Guess N
Target word GAZE; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess B
Target word GAZE; Step 2; Word list COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess C
Target word GAZE; Step 3; Word list DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess D
Target word GAZE; Step 4; Word list FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess F
Target word GAZE; Step 5; Word list GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess G
Target word AZE; Step 6; Word list AZE ; Guess A
Target word ZE; Step 7; Word list ZE ; Guess Z
Target word E; Step 8; Word list E ; Guess E
Target word SIZE; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess B
Target word SIZE; Step 2; Word list COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess C
Target word SIZE; Step 3; Word list DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess D
Target word SIZE; Step 4; Word list FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess F
Target word SIZE; Step 5; Word list GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess G
Target word SIZE; Step 6; Word list HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess H
Target word SIZE; Step 7; Word list JAZZ LAZY SIZE ZERO ZONE ; Guess J
Target word SIZE; Step 8; Word list LAZY SIZE ZERO ZONE ; Guess L
Target word SIZE; Step 9; Word list SIZE ZERO ZONE ; Guess S
Target word IZE; Step 10; Word list IZE ; Guess I
Target word ZE; Step 11; Word list ZE ; Guess Z
Target word E; Step 12; Word list E ; Guess E
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 C
Target word LAZY; Step 3; Word list DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess D
Target word LAZY; Step 4; Word list FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess F
Target word LAZY; Step 5; Word list GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess G
Target word LAZY; Step 6; Word list HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess H
Target word LAZY; Step 7; Word list JAZZ LAZY SIZE ZERO ZONE ; Guess J
Target word LAZY; Step 8; Word list LAZY SIZE ZERO ZONE ; Guess L
Target word AZY; Step 9; Word list AZY ; Guess A
Target word ZY; Step 10; Word list ZY ; Guess Z
Target word Y; Step 11; Word list Y ; Guess Y
Target word HAZE; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess B
Target word HAZE; Step 2; Word list COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess C
Target word HAZE; Step 3; Word list DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess D
Target word HAZE; Step 4; Word list FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess F
Target word HAZE; Step 5; Word list GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess G
Target word HAZE; Step 6; Word list HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess H
Target word AZE; Step 7; Word list AZE ; Guess A
Target word ZE; Step 8; Word list ZE ; Guess Z
Target word E; Step 9; Word list E ; Guess E
Target word COZY; Step 1; Word list BUZZ COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess B
Target word COZY; Step 2; Word list COZY DOZE FUZZ GAZE HAZE JAZZ LAZY SIZE ZERO ZONE ; Guess C
Target word OZY; Step 3; Word list OZY ; Guess O
Target word ZY; Step 4; Word list ZY ; Guess Z
Target word Y; Step 5; Word list Y ; Guess Y
Average number of errors per word 4.909091
2
Pass
 
%%
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.586735
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 2.830000