2016-10-09 24 views
-3

文字列のpythonでの部分文字列を見つけるが、私は答えを見つけるように見えることはできませんどこでも、私が検索しcharachersが異なるにされた別の文字列から部分文字列を返すにはどうすればよいこれは初心者の質問かもしれません

注文?

私は以下のコードをチェックすると、正解が出ているようですが、それを真正または偽にして印刷しようとしています。また、提出すると、「間違っています。 (UdaciousUdacitee、Udacity)。あなたの投稿は、4つのテストケースのうち3つを通過しました。「....私は混乱しています.3時間ほど私の脳を包み込んでいます。 。

Test case 1: False 
Test case 2: True 
Test case 3: True 
Test case 4: True 

より正確にありがとう:

def fix_machine(debris, product): 
    if debris.find(product): 
    return product 
    else: 
    print("Give me something that's not useless next time.") 


print "Test case 1: ", fix_machine('UdaciousUdacitee', 'Udacity') == "Give me something that's not useless next time." 
print "Test case 2: ", fix_machine('buy me dat Unicorn', 'Udacity') == 'Udacity' 
print "Test case 3: ", fix_machine('AEIOU and sometimes y... c', 'Udacity') == 'Udacity' 
print "Test case 4: ", fix_machine('wsx0-=mttrhix', 't-shirt') == 't-shirt' 
+3

ここまでお試しください。スタックオーバーフローは、他の人にあなたのコードを書くように依頼するのに適していません。 – smarx

答えて

0

あなたがprint()elseケースをINGのです。私はあなたがその文字列を返すことを意味すると思います。 (あなたのアサーションコードに少なくとも従ってください)

0

str.find()が間違っています。

"It determines if string str occurs in string, or in a substring of string if starting index beg and ending index end are given." 

あなたが望むものではないと考えられます。 fix_machine

def fix_machine(debris, product): 
    charNumInDebris = dict() 
    charNumInProduct = dict() 

    for c in debris: 
    if c in charNumInDebris: 
     charNumInDebris[c] += 1 
    else: 
     charNumInDebris[c] = 1 

    for c in product: 
    if c in charNumInProduct: 
     charNumInProduct[c] += 1 
    else: 
     charNumInProduct[c] = 1 

    for c in charNumInProduct: 
    if not (c in charNumInDebris and charNumInDebris[c] >= charNumInProduct[c]): 
     return "Give me something that's not useless next time." 

    return product