断言一个数字是二进制的



我正在做作业,遇到了一个问题。我必须把二进制数字串起来。假设我们已经加入了(1,3)- 1是开始的数字3表示我要再处理两个数字(如果我加入了(5,4),我就会处理5,6,7和8)12 103 11结果11011

def num_to_2(number):
num2 = 0
exp = 0
while number >= 1:
num2 += (number % 2) * (10 ** exp)
number = number // 2
exp += 1
return num2

def num_lenght(number):
if number == 0:
return 0
lenght = 0
while number >= 1:
number /= 10
lenght += 1
return lenght

def joined(start, count):
result = 0
end = start + count - 1
for i in range(end, start - 1, -1):
number = num_to_2(i)
res_lenght = num_lenght(result)
result += number * (10 ** res_lenght)
return result

def main():
assert joined(1, 3) == 0b11011
assert joined(10, 4) == 0b1010101111001101
assert joined(8, 5) == 0b10001001101010111100
assert joined(99, 2) == 0b11000111100100
assert joined(999, 3) == 0b111110011111111010001111101001
assert joined(1111, 1) == 0b10001010111

该函数正常工作,但它给我的结果没有0b前缀,我需要为了传递断言。我怎么把它加进去?还有:我不能使用字符串。

提前感谢!

编辑:我添加了其余的代码,所以它更有意义

我已经解决了我的问题,我要做的是"手动"。将伪二进制数转换为以10为基数的数。这是我现在的代码:

def convert_to_2(number):
num2 = 0
exp = 0
while number >= 1:
num2 += (number % 2) * (10 ** exp)
number = number // 2
exp += 1
return num2

def num_lenght(number):
if number == 0:
return 0
lenght = 0
while number >= 1:
number /= 10
lenght += 1
return lenght

def joined(start, count):
result = 0
end = start + count - 1
for i in range(end, start - 1, -1):
number = convert_to_2(i)
res_lenght = num_lenght(result)
result += number * (10 ** res_lenght)
result = convert_to_10(result)
return result

def convert_to_10(number):
num10 = 0
exp = 0
while number >= 1:
num10 += (number % 10) * (2 ** exp)
number = number // 10
exp += 1
return num10

def main():
assert joined(1, 3) == 0b11011
assert joined(10, 4) == 0b1010101111001101
assert joined(8, 5) == 0b10001001101010111100
assert joined(99, 2) == 0b11000111100100
assert joined(999, 3) == 0b111110011111111010001111101001
assert joined(1111, 1) == 0b10001010111

if __name__ == "__main__":
main()

非常感谢你的帮助):

下面是传递所有断言的移位版本:

from math import ceil, log2
from functools import reduce
def join2(a,b):
n = ceil(log2(b+1))
return (a<<n) | b
def joined(start, count):
return reduce(join2,range(start,start+count))
def main():
assert joined(1, 3) == 0b11011
assert joined(10, 4) == 0b1010101111001101
assert joined(8, 5) == 0b10001001101010111100
assert joined(99, 2) == 0b11000111100100
assert joined(999, 3) == 0b111110011111111010001111101001
assert joined(1111, 1) == 0b10001010111

你的老师说"不要使用字符串"是什么意思,我不太清楚。我发现这是一个很奇怪的作业,所以我做了,没有字符串。

我不确定你应该从中学到什么,但它是:

def to_binary(number):
b = 0
while number >= 2**(b+1):
b += 1

while True:
if number >= 2**b:
yield 1
number -= 2**b
else:
yield 0
b -= 1
if b < 0:
return

def joined(start, count):
result = 0
for i in range(start, start + count):
for digit in to_binary(i):
result = (result << 1) + digit
return result

def main():
assert joined(1, 3) == 0b11011
assert joined(10, 4) == 0b1010101111001101
assert joined(8, 5) == 0b10001001101010111100
assert joined(99, 2) == 0b11000111100100
assert joined(999, 3) == 0b111110011111111010001111101001
assert joined(1111, 1) == 0b10001010111

它实现了一个to_binary()函数,该函数将产生一个整数序列。然后join函数将结果二进制向左移位,并加上1或0。

No strings attached…

您不需要担心0b前缀,它只是在Python源代码中表示数字字面值的一种方式。他们可以写270x1b而不是0b11011,但是二进制形式使join()返回的整数值中期望的位模式更清晰。

你可以使用整数的。bit_length()方法来知道有多少位用来表示一个数字。这将使函数更简单一些。

def joined(start,count):
result = 0
for n in range(start,start+count):
result <<= n.bit_length()       # offset by the number of bits
result  |= n                    # add the number's bits
return result

如果不允许使用该方法,可以编写自己的函数来实现:

def bitLength(N): return 0 if not N else bitLength(N//2)+1

如果不允许使用位操作符,则可以使用乘法和加法:

def joined(start,count):
result = 0
for n in range(start,start+count):
result  *= 2**bitLength(n)      # offset by the number of bits
result  += n                    # add the number's bits
return result

相关内容

  • 没有找到相关文章

最新更新