This article mainly introduces the algorithm for cracking the string find-the-difference game implemented in Python, briefly analyzes the principle of the find-the-difference game, and analyzes the relevant implementation techniques for cracking the find-the-difference game in Python based on specific examples. Friends in need can refer to the following
The example in this article describes the algorithm for cracking the string find-the-difference game implemented in Python. Share it with everyone for your reference, the details are as follows:
Recently I found that kind of robot in a QQ group, which sent out a string find the difference game:
is somewhat similar to:
Nope, nope, nope, nope, nope, nope, nope, nope, nope, nope, nope, nope, nope, nope, nope, nope, nope, nope, nope, nope, nope, nope, nope, nope, nope No no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no no nope No, no, no?
# #找茬
Then there is an automatic chat robot in the group. After receiving this sentence, he will send a lot of text above to the group.
Then you can You find that there is a "Servant" in it. At this time, you send the message
#找茬[役]
to the group. After receiving your message, the chatbot will say: The answer is correct , or give wrong answers, etc.
#!/usr/bin/env python # -*- coding: utf-8 -*- def char_diff(text): text=text.replace('\n','').replace('\r','') try: text=text.decode('gb18030','ignore') except: try: text=text.decode('utf-8','ignore') except: pass d={} for x in text: d[x]=d.get(x,0)+1 lll= d.items() lll.sort(key = lambda x: x[1]) return lll[0][0] if __name__ == '__main__': while 1: text = raw_input("> ").decode('gb18030') #print type(text) if text in ['q','e','exit','quit','bye',u'退出']: print 'Bye!' break print u'#找茬[%s] ' % char_diff(text)
is very simple, it is to count the number of characters and return the one with the least number of occurrences.
The above is the detailed content of Implementation method of Python cracking string difference finding game. For more information, please follow other related articles on the PHP Chinese website!