2017-03-08 54 views
0

我想查找长度在1-4个字符之间的字母字符串。按顺序遍历多个字符列表

我开始通过52个字母列表迭代:

letters = string.ascii_letters 

我则需要通过同一个目录遍历该字符串的接下来的三个字符,直到我发现我要找的字符串。

如果每个_代表的52个字母列表,我需要基本上做到这一点,而在每次迭代检查匹配:

_ 
_ _ 
_ _ _ 
_ _ _ _ 

如何将我最好的结构系列回路做到这一点?


如果问题的前提看起来很混乱,这是针对暴力破解设置的问题。我简单地提取了我正在努力解决的部分问题。


编辑:这是我到目前为止的地方。

#we know the salt is the 2-digit '50' 
#we know the key is limited to 4 alphabetical letters 
#cycle through all possibilities of the key till we match the hash 

letters = string.ascii_letters 
lcounter = 0 
i = 0 
j = 0 
k = 0 
l = 0 
tryhash = "a" 
word = [letters[i]] 

while(tryhash != hash): 
    for c in letters: 
     word = [letters[i]] #this does not work as the additional letters need to be appended to word after the first lcounter loop 
     tryword = ''.join(word) 
     tryhash = crypt.crypt(tryword, "50") 

     if (tryhash == hash): 
      print(word) 
      break 

     i += 1 

     if (lcounter > 0) and (i == 52): 
      i = 0 
      if (lcounter == 1) and (j == 0): 
       word.insert(lcounter, letters[j]) 
      j += 1 

      if (lcounter > 1) and (k == 52): 
       j = 0 
       if (lcounter == 2) and (k == 0): 
        word.insert(lcounter, letters[k]) 
       k += 1 

       if (lcounter > 2) and (k == 52): 
        k = 0 
        if (lcounter == 3) and (l == 0): 
         word.insert(lcounter, letters[l]) 
        l += 1 

    lcounter += 1 
+0

欢迎SO Kyap!尽管我们非常乐意提供帮助,但您必须先向我们展示您首先完成的工作。 SO通常不是代码写入服务。 – AlG

+0

您能否包含一些示例数据或伪代码?我在阅读您的问题时遇到问题 –

+0

请参阅http://stackoverflow.com/questions/7074051/what-is-the-best-way-to-generate-all-possible-three-letter-strings。 – kennytm

回答

0

这样的事情也许是:

my_string = "some" 
for letter1 in string.ascii_letters: 
    if letter1 == my_string: 
     print("success") 
    for letter2 in string.ascii_letters: 
     if letter1 + letter2 == my_string: 
      print("success") 
     for letter3 in string.ascii_letters: 
      if letter1 + letter2 + letter3 == my_string: 
       print("success") 
      for letter4 in string.ascii_letters: 
       if letter1 + letter2 + letter3 + letter4 == my_string 
        print("success") 
+0

完美!也很简单。我希望我能够为此提出逻辑。 – Kyap

2

你可以做这样的事情:

 

    import string 
    import itertools 

    data = string.ascii_lowecase 

    for i in itertools.permutations(data, 4): 

     if i == 'your_string': 
      #do something 
     else: 
      pass 

+0

使用itertools.permutation不能很好地工作,因为我必须考虑1,2和3个字母字符,而不仅仅是4个字母的排列。 – Kyap

+0

您可以使用outerloop来维持字符串长度,然后使用itertools.permutation。 –