日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python中异或运算_python – 基于ID列表有效计算XOR(^)校验和的方法

發布時間:2025/3/15 python 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python中异或运算_python – 基于ID列表有效计算XOR(^)校验和的方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

當谷歌搜索有關

Python列表理解的信息時,我獲得了一個google foobar挑戰,我在過去的幾天里一直在慢慢地工作以獲得樂趣.最新挑戰:

有效地要求生成ID列表,忽略每個新行的增加數字,直到剩下一個ID.然后你應該XOR(^)ID來產生校驗和.我創建了一個輸出正確答案的工作程序,但是在分配的時間內傳遞所有測試用例(通過6/10)效率不高.長度為50,000應該在20秒內產生結果,但需要320.

有人可以引導我朝著正確的方向前進,但請不要為我做這件事,我很樂意用這個挑戰推動自己.也許我可以實現一種數據結構或算法來加快計算時間?

代碼背后的邏輯:

>首先,獲取起始ID和長度

>生成ID列表,忽略每個新行中越來越多的ID,從忽略第一行的0開始.

>使用for循環對IDS列表中的所有數字進行異或

>答案以int形式返回

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)

總結

以上是生活随笔為你收集整理的python中异或运算_python – 基于ID列表有效计算XOR(^)校验和的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。