How Can I Scramble A Word With A Factor?
Solution 1:
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.
from random import random
def shuffle(string, factor):
string = list(string)
length = len(string)
if length < 2:
returnstring
shuffles = int(length * factor)
for i inxrange(shuffles):
i, j = tuple(int(random() * length) for i inxrange(2))
string[i], string[j] = string[j], string[i]
return"".join(string)
x = "computer"print shuffle(x, .2)
print shuffle(x, .5)
print shuffle(x, .9)
coupmter eocpumtr rpmeutoc
If you want the first and the last characters to stay in place, simply split them and add them later on.
def CoolWordScramble(string, factor = .5):
iflen(string) < 2:
returnstring
first, string, last = string[0], string[1:-1], string[-1]
return first + shuffle(string, factor) + last
Solution 2:
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.
Solution 3:
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.
Solution 4:
It could be implemented many ways, but here is my solution:
Wrote a function that just changes a letter's place:
def scramble(s):
s = list(s) #i think more easier, but it is absolutely performance loss
p = s.pop(random.randint(0, len(s)-1))
s.insert(random.randint(0, len(s)-1), p)
return "".join(s)
And wrote a function that apply to a string many times:
defscramble_factor(s, n):
for i inrange(n):
s = scramble(s)
return s
Now we can use it:
>>>s = "paragraph">>>scramble_factor(s, 0)
'paragraph'
>>>scramble_factor(s, 1)
'pgararaph'
>>>scramble_factor(s, 2)
'prahagrap'
>>>scramble_factor(s, 5)
'pgpaarrah'
>>>scramble_factor(s, 10)
'arpahprag'
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:
def scramble(s):
if len(s)<=1:
return s
index = random.randint(0, len(s)-2)
return s[:index] + s[index + 1] + s[index] + s[index+2:]
Solution 5:
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.
Post a Comment for "How Can I Scramble A Word With A Factor?"