排列与指定位置的单词

huangapple go评论58阅读模式
英文:

permutations with words at set positions

问题

I am trying to write a script that creates all permutations of words, with a few in set positions. I'm basically trying to recover my 12 word mnemonic seed, in which i have a few extra letters but I know the position of a few for example, I know that word 1 is wild and word 5 is script and the last word is hurt. and I have 15 words that I want to try permutations on, so that I get

wild permword1 permword2 permword3 script permword6 permword7 permword8 permword9 permword10 permword11 hurt

` I have basic knowledge of python. I only know how to just print all the permutations of the words, however that takes longer and also prints some that wouldn't even be a possibility because one of the permutations has word 5 at a different position. I have my list of words, I'm just trying to recover my mnemonic seed in a much more efficient way, because I'm ending up creating so many permutations that my laptop runs out of space. I'm not looking to be reccomended btcrecover as I have a few issues with it.

Ideally I'd get a list like,
> wild permword1 permword2 permword3 script permword6 permword7 permword8 permword9 permword10 permword11 hurt

> wild permword1 permword2 permword3 script permword6 permword7 permword8 permword9 permword10 permword12 hurt

> wild permword1 permword2 permword3 script permword6 permword7 permword8 permword9 permword10 permword13 hurt

I appreciate in advance!

just regular permutations, but i'm unsure of how to split the words so that word 5 is always script.

英文:

I am trying to write a script that creates all permutations of words, with a few in set positions. I'm basically trying to recover my 12 word mnemonic seed, in which i have a few extra letters but I know the position of a few for example, I know that word 1 is wild and word 5 is script and the last word is hurt. and I have 15 words that I want to try permutations on, so that I get

wild permword1 permword2 permword3 script permword6 permword7 permword8 permword9 permword10 permword11 hurt

` I have basic knowledge of python. I only know how to just print all the permutations of the words, however that takes longer and also prints some that wouldn't even be a possibility because one of the permutations has word 5 at a different position. I have my list of words, I'm just trying to recover my mnemonic seed in a much more efficient way, because I'm ending up creating so many permutations that my laptop runs out of space. I'm not looking to be reccomended btcrecover as I have a few issues with it.

Ideally I'd get a list like,
> wild permword1 permword2 permword3 script permword6 permword7 permword8 permword9 permword10 permword11 hurt

> wild permword1 permword2 permword3 script permword6 permword7 permword8 permword9 permword10 permword12 hurt

> wild permword1 permword2 permword3 script permword6 permword7 permword8 permword9 permword10 permword13 hurt

I appreciate in advance!

just regular permutations, but i'm unsure of how to split ther words so that word 5 is always script.

答案1

得分: 2

生成器

我猜你的计算机出现“空间”不足的问题是因为你一次性将所有创建的排列都保存在内存中。这是Python 中生成器的一个完美应用案例。你可以将生成器视为根据索引值创建某个值的规则,而不是一次性创建所有值。例如,可以参考这里:https://stackoverflow.com/questions/1756096/understanding-generators-in-python

在排列中设置值

至于在固定位置设置单词,你可以为每个排列创建一个列表,然后插入你预设的单词。

解决方案

我认为以下代码可以解决你的问题。在最后一行,我打印了排列,你可以将其替换为你想要对每个排列执行的操作。

import itertools

permwords = [f"permword{ind+1}" for ind in range(0, 15)]

def produce_partially_set(word_list):
    word_list.insert(0, "wild")
    word_list.insert(4, "script")
    word_list.append("hurt")
    return word_list

indices_perm = itertools.permutations(range(0, 12))  # 12个未知单词在“solution”中
while True:
    try:
        indices = next(indices_perm)
        permutation = [permwords[x] for x in indices]
        mnemonic_seed = produce_partially_set(permutation)
        print(mnemonic_seed)
    except StopIteration:
        break

备注:一旦遍历了所有可能的排列,代码将引发“StopIteration”异常。

英文:

Generators

I guess that the reason your computer is running out of "space" is that you keep all created permutations in memory at once. This is a perfect use case for generators in python. You can think of generators as rules to create some value given an index value, instead of creating everything at once. See for example here: https://stackoverflow.com/questions/1756096/understanding-generators-in-python

Set values in permutations

As to the words at set positions, you can just create a list for each permutation and then insert your pre-set words.

Solution

I think the following code solves your problem. In the last line, I print the permutation, you can replace this with whatever you want to do with each permutation.

import itertools

permwords = [f"permword{ind+1}" for ind in range(0,15)]

def produce_partially_set(worlist):
    worlist.insert(0, "wild")
    worlist.insert(4, "script")
    worlist.append("hurt")
    return worlist

indices_perm = itertools.permutations(range(0,12)) # 12 unknown words in "solution"
while True:
    indices = next(indices_perm)
    permutation = [permwords[x] for x in indices]
    mneumonic_seed = produce_partially_set(permutation)
    print(mneumonic_seed)

PS: the code will throw a "StopIteration", once it iterated through all possible permutations.

huangapple
  • 本文由 发表于 2023年2月23日 23:42:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547100.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定