How to write a function to scramble letters?

How would I create a function called 'wordScramble', which will randomly scramble letters in a word. The function accepts as an input a string of any length and returns a string with the same length as the input string, but with the characters randomly scrambled.
For example, if I input wordScramble('Hello') and it returned ans= l o e l H

답변 (3개)

Joseph Cheng
Joseph Cheng 2015년 4월 28일
편집: Joseph Cheng 2015년 4월 28일

1 개 추천

You can use the function randperm() to generate a random permutation of the indexes. so then you would use the random permutation to reorder the string array

댓글 수: 7

computing6
computing6 2015년 4월 28일
I don't think we've learned that function in my computing course
Joseph Cheng
Joseph Cheng 2015년 4월 28일
Well since its homework what have you tried or thought up of? I find these problems are not just about learning the syntax but ways to get you to think of creative ways to tackle the problem. What have you learned and what functions are you allowed to use?
Putting my TA hat on maybe this hint would help. you probably know how to make a for loop, generate a random number, and take items away from an array/put things into another array. using just these three methods would get your scrambling function.
So learn now . What's wrong with learning new things?
computing6
computing6 2015년 4월 28일
Haven't thought up anything, really. I've watched a bunch of For Loop tutorials to try and use that to help me figure it out, but I just don't really know where to even begin. I've felt pretty lost all semester in my class :(
computing6
computing6 2015년 4월 28일
Well, since we haven't learned it in lecture, I don't think I can use that on my exam or homework
Joseph Cheng
Joseph Cheng 2015년 4월 28일
편집: Joseph Cheng 2015년 4월 28일
Okay so... I'll only provide an outline so your function would look something like this.
function scrambled = wordScramble(Oword)
totNum = total number of letters using functions like length() or size();
while totNum does not equal 0
randindex = random number from 1 to totNum;
%using the randindex to add the letter to scrambled.
%using randindex remove that index from Oword.
totNum = totNum-1
end
While this is one way to do it there are multiple ways to accomplish the scrambling using basic Matlab calls. Maybe use rand and loops to perform a mixing like the shell game aka Thimblerig.
I'm not sure if it is the best way but one method is to write out an outline, like i have above, of what you're trying to accomplish.
I would ask your instructor explicitly if you are, or are not, allowed to use other functions that you've learned about on your own outside of class.

댓글을 달려면 로그인하십시오.

Image Analyst
Image Analyst 2015년 4월 29일
편집: Image Analyst 2015년 4월 29일

0 개 추천

Well, try this:
string = 'abcdefghijklmnopqrstuvwxyz'
scrambledString = [];
while length(string) > 0
% Get a random location in string.
index = randi(length(string), 1);
% Append that to scrambledString.
scrambledString = [scrambledString, string(index)];
% Remove that letter from string.
string(index) = [];
end
% Show in the command window.
scrambledString
In the command window:
string =
abcdefghijklmnopqrstuvwxyz
scrambledString =
fcjknpbhwaytorgqzmselvuidx
Hopefully I didn't just do the entire homework question for you or you'd be plagiarizing. If it was homework, you were supposed to add the "homework" tag to it up below your question but I didn't see that.

댓글 수: 1

computing6
computing6 2015년 4월 29일
It's not homework! These are practice questions to work through to review for our final, so it's nothing I will be receiving credit for
Thank you!!

댓글을 달려면 로그인하십시오.

Emil Latypov
Emil Latypov 2020년 4월 2일

0 개 추천

word = 'fantastic'
newword = wordscramble(word)
function newword = wordscramble(word)
if isempty(word)
newword = '';
else
len = length(word);
k = randi(len);
ch = word(k);
word(k) = [];
newword = [ch wordscramble(word)];
end
end

카테고리

도움말 센터File Exchange에서 Characters and Strings에 대해 자세히 알아보기

질문:

2015년 4월 28일

답변:

2020년 4월 2일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by