简体   繁体   English

Python位求和算法

[英]Python Bit Summation Algorithm

I am trying to implement a function that will be used to judge whether a generator's output is continuous. 我正在尝试实现一个函数,该函数将用于判断发电机的输出是否连续。 The method I am gravitating towards is to iterate through the generator. 我倾向于的方法是遍历生成器。 For each value, I right justify the bits of the value (disregarding the 0b ), count the number of ones, and shift the number of ones. 对于每个值,我都正确地证明了该值的位(不考虑0b ),计算了0b的数量,并转移了一位的数量。

#!/usr/bin/python3
from typing import Tuple


def find_bit_sum(top: int, pad_length: int) -> int :
    """."""
    return pad_length * (top + 1)


def find_pad_length(top: int) -> int :
    """."""
    return len(bin(top)) - 2  # -"0b"


def guess_certain(top: int, pad_length: int) -> Tuple[int, int, int] :
    """."""
    _both: int = find_bit_sum(top, pad_length)
    _ones: int = sum(sum(int(_i_in) for _i_in in bin(_i_out)[2 :]) for _i_out in range(1, top + 1))
    return _both - _ones, _ones, _both  # zeros, ones, sum


def guess(top: int, pad_length: int) -> Tuple[int, int, int] :  # zeros then ones then sum
    """."""
    _bit_sum: int = find_bit_sum(top, pad_length)  # number of bits in total
    _zeros: int = _bit_sum  # ones are deducted
    _ones: int = 0  # _bit_sum - _zeros
    # detect ones
    for _indexed in range(pad_length) :
        _ones_found: int = int(top // (2 ** (_indexed + 1)))  # HELP!!!
        _zeros -= _ones_found
        _ones += _ones_found
    #
    return _zeros, _ones, _bit_sum


def test_the_guess(max_value: int) -> bool :  # the range is int [0, max_value + 1)
    pad: int = find_pad_length(max_value)
    _zeros0, _ones0, _total0 = guess_certain(max_value, pad)
    _zeros1, _ones1, _total1 = guess(max_value, pad)
    return all((
        _zeros0 == _zeros1,
        _ones0 == _ones1,
        _total0 == _total1
    ))


if __name__ == '__main__' :  # should produce a lot of True
    for x in range(3000) :
        print(test_the_guess(x))

For the life of me, I cannot make guess() agree with guess_certain() . 对于我的一生,我无法使guess()guess_certain() The time complexity of guess_certain() is my problem: it works for small ranges [0, top] , but one can forget 256-bit numbers ( top s). 我的问题是guess_certain()的时间复杂度:它适用于小范围[0, top] ,但是您可以忘记256位数字( top s)。 The find_bit_sum() function works perfectly. find_bit_sum()函数可完美运行。 The find_pad_length() function also works. find_pad_length()函数也可以使用。

top // (2 ** (_indexed + 1))

I've tried 40 or 50 variations of the guess() function. 我已经尝试了40个或50个guess()函数的变体。 It has thoroughly frustrated me. 这使我感到非常沮丧。 The guess() function is probabilistic. guess()函数是概率性的。 In its finished state: if it returns False , then the Generator definitely isn't producing every value in range(top + 1) ; 处于完成状态:如果返回False ,则Generator绝对不会产生range(top + 1)每个值; however, if it returns True , then the Generator could be. 但是,如果返回True ,则Generator可能是。 We already know that the generator range(top + 1) is continuous because it does produce each number between 0 and top inclusively; 我们已经知道生成器range(top + 1)是连续的,因为它会生成介于0top之间(包括0top每个数字; so, test_the_guess() should be returning True . 因此, test_the_guess()应该返回True

I sincerely do apologise for the chaotic explanation. 我诚挚地为混乱的解释表示歉意。 If you have anny questions, please don't hesitate to ask. 如果您有任何疑问,请随时提出。

I adjusted your ones_found assignment statement to account for the number of powers of two per int(top // (2 ** (_indexed + 1))) , as well as a additional "rollover" ones that occur before the next power of two. 我调整了ones_found赋值语句,以考虑每个int(top // (2 ** (_indexed + 1)))的幂数,以及在下一个2的幂之前发生的附加“过渡” int(top // (2 ** (_indexed + 1))) 。 。 Here is the resulting statement: 这是结果语句:

_ones_found: int = int(top // (2 ** (_indexed + 1))) * (2 ** (_indexed)) + max(0, (top % (2 ** (_indexed + 1))) - (2 ** _indexed) + 1)

I also took the liberty of converting the statement to bitwise operators for both clarity and speed, as shown below: 为了清楚和快速起见,我还自由地将语句转换为按位运算符,如下所示:

_ones_found: int = ((top >> _indexed + 1) << _indexed) + max(0, (top & (1 << _indexed + 1) - 1) - (1 << _indexed) + 1)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM