This article mainly introduces the implementation of the Python hack string fault game algorithm, a simple analysis of the principle of the game, and combined with specific examples of the implementation of Python to break the game of the relevant implementation skills, the need for friends can refer to the next
In this paper, we describe the algorithm of solving the puzzle of Python implementation. Share to everyone for your reference, as follows:
Recently found in a QQ group of that kind of robot, send a string to pick up the game:
Somewhat similar to:
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.
Play is the user sends messages to the group:
#找茬
Then there was a robot with automatic chat, and after receiving the sentence, he would send a large pile of words to the group.
And then you can see that there's a "service" in it, and this time, you're sending
#找茬 [Service]
This message to the group, the chat robot received your message will say: The answer is correct, or answer the error and so on.
Sometimes, looking for the word, the eyes looking at the flowers, the bother, I wrote a script in Python to deal with this:
#!/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 ' exit ']: print ' bye! ' Break print U ' #找茬 [%s] '% Char_diff (text)
The principle is simple, is to count the number of characters, return the least number of occurrences.