word scrambler script

4 vues (au cours des 30 derniers jours)
freel
freel le 1 Mar 2011
So I am supposed to write a script that scrambles the letters in a word with the exception of the first and last letter and keep the punctuation intact. so the word "couldn't" should reproduce something along the lines of "cldnuo't".
This is my script so far but I do not understand why it does not work. function out = scrambleWord(in)
if length(in) > 3
n = nnz(in);
ri = randperm(n);
ri(ri == max(ri)) = [];
ri(ri == min(ri)) = [];
if isletter(in) ~= 1
in2 = in
o = isletter(in) ~= 1
in(o) = []
in2(1:(find(o)-1)) = in(1:(find(o))-1)
in2((find(o)+1):end) = in((find(o)):end)
out = in2
end
in(2:(n-1)) = in(ri);out = in
out= in
else out = in;
end
end
  1 commentaire
Walter Roberson
Walter Roberson le 1 Mar 2011
If the word ends in punctuation (which can happen with some possessive forms), then is the letter before the punctuation the "last letter" ? If so then you need to adjust the way you remove the first and last letter in your ri() = [] statements.

Connectez-vous pour commenter.

Réponses (3)

Jan
Jan le 1 Mar 2011
The question sound very similar to:
Please look there for a answers also. An short method to solve your problem:
word = 'couldn''t';
isPunct = ismember(word, '.,-:;''');
cleaned = word(~isPunct);
newWord(~isPunct) = cleaned(randperm(length(cleaned)));

Matt Fig
Matt Fig le 1 Mar 2011
I suspect the problem comes from your (seeming) assumption that the code inside the IF statement only operates on those elements of an array which pass its conditional. That is a false assumption.
When an array is passed to an IF statement, the body of the IF statement is only executed if ALL elements of the vector are true. Try this:
if ([1 0]),disp('inif 1'),end
if ([1 1]),disp('inif 2'),end
To operate only on the elements of an array for which a condition is true, use logical indexing instead. For example:
x = [0 1 4 0 3 0];
idx = x>0; % A logical index.
x(idx) = -9 % Set only the values in the index to -9.
If there are differences between how you treat certain subgroups of the logical index, you may have to make two passes, or simply use a FOR loop and an IF statement to deal with each element one at a time.

Matt Tearle
Matt Tearle le 1 Mar 2011
Is this a homework problem? Without wanting to do the whole thing for you, let me suggest an approach that works:
  1. Use the approach Jan & I showed here to strip and replace punctuation.
  2. Instead of just doing randperm, scramble the letters within the words by splitting into words using regexp. See example 4 "splitting the input string" in the doc for regexp.
  3. Loop through the resulting cell array of words, extract and scramble the 2:end-1 letters. randperm works well for the scrambling.
  4. Reassemble the string (again, see example 4 in the regexp doc).
  5. Reinsert the punctuation (see #1).
I have this working in about a dozen lines of code.

Catégories

En savoir plus sur Characters and Strings dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by