您好,登錄后才能下訂單哦!
今天刷的第一道算法題,先拿一道簡單點(diǎn)的試試手,這道題目的要求是:
兩個(gè)人甲乙在猜數(shù)字,甲先從1,2,3三個(gè)數(shù)字中隨機(jī)抽3次,結(jié)果是guess。乙隨后也隨機(jī)抽三次,結(jié)果是answer。然后對(duì)比甲乙兩個(gè)人的結(jié)果。示例如下:
guess:[1,2,3], answer: [1, 2, 3]
那么結(jié)果就是猜對(duì)了3次
guess: [1,2,3] answer:[3,2,1]
那么結(jié)果就是猜對(duì)了1次
guess: [1,2,3], answer:[3, 3,1]
那么結(jié)果就是猜對(duì)了0次
即將guess和answer兩個(gè)作為參數(shù)輸入,返回猜對(duì)的次數(shù)。
我想出來的幾個(gè)答案如下所示:
答案1:
class Solution: def game(self, guess: List[int], answer: List[int]) -> int: count = 0 for i in zip(guess, answer): if i[0] == i[1]: count += 1 return count
思路是:使用zip將兩個(gè)列表進(jìn)行組合,返回每個(gè)列表中單個(gè)元素組成的元組,然后循環(huán)對(duì)比。如果相等就將臨時(shí)變量值+1,最終返回統(tǒng)計(jì)結(jié)果。
答案2:
class Solution: def game(self, guess: List[int], answer: List[int]) -> int: count = 0 for i in range(3): if guess[i] == answer[i]: count+=1 return count
思路:轉(zhuǎn)念一想,發(fā)現(xiàn)自己想復(fù)雜了,因?yàn)椴恍枰唇?,就可以直接取這兩個(gè)列表對(duì)應(yīng)的值進(jìn)行對(duì)比。就更簡單了,直接循環(huán)對(duì)比,有相等的情況將臨時(shí)變量+1,最后返回臨時(shí)變量即可。
答案3:
class Solution: def game(self, guess: List[int], answer: List[int]) -> int: return sum(map(lambda x,y: x==y, guess, answer))
思路:既然可以直接對(duì)比,那么能不能再簡單點(diǎn),我就想到了map函數(shù),map函數(shù)第一個(gè)參數(shù)直接使用匿名函數(shù)來進(jìn)行元素對(duì)比,然后收集函數(shù)執(zhí)行結(jié)果。如果對(duì)比相等,結(jié)果是True,對(duì)比不相等,結(jié)果是False。
最后返回map函數(shù)直接結(jié)果的總和即可。
答案4:
class Solution: def game(self, guess: List[int], answer: List[int]) -> int: if guess == answer: return 3 elif guess[0] == answer[0]: if guess[1] == answer[1]: return 2 else: return 1 elif guess[1] == answer[1]: if guess[2] == answer[2]: return 2 else: return 1 elif guess[2] == answer[2]: return 1 else: return 0
思路:將guess和answer進(jìn)行對(duì)比,這是我認(rèn)為自己最差的一個(gè)答案,因?yàn)檫@種情況只適合這種簡短的列表對(duì)比,再長一點(diǎn)整個(gè)代碼就沒法看了。
內(nèi)容擴(kuò)展:
Python初學(xué)者小游戲:猜數(shù)字
游戲邏輯:電腦隨機(jī)生成一個(gè)數(shù)字,然后玩家猜數(shù)字,電腦提示猜的數(shù)字大了還是小了,供玩家縮小數(shù)字范圍,達(dá)到既定次數(shù)后,玩家失敗。若在次數(shù)內(nèi)猜對(duì),玩家獲勝。
涉及知識(shí)點(diǎn):random.randint() , print() , input() ( raw_input() )
參考實(shí)現(xiàn)代碼:
#!/usr/bin/env python # encoding: utf-8 #使用print("",end=...)標(biāo)準(zhǔn) from __future__ import print_function import os import sys import time import random #輸入檢測(cè) while 1: 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 #猜數(shù)字流程 count_num = 0 ran = random.randint(1,10) while count_num < diff: count_num += 1 print (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...") continue else: print ("Congraduation! You Win...") break if count_num == diff: print ("You Lose...")
到此這篇關(guān)于Python猜數(shù)字算法題詳解的文章就介紹到這了,更多相關(guān)Python實(shí)現(xiàn)猜數(shù)字內(nèi)容請(qǐng)搜索億速云以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持億速云!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。