資訊內容
用Python解刑偵科推理題,你想到了沒?
cf3少兒編程網-Scratch_Python_教程_免費兒童編程學習平臺
刑偵科推理題,不知是誰設計出來的,邏輯嚴整細致,有耐心看完題目的人就沒幾個。如果這真是刑警的日常考試題,我覺得他們實在是太厲害了,保證犯罪分子難逃法網。cf3少兒編程網-Scratch_Python_教程_免費兒童編程學習平臺
cf3少兒編程網-Scratch_Python_教程_免費兒童編程學習平臺
編程遍歷,這個邏輯本身很普通。但程序中有幾個有趣的點值得一提。cf3少兒編程網-Scratch_Python_教程_免費兒童編程學習平臺
首先是十道題,每題有4種可能的選項,全部為4^10=1M,約100萬種可能,可以利用python的yield功能,避免將這么多中間結果保存起來。cf3少兒編程網-Scratch_Python_教程_免費兒童編程學習平臺
def makeList(choose, n): if n==1: for x in choose: yield x a= makeList(choose, n-1) for item in a: b= list(item) for x in choose: c= b.copy() c.append(x) yield c另一個問題就是怎樣把這些題目和選項完整地形式化。cf3少兒編程網-Scratch_Python_教程_免費兒童編程學習平臺
就不一一講解了,代碼邏輯很清晰,直接上代碼。注意:所有加fake的函數,代表對題目的略寫,僅保證了選項正確,而沒有保證非選項錯誤。cf3少兒編程網-Scratch_Python_教程_免費兒童編程學習平臺
之所以這樣處理,是希望假結果也出現。計算結果表明,第5、6、8使用略寫法不會增加新的假結果,只有第4題放開才出現假結果。cf3少兒編程網-Scratch_Python_教程_免費兒童編程學習平臺
為了和題目統一序號,避免出錯,使用了1起,所以在列表前面增加了一個空選項。對其他題目不會有影響,但對第7題和第9題須注意一下,排除掉空選項的干擾。cf3少兒編程網-Scratch_Python_教程_免費兒童編程學習平臺
def q1(x): return True def q2(x): i1= x[2]==A and x[5]==C i2= x[2]==B and x[5]==D i3= x[2]==C and x[5]==A i4= x[2]==D and x[5]==B return i1 or i2 or i3 or i4 def q3(x): i1= x[3]==A and x[2]==x[4]==x[6] and x[2]!=A i2= x[3]==B and x[2]==x[4]==x[3] and x[6]!=B i3= x[3]==C and x[3]==x[4]==x[6] and x[2]!=C i4= x[3]==D and x[2]==x[3]==x[6] and x[4]!=D return i1 or i2 or i3 or i4 def q4(x): i1= x[4]==A and x[1]==x[5] and x[2]!=x[7] and x[1]!=x[9] and x[6]!=x[10] i2= x[4]==B and x[1]!=x[5] and x[2]==x[7] and x[1]!=x[9] and x[6]!=x[10] i3= x[4]==C and x[1]!=x[5] and x[2]!=x[7] and x[1]==x[9] and x[6]!=x[10] i4= x[4]==D and x[1]!=x[5] and x[2]!=x[7] and x[1]!=x[9] and x[6]==x[10] return i1 or i2 or i3 or i4 def q4_fake(x): i1= x[4]==A and x[1]==x[5] i2= x[4]==B and x[2]==x[7] i3= x[4]==C and x[1]==x[9] i4= x[4]==D and x[6]==x[10] return i1 or i2 or i3 or i4 def q5(x): i1= x[5]==A and x[8]==x[5] and x[4]!=x[5] and x[9]!=x[5] and x[7]!=x[5] i2= x[5]==B and x[8]!=x[5] and x[4]==x[5] and x[9]!=x[5] and x[7]!=x[5] i3= x[5]==C and x[8]!=x[5] and x[4]!=x[5] and x[9]==x[5] and x[7]!=x[5] i4= x[5]==D and x[8]!=x[5] and x[4]!=x[5] and x[9]!=x[5] and x[7]==x[5] return i1 or i2 or i3 or i4 def q5_fake(x): i1= x[5]==A and x[8]==x[5] i2= x[5]==B and x[4]==x[5] i3= x[5]==C and x[9]==x[5] i4= x[5]==D and x[7]==x[5] return i1 or i2 or i3 or i4 def q6(x): i1= x[6]==A and (x[8]==x[2]==x[4]) and not(x[1]==x[6]==x[8]) and not(x[3]==x[10]==x[8]) and not(x[5]==x[9]==x[8]) i2= x[6]==B and not(x[8]==x[2]==x[4]) and (x[1]==x[6]==x[8]) and not(x[3]==x[10]==x[8]) and not(x[5]==x[9]==x[8]) i3= x[6]==C and not(x[8]==x[2]==x[4]) and not(x[1]==x[6]==x[8]) and (x[3]==x[10]==x[8]) and not(x[5]==x[9]==x[8]) i4= x[6]==D and not(x[8]==x[2]==x[4]) and not(x[1]==x[6]==x[8]) and not(x[3]==x[10]==x[8]) and (x[5]==x[9]==x[8]) return i1 or i2 or i3 or i4 def q6_fake(x): i1= x[6]==A and (x[8]==x[2]==x[4]) i2= x[6]==B and (x[1]==x[6]==x[8]) i3= x[6]==C and (x[3]==x[10]==x[8]) i4= x[6]==D and (x[5]==x[9]==x[8]) return i1 or i2 or i3 or i4 def q7(x): x0=x[1:] mn= min(x0, key=x0.count) i1= x[7]==A and mn==C i2= x[7]==B and mn==B i3= x[7]==C and mn==A i4= x[7]==D and mn==D return i1 or i2 or i3 or i4 def q8_fake(x): i1= x[8]==A and abs(ord(x[7])- ord(x[1]))!=1 i2= x[8]==B and abs(ord(x[5])- ord(x[1]))!=1 i3= x[8]==C and abs(ord(x[2])- ord(x[1]))!=1 i4= x[8]==D and abs(ord(x[10])- ord(x[1]))!=1 return i1 or i2 or i3 or i4 def q9(x): i1= x[9]==A and xor(x[1]==x[6], x[6]==x[5]) i2= x[9]==B and xor(x[1]==x[6], x[10]==x[5]) i3= x[9]==C and xor(x[1]==x[6], x[2]==x[5]) i4= x[9]==D and xor(x[1]==x[6], x[9]==x[5]) return i1 or i2 or i3 or i4 def q10(x): x0=x[1:] m1= max(x0, key=x0.count) m2= min(x0, key=x0.count) mx= x0.count(m1) mn= x0.count(m2) i1= x[10]==A and mx- mn==3 i2= x[10]==B and mx- mn==2 i3= x[10]==C and mx- mn==4 i4= x[10]==D and mx- mn==1 return i1 or i2 or i3 or i4注意看一下第9題,其中的xor不是python自帶的函數,它的定義很簡單。cf3少兒編程網-Scratch_Python_教程_免費兒童編程學習平臺
def xor(a, b): return (a or b) and not(a and b)**后遍歷得到結果:cf3少兒編程網-Scratch_Python_教程_免費兒童編程學習平臺
def testAnswer(x): a= q1(x) a= a and q2(x) a= a and q3(x) a= a and q4_fake(x) a= a and q5_fake(x) a= a and q6_fake(x) a= a and q7(x) a= a and q8_fake(x) a= a and q9(x) a= a and q10(x) return a A='A' B='B' C='C' D='D' a= makeList([A, B, C, D], 10) c= 0 for x in a: x.insert(0, '') c+=1 if testAnswer(x): print(c, x) print('tested %d times' % c)運行結果cf3少兒編程網-Scratch_Python_教程_免費兒童編程學習平臺
cf3少兒編程網-Scratch_Python_教程_免費兒童編程學習平臺
稍做驗證即可知道,第一個答案是正確的,第二個即前文所提的假結果。cf3少兒編程網-Scratch_Python_教程_免費兒童編程學習平臺
更多Python知識,請關注Python視頻教程!!cf3少兒編程網-Scratch_Python_教程_免費兒童編程學習平臺

- 上一篇
給小白整理的第二篇Python知識點
簡介python視頻教程欄目介紹第二篇Python基礎知識。本系列Python基礎教程共四篇,本文是第二篇。6.2元組tuple和list十分相似,但是tuple是不可變的,即不能修改tuple,元組通過圓括號中用逗號分割的項定義。支持索引和切片操作可以使用in查看一個元素是否在tuple中。空元組()
- 下一篇
Python自學文件操作
簡介python視頻教程欄目介紹自學文件操作推薦(免費):python視頻教程我是一個學習Python初學者,近期剛學完文件操作。特地分享文章分為兩部分,第一部分為文件讀取類型以及讀取的方式,第二部分為練習題文件讀取類型文件讀取的操作方式有以下幾種:只讀,只寫,追加,讀寫,寫讀只讀rf=open(