如何计算组合的百分比?

How to get percentage of combinations computed?

我有这个密码生成器,它从包含小写字母、大写字母和数字(不带 0)的列表中计算长度为 2 到 6 个字符的组合 - 总共 61 个字符。

我只需要显示已创建组合的百分比(步长为 5)。我试图计算所选长度的所有组合,从该数字一个边界值(5%步长值)并计算文本文件中写入的每个组合,当组合计数满足边界值时,打印 xxx % completed, 但此代码似乎不起作用。

请问您知道如何轻松显示百分比吗?

抱歉我的英语不好,我不是母语人士。

谢谢大家!

def pw_gen(characters, length):

    """generate all characters combinations with selected length and export them to a text file"""

    # counting number of combinations according to a formula in documentation
    k = length
    n = len(characters) + k - 1
    comb_numb = math.factorial(n)/(math.factorial(n-length)*math.factorial(length))

    x = 0

    # first value
    percent = 5

    # step of percent done to display
    step = 5

    # 'step' % of combinations
    boundary_value = comb_numb/(100/step)

    try:
        # output text file
        with open("password_combinations.txt", "a+") as f:
            for p in itertools.product(characters, repeat=length):
                combination = ''.join(p)

                # write each combination and create a new line
                f.write(combination + '\n')
                x += 1

                if boundary_value <= x <= comb_numb:
                    print("{} % complete".format(percent))
                    percent += step
                    boundary_value += comb_numb/(100/step)

                elif x > comb_numb:
                    break

首先 - 我认为您使用的组合公式不正确,因为 itertools.product 会产生重复变化,因此正确的公式是 n^k(n 的 k 次方)。

此外,您将百分比计算过于复杂了一点。我刚刚修改了您的代码以按预期工作。

import math
import itertools

def pw_gen(characters, length):
    """generate all characters combinations with selected length and export them to a text file"""

    k = length
    n = len(characters)
    comb_numb = n ** k

    x = 0
    next_percent = 5
    percent_step = 5

    with open("password_combinations.txt", "a+") as f:
        for p in itertools.product(characters, repeat=length):
            combination = ''.join(p)

            # write each combination and create a new line
            f.write(combination + '\n')
            x += 1

            percent = 100.0 * x / comb_numb
            if percent >= next_percent:
                print(f"{next_percent} % complete")
                while next_percent < percent:
                    next_percent += percent_step

棘手的部分是一个 while 循环,它确保对于非常小的集合(其中一个组合超过结果的 step 百分比)一切都能正常工作。

已删除 try:,因为您没有处理 expect 的任何错误。 也删除了 elif:,这个条件无论如何都不会满足。 此外,您的 comb_numb 公式不正确,因为您正在生成重复组合。通过这些更改,您的代码很好。

import math, iterations, string
def pw_gen(characters, length):

    """generate all characters combinations with selected length and export them to a text file"""

    # counting number of combinations according to a formula in documentation
    comb_numb = len(characters) ** k

    x = 0

    # first value
    percent = 5

    # step of percent done to display
    step = 5

    # 'step' % of combinations
    boundary_value = comb_numb/(100/step)
    # output text file
    with open("password_combinations.txt", "a+") as f:
        for p in itertools.product(characters, repeat=length):
            combination = ''.join(p)
            # write each combination and create a new line
            f.write(combination + '\n')
            x += 1
            if boundary_value <= x:
                print("{} % complete".format(percent))
                percent += step
                boundary_value += comb_numb/(100/step)

pw_gen(string.ascii_letters, 4)