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

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

檢查一個字符串在一個特定單詞中出現(xiàn)多少次

檢查一個字符串在一個特定單詞中出現(xiàn)多少次

元芳怎么了 2021-03-17 14:18:25
我正在這個網(wǎng)站上練習python編碼。這就是問題Return True if the string "cat" and "dog" appear the same number of times in the given string. cat_dog('catdog') → Truecat_dog('catcat') → Falsecat_dog('1cat1cadodog') → True這是我的代碼,出于某些未知原因,我沒有通過所有測試用例。我在調(diào)試時遇到問題def cat_dog(str):    length=len(str)-2    i=0    catcount=0    dogcount=0    for i in range (0,length):        animal=str[i:i+2]        if ("cat" in animal):            catcount=catcount+1        if ("dog" in animal):            dogcount=dogcount+1    if (dogcount==catcount):        return True    else:        return False
查看完整描述

2 回答

?
翻閱古今

TA貢獻1780條經(jīng)驗 獲得超5個贊

您不需要創(chuàng)建函數(shù),只需一行就可以了,例如:

return s.count('cat') == s.count('dog')


查看完整回答
反對 回復 2021-03-27
?
四季花海

TA貢獻1811條經(jīng)驗 獲得超5個贊

沒有循環(huán)的替代方法:


> def cat_dog(str):

>     total_len = len(str)

>     cat = str.replace("cat", "")

>     dog = str.replace("dog", "")

>     if len(cat) == len(dog):

>       if len(cat) < len(str):

>         if len(dog) < len(str):

>           return True

>     if len(cat) == len(str) and len(dog) == len(str):

>       return True

>     else: return False


查看完整回答
反對 回復 2021-03-27
  • 2 回答
  • 0 關注
  • 215 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

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