第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

字符串比較排序功能

字符串比較排序功能

qq_笑_17 2021-11-02 20:00:19
我正在設(shè)計(jì)一個(gè)猜字游戲,我需要一些關(guān)于其中一個(gè)功能的幫助。該函數(shù)接收 2 個(gè)輸入并返回 true 或 false。輸入 my_word 包含猜出并與某個(gè)單詞匹配的字母。輸入 other_word 是一些要與 my_input 進(jìn)行比較的單詞。例子:>>> match_with_gaps("te_ t", "tact")False>>> match_with_gaps("a_ _ le", "apple")True>>> match_with_gaps("_ pple", "apple")True>>> match_with_gaps("a_ ple", "apple")False我的問題是應(yīng)用它來返回一個(gè) False 就像上一個(gè)例子一樣,我不知道該怎么做。這是我迄今為止所做的。它有效,但不適用于 my_word 中一個(gè)猜出的字母在 other_word 中出現(xiàn) 2 次的情況。在這種情況下,我返回 true 但它應(yīng)該是 False。輸入必須與示例中的格式完全相同(下劃線后的空格)。def match_with_gaps(my_word, other_word):    myWord = []    otherWord = []    myWord_noUnderLine = []    for x in my_word:        if x != " ": # remove spaces            myWord.append(x)    for x in myWord:        if x != "_": # remove underscore            myWord_noUnderLine.append(x)    for y in other_word:        otherWord.append(y)    match = ( [i for i, j in zip(myWord, otherWord) if i == j] ) # zip together letter by letter to a set    if len(match) == len(myWord_noUnderLine): # compare length with word with no underscore        return True    else:        return Falsemy_word = "a_ ple"other_word = "apple"print(match_with_gaps(my_word, other_word))
查看完整描述

2 回答

?
繁星coding

TA貢獻(xiàn)1797條經(jīng)驗(yàn) 獲得超4個(gè)贊

您可以創(chuàng)建字符串的“無空格”版本和“無空格,無下劃線”版本,然后比較每個(gè)字符以查看是否匹配非下劃線字符或是否已使用與下劃線對應(yīng)的字符。例如:


def match_with_gaps(match, guess):

    nospace = match.replace(' ', '')

    used = nospace.replace('_', '')

    for a, b in zip(nospace, guess):

        if (a != '_' and a != b) or (a == '_' and b in used):

            return False

    return True


print(match_with_gaps("te_ t", "tact"))

# False

print(match_with_gaps("a_ _ le", "apple"))

# True

print(match_with_gaps("_ pple", "apple"))

# True

print(match_with_gaps("a_ ple", "apple"))

# False


查看完整回答
反對 回復(fù) 2021-11-02
?
撒科打諢

TA貢獻(xiàn)1934條經(jīng)驗(yàn) 獲得超2個(gè)贊

這條線在這里:

 if len(match) == len(myWord_noUnderLine)

不會(huì)給你你現(xiàn)在想要的。在“a_ple”示例中,空格和“_”都被刪除,因此您的 myWord_noUnderLine 變量將為“aple”,因此檢查“aple”和“apple”之間的長度匹配肯定會(huì)失敗


查看完整回答
反對 回復(fù) 2021-11-02
  • 2 回答
  • 0 關(guān)注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號