Question
MATLAB: Write a RECURSIVE function anagram that will receive a string as an input argument. It will then randomly scramble the letters and return the
MATLAB: Write a RECURSIVE function anagram that will receive a string as an input argument. It will then randomly scramble the letters and return the resulting anagram. The function should return an appropriate output regardless of the input string size (An anagram is a type of word play, the result of rearranging the letters of a word or phrase to produce a new word or phrase, using all the original letters exactly once.)
Will only rate well for RECURSIVE matlab code, NOT allowed to use built-in Matlab functions, most specifically: perms, randperm, randsample.
Sample run:
>> anagram('Hello') ans =
oHlle
This code doesn't allow the outstring to be the correct size and has a bunch of problem with indecies as the instring gets smaller
function outstring = anagram_rec(instring)
len = length(instring);
i = randi(len,1);
for j = 1:len
index = j;
outstring = char(len);
if index == 1
outstring(i) = instring(i);
instring(i) = [];
if len > 0
anagram(instring);
end
elseif index < len
outstring = [outstring,instring(i)];
instring(i) = [];
if len > 0
anagram(instring);
end
end
end
end
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started