有没有一种通用的方法来计算for循环的渐近时间复杂度?(Is there a general way to calculate the asymptotic time complexity of for loops with step?)
是否有一般方法/方法/方法来计算此类循环的渐近时间复杂度?
j=a while j<n do j=f(j)
其中
f(j)
可以是j+1
2*j
,j*j
等。请注意,上面的代码等同于C for循环
for (j=a; j<n; j=f(j));
Is there a general way/approach/methodology to calculate the asymptotic time complexity of such loops?
j=a while j<n do j=f(j)
where
f(j)
could bej+1
,2*j
,j*j
etc..Note that the above code is equivalent to the C for-loop
for (j=a; j<n; j=f(j));
原文:https://stackoverflow.com/questions/37645550
最满意答案
如果你想获得
bytearray(b'\x01\x02xx')
你需要使用bytearray对象的__repr__
方法:print repr(bytearray([1,2,120,120]))
默认情况下,
__str__
处于调用状态,返回字节数组的字符串表示形式。 该字符串包含不可打印字符的\x01\x02
,因此您会看到问号。参考文献:
If you want to get
bytearray(b'\x01\x02xx')
you need to use the__repr__
method of a bytearray object:print repr(bytearray([1,2,120,120]))
By default
__str__
in invoked, that returns a string representation of a byte array. And that string contains the\x01\x02
that are not printable characters, hence you see question marks.References:
相关问答
更多-
python中如何定义成员为bytearray的数组[2023-01-13]
1、Python数组的变量是不需要定义的,这方面和php一样,它比javascript还是宽松,不过它是用缩进作为段落标识的,作为习惯了C语言风格语法的人,可能一开始会很不习惯使用。 但细一想,其实也是很正常,很合理的。 2、虽然Python在面向过程/面向对象方面也是很宽松,但实际上一般的程序都是一个主入口。 3、然后不断调用其它类库或函数,所以用缩进的方式并无不妥,那样一方面要求用户写代码时要规范,另一方面反向省去了多余的{}。 4、与C语言风格相比,Python主要语法特点而下: ... -
正如医生所说, 返回一个新的字节数组。 ...是0 <= x <256范围内的可变整数序列 例如, >>> s = 'hello world' >>> print bytearray(s) hello world >>> bytearray(s)[0] 104 104是ASCII的ASCII侧。 类bytearray的方法是reverse ,但string不是。 为了反转字符串,这段代码首先得到它的字节数组,然后保留,最后得到str的反向字符串。 此外,您可以使用[::-1]来反转字符串。 ...
-
格式f适用于32位浮点数,需要使用d来表示64位浮点数(双精度)。 请参见格式字符 - struct 明确指定>以获得与站点相同的结果(big-endian) 使用binascii.hexlify将二进制字符串转换为十六进制表示形式。 >>> binascii.hexlify(struct.pack('>d', floats[0])) b'40091eb851eb851f' >>> import struct >>> import binascii >>> >>> floats = [3.14, 2.7, ...
-
Python bytearray是否在C表示中使用有符号整数?(Does Python bytearray use signed integers in the C representation?)[2022-05-29]
Python bytearray是否在C表示中使用有符号整数? 它使用char 。 这些是否签名取决于编译器。 您可以在Include/bytearrayobject.h看到这一点。 这是2.7版本 : /* Object layout */ typedef struct { PyObject_VAR_HEAD /* XXX(nnorwitz): should ob_exports be Py_ssize_t? */ int ob_exports; /* how many buffe ... -
因为2f是字符/的ASCII码。 Because 2f is the ASCII code for the character /.
-
好吧,最后,在网络软件上工作,我发现了一个用于内存视图的用例:当我们有一个固定大小的套接字缓冲区并且我们想对它执行快速切片时(从任何给定位置读/写数据不创建额外的副本)在内存中),像这样: buf = bytearray(4096) mview = memoryview(buf) socket.recv_into(mview) print mview[256:] 所以,在某些情况下,在bytearray上有一个内存视图是很方便的。 这个方案唯一应该记住的是:在bytearray上创建一个内存视图后,你不能 ...
-
传递sep=","作为print()的参数 你几乎在那里的印刷声明。 不需要循环, print有一个sep参数和end 。 >>> print(*range(5), sep=", ") 0, 1, 2, 3, 4 一点解释 内置print将任意数量的项目作为参数打印。 任何非关键字参数将被打印,由sep分隔。 sep的默认值是一个空格。 >>> print("hello", "world") hello world 改变sep有预期的结果。 >>> print("hello", "world", sep ...
-
如果需要实际的bytearray对象,请将列表传递给bytearray构造函数。 serialCOM.write(bytearray(packet)) 请注意, unixTime不会转换为字符串,因此您必须先将其转换。 但你可能想要的(基于有关分号的注释)是使用字符串的join方法加入字符串,如下所示: # Force all items in the list to be strings msg = ';'.join(map(str,packet)) serialCOM.write(msg) 然后,加 ...
-
python bytearray(python bytearray)[2021-09-17]
如果变量是整数,则可以使用位移和按位或运算来形成由8位组成的值,然后将其存储在bytearray中的所需位置。 ba[i] = version << 6 | padding << 5 | extension << 4 | cc If the variables are integers, you can just use bit-shifts and bitwise-or operations to form a value comprised of 8-bits and then store that ... -
如果你想获得bytearray(b'\x01\x02xx')你需要使用bytearray对象的__repr__方法: print repr(bytearray([1,2,120,120])) 默认情况下, __str__处于调用状态,返回字节数组的字符串表示形式。 该字符串包含不可打印字符的\x01\x02 ,因此您会看到问号。 参考文献: https://docs.python.org/2/library/functions.html#repr If you want to get bytearray( ...