- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
当我在谷歌上搜索有关 Python 列表理解的信息时,我收到了一个 google foobar 挑战,过去几天我一直在慢慢地研究它以获得乐趣。最新挑战:
有效地要求生成一个 ID 列表,忽略每一行中不断增加的数字,直到剩下一个 ID。然后你应该对 ID 进行 XOR(^) 以产生校验和。我创建了一个输出正确答案的工作程序,但是它的效率不足以在分配的时间内通过所有测试用例(通过 6/10)。 50,000 的长度应该在 20 秒内产生结果,但它需要 320。
有人可以引导我朝着正确的方向前进,但请不要为我做这件事,我很乐意通过这个挑战来插入自己。也许我可以实现一种数据结构或算法来加快计算时间?
代码背后的逻辑:
首先取起始ID和长度
生成一个 ID 列表,忽略每一行中越来越多的 ID,从忽略第一行的 0 开始。
使用 for 循环对 IDS 列表中的所有数字进行异或运算
答案以整数形式返回
import timeit
def answer(start,length):
x = start
lengthmodified = length
answerlist = []
for i in range (0,lengthmodified): #Outter for loop runs an amount of times equal to the variable "length".
prestringresult = 0
templist = []
for y in range (x,x + length): #Fills list with ids for new line
templist.append(y)
for d in range (0,lengthmodified): #Ignores an id from each line, increasing by one with each line, and starting with 0 for the first
answerlist.append(templist[d])
lengthmodified -= 1
x += length
for n in answerlist: #XORs all of the numbers in the list via a loop and saves to prestringresult
prestringresult ^= n
stringresult = str(prestringresult)
answerlist = [] #Emptys list
answerlist.append(int(stringresult)) #Adds the result of XORing all of the numbers in the list to the answer list
#print(answerlist[0]) #Print statement allows value that's being returned to be checked, just uncomment it
return (answerlist[0]) #Returns Answer
#start = timeit.default_timer()
answer(17,4)
#stop = timeit.default_timer()
#print (stop - start)
最佳答案
您可能需要一种不同的方法,而不仅仅是像 John 那样的小改进。我刚刚编写了一个解决方案,可以在我的 PC 上大约 2 秒内完成 answer(0, 50000)
。我仍然逐行进行,但不是对行范围内的所有数字进行异或运算,而是逐位进行。行中有多少个数字设置了 1 位?[*] 奇数个数字?然后我翻转答案的 1 位。然后与 2 位、4 位、8 位等相同,直至 230 位。所以对于每一行,它只是 31 次小计算(而不是实际对数万个数字进行异或)。
[*] 可以从范围的开始/停止开始在恒定时间内快速计算。
编辑: 由于您要求提供另一个提示,下面介绍如何计算在某个范围 (a, b) 中设置 1 位的频率。计算它在范围 (0, a) 中设置的频率,并从它在范围 (0, b) 中设置的频率中减去该值。如果范围从零开始会更容易。在某个范围(0,c)中设置 1 位的频率是多少?简单:c//2
次。那么在某个范围(a,b)中设置 1 位的频率是多少?只需 b//2 - a//2
次。高位类似,只是稍微复杂一点。
编辑 2: 哦,等等,我刚想起来...有一种简单的方法可以计算某个范围 (a, b) 内所有数字的异或。再次将工作分成 range(0, a) 和 range(0, b)。某个范围 (0, c) 中所有数字的异或很容易,因为有一个很好的模式(如果你对从 0 到比方说 30 的所有 c 都这样做,你就会看到它)。使用它,我现在在大约 0.04 秒 内解决了 answer(0, 50000)
。
关于python - 基于 ID 列表有效计算 XOR(^) 校验和的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41581941/
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!