2012-01-06 7 views
2

私は単語 "banana"に "ana"のようなフレーズを入れることができ、フレーズが何回見つかったかを数えます言葉で。私のテストユニットの一部が動作しないようにするエラーを見つけることができません。私のカウント機能をPythonで動かすことができません

def test(actual, expected): 
    """ Compare the actual to the expected value, 
     and print a suitable message. 
    """ 
    import sys 
    linenum = sys._getframe(1).f_lineno # get the caller's line number. 
    if (expected == actual): 
     msg = "Test on line {0} passed.".format(linenum) 
    else: 
     msg = ("Test on line {0} failed. Expected '{1}', but got '{2}'.".format(linenum, expected, actual)) 
    print(msg) 

def count(phrase, word): 
    count1 = 0 
    num_phrase = len(phrase) 
    num_letters = len(word)  

    for i in range(num_letters): 
     for x in word[i:i+num_phrase]: 
      if phrase in word: 
       count1 += 1 
      else: 
       continue  
     return count1 

def test_suite(): 
    test(count('is', 'Mississippi'), 2) 
    test(count('an', 'banana'), 2) 
    test(count('ana', 'banana'), 2) 
    test(count('nana', 'banana'), 1) 
    test(count('nanan', 'banana'), 0) 
    test(count('aaa', 'aaaaaa'), 4) 

test_suite() 
+1

:?

デフ(シーケンス、アイテム)を数えますかP.S.あなたの質問をより読みやすくするために余分な空白行を減らしてください。ありがとうございました。 –

+0

word []のxの繰り返しは意味をなさない。 –

+0

あなたの変数名は非常に混乱しています。例えば、 'num_phrase'はフレーズの数ではなく、その長さです。*' x'は*完全に*記述的ではありません。私の経験では、用語を整理することは、短期間で問題を明らかにする傾向があります。 – kindall

答えて

5

は、以下にごcount機能を変更するには、テストに合格

for i in range(num_letters): #This will go from 1, 2, ---> len(word)  

    for x in word[i:i+num_phrase]: 
    #This will give you the letters starting from word[i] to [i_num_phrase] 
    #but one by one, so : for i in 'dada': will give you 'd' 'a' 'd' 'a' 

     if phrase in word:  #This condition doesnt make sense in your problem, 
            #if it's true it will hold true trough all the 
            #iteration and count will be 
            #len(word) * num_phrase,     
            #and if it's false it will return 0 
      count1 += 1 
     else: 
      continue 
+0

ありがとうございます。どのように私はそれを見落としたか分からない。私はちょうど私の機能を複雑すぎると思う。 – user1091975

+0

大きな文字列を検索している場合、検索を高速化するためにいくつかの[アルゴリズム](http://en.wikipedia.org/wiki/String_searching_algorithm)があります。 – MattH

4

str.count(substring)を使用してください。これは、部分文字列がフルストリング(str)に何回現れるかを返します。あなたが見ることができるように、関数は非重複である

>>> 'Mississippi'.count('is') 
2 
>>> 'banana'.count('an') 
2 
>>> 'banana'.count('ana') 
1 
>>> 'banana'.count('nana') 
1 
>>> 'banana'.count('nanan') 
0 
>>> 'aaaaaa'.count('aaa') 
2 
>>> 

はここにそれがどのように動作するかを示すインタラクティブなセッションです。あなたが重複動作が必要な場合は、ここを見て:そう、あなたが間違った反復を使用している

def count(phrase, word): 
    count1 = 0 
    num_phrase = len(phrase) 
    num_letters = len(word)  
    for i in range(num_letters): 
     if word[i:i+num_phrase] == phrase: 
      count1 += 1 
    return count1 
0

Iをstr.count(部分文字列)は重複する部分文字列を数えず、テストスイートが失敗するため、間違った解決策であると推測されます。

また、タスクに役立つ可能性がある組み込みstr.findメソッドがあります。

-1

今回は基本的な質問です。

"isisisisisi"のような文字列を参照すると、どのように "isi"がカウントされますか?

最初の状態では、文字列"isi s isi s isi"が表示され、カウントとして3が返されます。

2番目の状態では、文字列"isisisisisi"が表示され、「"isi isi isi isi isi"」のようなフレーズごとに「i」トウをカウントします。 2番目の 'i'は最初の 'isi'の最後の文字で、2番目の 'isi'の最初の文字です。

ですので、5をカウントとして返す必要があります。最初の状態のため

は、単純に使用することができます。

>>> string = "isisisisisi" 
>>> string.count("isi") 
3 

と第2の状態のために、あなたは、検索キーワードに"phrase"+"anything"+"phrase"を認識する必要があります。

以下の機能にそれを行うことができます。

def find_iterate(Str): 
    i = 1 
    cnt = 0 
    while Str[i-1] == Str[-i] and i < len(Str)/2: 
     i += 1 
     cnt += 1 
    return Str[0:cnt+1] 

は今、あなたは、文字列の検索キーワードをカウントするために多くの選択肢を持っています。

私は、このような下に行う例えば

if __name__ == "__main__": 
    search_keyword = "isi" 
    String = "isisisisisi" 
    itterated_part = find_iterate(search_keyword) 
    c = 0 
    while search_keyword in String: 
     c += String.count(search_keyword) 
     String = String.replace(search_keyword, itterated_part) 
    print c 

より良い方法は、私は正規表現の助けを借りてこれを実行しようとしましたが、何の方法を見つけないpython.butになるかどうかはわかりません。

0

別の方法:エラーは何ですか

count = 0 

    for x in sequence : 

    if x == item : 
    count = count+1 
    return count 
関連する問題