国产日韩欧美一区二区三区综合,日本黄色免费在线,国产精品麻豆欧美日韩ww,色综合狠狠操

極客小將

您現在的位置是:首頁 » python編程資訊

資訊內容

用Python解刑偵科推理題,你想到了沒?

極客小將2020-12-27-
簡介刑偵科推理題,不知是誰設計出來的,邏輯嚴整細致,有耐心看完題目的人就沒幾個。如果這真是刑警的日常考試題,我覺得他們實在是太厲害了,保證犯罪分子難逃法網。編程遍歷,這個邏輯本身很普通。但程序中有幾個有趣的點值得一提。首先是十道題,每題有4種可能的選項,全部為4^10=1M,約100萬種可能,可以利用p

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_教程_免費兒童編程學習平臺

預約試聽課

已有385人預約都是免費的,你也試試吧...

主站蜘蛛池模板: 莱州市| 葫芦岛市| 泸定县| 华阴市| 易门县| 班戈县| 三河市| 山阴县| 山丹县| 濉溪县| 塔城市| 饶阳县| 安化县| 永德县| 崇信县| 墨竹工卡县| 双峰县| 大渡口区| 东兴市| 沙田区| 介休市| 阿瓦提县| 松滋市| 辉南县| 玉龙| 台北县| 阜新| 樟树市| 容城县| 永丰县| 虎林市| 清丰县| 汾西县| 武义县| 黑水县| 睢宁县| 龙江县| 黑龙江省| 乌拉特后旗| 贺州市| 卫辉市|