如何用因子打乱一个单词?

发布于 11-28 08:51 字数 487 浏览 2 评论 0原文

我想用一个因素来打乱一个单词。因子越大,单词就会变得越乱。

例如,系数为 1.00 的单词“paragraphs”将变为“paaprahrgs”,系数为 0.50 的单词“paragraphs”将变为“paargarphs”。

应考虑与原始字母位置的距离和打乱字母的数量。

到目前为止,这是我的代码,它只会在没有因素的情况下进行混乱:

def Scramble(s): 
    return ''.join(random.sample(s, len(s)))

有什么想法吗?

PS这不是一项家庭作业 - 我正在尝试做这样的事情: http:// d24w6bsrhbeh9d.cloudfront.net/photo/190546_700b.jpg

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

对不⑦2024-12-05 08:51:02

您可以将该因子用作字符串中的多个打乱字符。
由于该因子似乎介于 0 和 1 之间,因此您可以将该因子乘以字符串的长度。

from random import random

def shuffle(string, factor):
    string    = list(string)
    length      = len(string)
    if length < 2:
        return string
    shuffles    = int(length * factor)
    for i in xrange(shuffles):
        i, j    = tuple(int(random() * length) for i in xrange(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)

耦合器
eocpumtr
rpmeutoc

如果您希望第一个和最后一个字符保留在原位,只需将它们分开并稍后添加即可。

def CoolWordScramble(string, factor = .5):
    if len(string) < 2:
        return string
    first, string, last = string[0], string[1:-1], string[-1]

    return first + shuffle(string, factor) + last

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:
        return string
    shuffles    = int(length * factor)
    for i in xrange(shuffles):
        i, j    = tuple(int(random() * length) for i in xrange(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):
    if len(string) < 2:
        return string
    first, string, last = string[0], string[1:-1], string[-1]

    return first + shuffle(string, factor) + last
浪推晚风2024-12-05 08:51:02

您还没有定义您的“因子”的含义,所以请允许我为您重新定义它:扰乱因子 N(整数)将是在单词中交换两个随机字母 N 次的结果。

根据此定义,0 表示生成的单词与输入相同,1 表示仅交换一对字母,10 表示交换了 10 次。

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.

我偏爱纯白色2024-12-05 08:51:02

您可以使“因子”大致对应于单词的两个相邻字母交换位置(换位)的次数。

在每次换位中,选择一个随机位置(从 0 到长度减二),然后交换该位置处的字母及其后面的字母的位置。

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.

海之角2024-12-05 08:51:02

它可以通过多种方式实现,但这是我的解决方案:

编写一个仅更改字母位置的函数:

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)

并编写一个多次应用于字符串的函数:

def scramble_factor(s, n):
    for i in range(n):
        s = scramble(s)
    return s

现在我们可以使用它:

>>> 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'

当然函数可以组合或嵌套,但我认为很清楚。

编辑

它不考虑距离,但打乱功能很容易被替换,只需交换相邻字母即可。这是一个:

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:]

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:

def scramble_factor(s, n):
    for i in range(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:]
葬心2024-12-05 08:51:02

您可以执行一个倒数到 0 的 for 循环。

将字符串转换为字符数组,并使用 RNG 选择 2 个字母进行交换。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文