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