Home > Backend Development > Python Tutorial > Implementation method of Python cracking string difference finding game

Implementation method of Python cracking string difference finding game

巴扎黑
Release: 2017-09-26 10:37:18
Original
2126 people have browsed it

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?

#

  #找茬
Copy after login

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

  #找茬[役]
Copy after login

to the group. After receiving your message, the chatbot will say: The answer is correct , or give wrong answers, etc.

Sometimes, looking for this word is confusing and troublesome, so I wrote a script in python to handle this:

The principle of
#!/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)
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template