Detailed explanation of Python guessing algorithm problems

For the first algorithm question I wrote today, try a simpler one. The requirements for this question are:

Two people, A and B, are guessing numbers. First, A randomly draws three numbers from 1, 2, and 3, and the result is guess. Then B also randomly draws three times, and the result is an answer. Then compare the results of A and B. Examples are as follows:

guess:[1,2,3], answer: [1, 2, 3]

Then it turns out to be correct 3 times

guess: [1,2,3] answer:[3,2,1]

Then the result is one guess right

guess: [1,2,3], answer:[3, 3,1]

Then the result is that the guess is correct 0 times

That is, guess and answer are input as parameters, and the number of correct guesses is returned.

The answers I came up with are as follows:

Answer 1:

classSolution:
 def game(self, guess: List[int], answer: List[int])-  int:
 count =0for i inzip(guess, answer):if i[0]== i[1]:
  count +=1return count

The idea is: use zip to combine the two lists, return a tuple composed of a single element in each list, and then circularly compare. If they are equal, the temporary variable value is +1, and finally the statistical result is returned.

**Answer 2: **

classSolution:
 def game(self, guess: List[int], answer: List[int])-  int:
 count =0for i inrange(3):if guess[i]== answer[i]:
  count+=1return count

Idea: After thinking about it again, I found that my thinking is complicated, because there is no need for splicing, you can directly take the values corresponding to these two lists for comparison. It's even simpler, just loop the comparison directly, add the temporary variable +1 if it is equal, and finally return the temporary variable.

**Answer 3: **

classSolution:
 def game(self, guess: List[int], answer: List[int])-  int:returnsum(map(lambda x,y: x==y, guess, answer))

Idea: Since it can be directly compared, can it be simpler? I thought of the map function. The first parameter of the map function directly uses an anonymous function to compare elements, and then collects the function execution results. If the contrast is equal, the result is True, and the contrast is not equal, the result is False.

Finally, return the sum of the direct results of the map function.

**Answer 4: **

classSolution:
 def game(self, guess: List[int], answer: List[int])-  int:if guess == answer:return3
 elif guess[0]== answer[0]:if guess[1]== answer[1]:return2else:return1
 elif guess[1]== answer[1]:if guess[2]== answer[2]:return2else:return1
 elif guess[2]== answer[2]:return1else:return0

Idea: Comparing guess and answer. This is my worst answer, because this situation is only suitable for this kind of short list comparison. If it is longer, the entire code cannot be seen.

Content expansion:

Python game for beginners: guess the number

Game logic: The computer randomly generates a number, and then the player guesses the number. The computer prompts whether the guessed number is larger or smaller, so that the player can narrow the range of numbers. After the predetermined number is reached, the player fails. If the guess is correct within the number of times, the player wins.

Knowledge points involved: random.randint(), print(), input() (raw_input())

Reference implementation code:

#! /usr/bin/env python 
# encoding: utf-8 

# Use print("",end=...)standard
from __future__ import print_function 

import os 
import sys 
import time 
import random 

# Input detection

while1: 
os.system('cls')print("Hello , Welcome to Guess_Number Games...The Number is between 1 - 10...")print("Please input the level you want(1~10): ",end ='') 
level =raw_input("") 
diff =11-int(level)if diff   10 or diff <1:print("Invalid Input...") 
time.sleep(0.3)else:break

# Guess the number process

count_num =0
ran = random.randint(1,10)while count_num < diff: 
count_num +=1print(str(count_num)+": "+"Please input the number you guess: ",end ='') 
number =raw_input() 
number =int(number)if number < ran:print("Too Little...")continue
elif number   ran:print("Too Big...")continueelse:print("Congraduation! You Win...")breakif count_num == diff:print("You Lose...")

So far, this article on the detailed explanation of the Python number guessing algorithm problem is introduced. For more relevant Python implementation of guessing numbers, please search for the previous articles of ZaLou.Cn or continue to browse the related articles below. Hope you will support ZaLou.Cn more in the future. !

Recommended Posts

Detailed explanation of Python guessing algorithm problems
Detailed explanation of python backtracking template
Detailed explanation of python sequence types
Detailed explanation of Python IO port multiplexing
Detailed explanation of -u parameter of python command
Detailed explanation of the principle of Python super() method
Detailed explanation of python standard library OS module
Detailed explanation of the usage of Python decimal module
Detailed explanation of data types based on Python
Detailed explanation of the principle of Python function parameter classification
Detailed explanation of the principle of Python timer thread pool
Detailed explanation of the implementation steps of Python interface development
Detailed explanation of common tools for Python process control
Detailed explanation of Python web page parser usage examples
Detailed explanation of the remaining problem based on python (%)
Detailed implementation of Python plug-in mechanism
Detailed sorting algorithm (implemented in Python)
Detailed usage of dictionary in Python
Implementation of python gradient descent algorithm
Detailed analysis of Python garbage collection mechanism
Python from attribute to property detailed explanation
Detailed usage of Python virtual environment venv
Detailed explanation of the use of pip in Python | summary of third-party library installation
Ubuntu20.04 install Python3 virtual environment tutorial detailed explanation
Python classic algorithm
7 features of Python3.9
Detailed explanation of building Hadoop environment on CentOS 6.5
Detailed examples of using Python to calculate KS
Detailed explanation of static DNS configuration method in Ubuntu
Detailed explanation of Centos 7 system virtual machine bridging mode
Equal Insurance Evaluation: Detailed Explanation of Centos Timeout Exit
Detailed explanation of CentOS7 network setting tutorial in vmware
Detailed Python IO programming
Detailed Python loop nesting
Basics of Python syntax
Basic syntax of Python
Basic knowledge of Python (1)
Python—requests module detailed explanation
Prettytable module of python
09. Common modules of Python3
Python implements guessing game
Detailed explanation of Spark installation and configuration tutorial under centOS7