【问题标题】:create your own number system using python使用 python 创建自己的数字系统
【发布时间】:2020-06-20 16:35:25
【问题描述】:

我正在制作一个猜密码的小程序。

我制作这个程序只是为了学习,我想通过制作一个真正有意义的程序来提高我的python技能。

例如:

using_characts = "abcdefghijklmnopqrstuvwxyz" # I have other characters in my alphabetic system

我想做的是这样的:

for char in myCharacters:
    print(char)
    for char_1 in myCharacters:
        print(char + char_1)
        for char_2 in myCharacters:
            print(char + char_1 + char_2)
            ...etc

这使得这种方法是非动态的,同时也很难。 输出应该是这样的:

a

b

c

d

e

f

..etc

aa

ab

ac

..etc

ba

bb

bc

..etc

【问题讨论】:

  • 你要生成所有长度的所有组合?
  • 这能回答你的问题吗? Making all possible combinations of a list
  • 我刚刚添加了一个答案,告诉我你的帖子怎么样;)
  • 如果你想一一测试可能的密码,你可以使用生成器,一旦你找到正确的密码就停止。关键是yield关键字。 :)
  • 这就像一个比特币矿工大声笑找到哈希:D

标签: python


【解决方案1】:

你可以使用itertools.product,但你真的应该用一个小数字来限制自己。为更大的数字生成笛卡尔积可能需要很长时间:

from itertools import chain, product
chars = "abcdefghijklmnopqrstuvwxyz"
limit = 2
for perm in chain.from_iterable(product(chars, repeat=i) for i in range(1, limit+1)):
    print("".join(perm))
一个 b C . . . 啊 抗体 交流 . . . 齐 Z Z

【讨论】:

    【解决方案2】:

    给你,这行得通。如果您希望我解释任何部分,请告诉我。

    import itertools
    
    using_characts = "abc"
    
    for str_length in range(1,len(using_characts)+1):
        for q in itertools.product(using_characts,repeat=str_length):
            print("".join(q))
    

    【讨论】:

      【解决方案3】:

      因此,其他答案为您提供了可能有效的代码,但我想解释一种通用方法。此算法使用堆栈来跟踪需要生成的下一个内容,并继续生成直到达到您指定的最大长度。

      from collections import deque
      from typing import Deque, Iterator, Optional
      
      
      def generate_next_strings(chars: str, base: str = "") -> Iterator[str]:
          # This function appends each letter of a given alphabet to the given base.
          # At its first run, it will generate all the single-length letters of the
          # alphabet, since the default base is the empty string.
          for c in chars:
              yield f"{base}{c}"
      
      
      def generate_all_strings(chars: str, maxlen: Optional[int] = None) -> Iterator[str]:
          # We "seed" the stack with a generator. This generator will produce all the
          # single-length letters of the alphabet, as noted above.
          stack: Deque[Iterator[str]] = deque([generate_next_strings(chars)])
      
          # While there are still items (generators) in the stack...
          while stack:
              # ...pop the next one off for processing.
              next_strings: Iterator[str] = stack.popleft()
      
              # Take each item from the generator that we popped off,
              for string in next_strings:
                  # and send it back to the caller. This is a single "result."
                  yield string
      
                  # If we're still generating strings -- that is, we haven't reached
                  # our maximum length -- we add more generators to the stack for the
                  # next length of strings.
                  if maxlen is None or len(string) < maxlen:
                      stack.append(generate_next_strings(chars, string))
      

      您可以使用print("\n".join(generate_all_strings("abc", maxlen=5))) 进行尝试。

      【讨论】:

        【解决方案4】:

        以下代码将为您提供长度在1max_length - 1 之间的所有组合:

        import itertools
        
        combs = []
        
        for i in range(1, max_length):
            c = [list(x) for x in itertools.combinations(using_characts, i)]
            combs.extend(c)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2018-01-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多