I would like to scramble a word with a factor. The bigger the factor is, the more scrambled the word will become.
For example, the word "paragraphs" with factor of 1.00 would become "paaprahrgs", and it will become "paargarphs" with a factor of 0.50.
The distance from the original letter position and the number of scrambled letters should be taken into consideration.
This is my code so far, which only scrambles without a factor:
def Scramble(s):
return ''.join(random.sample(s, len(s)))
Any ideas?
P.S. This isn't an homework job - I'm trying to make something like this: http://d24w6bsrhbeh9d.cloudfront.net/photo/190546_700b.jpg
It could be implemented many ways, but here is my solution:
Wrote a function that just changes a letter's place:
And wrote a function that apply to a string many times:
Now we can use it:
Of course functions can be combined or nested, but it is clear I think.
Edit:
It doesn't consider distance, but the scramble function easily replaced just for swapping adjacent letters. Here is one:
You could use the factor as a number of shuffling chars in the string around. As the factor seem's to be between 0 and 1, you can multiply the factor with the string's length.
If you want the first and the last characters to stay in place, simply split them and add them later on.
You could do a for-loop that counts down to 0.
Convert the String into a Char-Array and use a RNG to choose 2 letters to swap.
You haven't defined what your "factor" should mean, so allow me to redefine it for you: A scrambling factor N (an integer) would be the result of swapping two random letters in a word, N times.
With this definition, 0 means the resulting word is the same as the input, 1 means only one pair of letters is swapped, and 10 means the swap is done 10 times.
You can make the "factor" roughly correspond to the number of times two adjacent letters of the word switch their positions (a transposition).
In each transposition, choose a random position (from 0 through the length-minus-two), then switch the positions of the letter at that position and the letter that follows it.