2017-02-19 4 views
-2

私は次のことを行う方法と、本当に困惑しているん:ブルートフォースアルゴリズムと文字列マッチング?

Your main program should read in two strings from the user (one string at a time, so you will have two separate strings). The first line (string) that is read is called the text string, and the second line (string) is called the pattern string.

Then you will implement a function, called match that implements the simple brute force pattern matching. This function takes two strings (the text string and the pattern string), and returns True if the pattern string matches some portion of the text string: otherwise, the function returns False .

Your main program must keep doing the following task: ask the user for a text string and a pattern string and print a message that states whether the pattern was found in the text or not. The program stops when the user enters qqq as the text string (your program must stop regardless of the case of the letter).

これまでのところ、私は仕事を入力したコードのどれも。私を助けてください!

+0

あなたがこれまで行ってきたことを示してください。 – Scovetta

+0

私たちは宿題の答えをあなたに与えるつもりはありません。あなたが私たちに聞くことができる特定の質問がありますか?あなたが思うように動作しない問題のいくつかの特定の側面?あなたの精神モデルがうまくいかなければならないと違う振る舞いをするコードの特定の部分は? – user2357112

+0

def bruteMatch(n、m): の範囲(0、len(n)-len(m)+1)の場合: n [i:len(m)+1] == mの場合: リターンn = "HelloStudents" m = "学生" もしlen(n)== len(m)ならばn == mをチェックしてください もしそうなら、len(n)> len(m) 私には私の名前が含まれていませんでした。それは動作しないため、質問のコード。私は物事を実行して一致させる方法を知りません... –

答えて

0

だから私はあなたを助けるかもしれない非常に有用なプログラムを書いたと思います。 @Scovettaが述べたように、我々はPythonのin演算子を使うことができます。しかし、あなたがブルートフォース方法を使用し、Pythonの優れた構文的な砂糖を使用しないことに強く忠実であれば、間違いなくそれを行うことができます。

ここに私が書いたプログラム例があります。これが役に立ったら教えてください!ご質問があればお気軽にお問い合わせください。

# This program is just going to find if a string contains a substring 

def match(text, pattern): 
    ''' 
    Simple match program 
    ''' 
    if pattern in text: 
     return True 
    else: 
     return False 

test_cases = [['ab', 'bab'], ['cat', 'catdog'], ['banana', 'apple'], ['tree', 'tree']] 

def brute_match(text,pattern): 
    ''' 
    We just want to check that our text has our pattern 
    So let's just iterate over our text 
    ''' 
    length_of_pattern = len(pattern) 
    for i in range(len(text)): 
     # Abuse python's splicing because we don't have to worry about end of the string nonsense 
     if text[i:length_of_pattern+i] == pattern: 
      return True 
    return False 


for case in test_cases: 
    ans = match(case[1], case[0]) 
    bans = brute_match(case[1], case[0]) 
    print("{} is in {} (T/F): {}".format(case[0], case[1], ans)) 
    print("{} is in {} (T/F): {}".format(case[0], case[1], bans)) 


# Enter your main program 
# We want to loop infinitely unless we get qqq 
while True: 
    text_string = raw_input("Please enter your text string: ") 
    if text_string == 'qqq': 
     break 
    pattern_string = raw_input("Please enter your pattern string: ") 
    print("Using 'in' operator:") 
    print('-'*20) 
    if match(text_string): 
     print("IS a valid match") 
    else: 
     print("NOT a valid match") 
    print("Using brute force:") 
    print('-'*20) 
    if brute_match(text_string): 
     print("IS a valid match") 
    else: 
     print("NOT a valid match") 

コードがちょっと面倒だとわかりましたが、私はちょうどすぐにそれを書きたいと思っていました!

質問がある場合はお知らせください。

+0

お返事ありがとうございます!私はテキストとパターンを書こうとしています。そして、プログラムは、パターンがテキスト内にあるかどうかを、一致するまで毎回1回シフトすることによって見つけるべきです。私はちょっとあなたのコードと混同しています - テストケースは何ですか?どのように4つありますか?どうもありがとう。 –

+0

絶対に!コードを少しクリアしてみましょう。 私は自分の正気のためにテストケースを作成しました。私は両方のケースが同等であることを証明したかったです(または、少なくとも少し重視してください)。これが私の頭の上からいくつかのサンプルテストケースを作成した理由です。 'brute_match'アルゴリズムに関しては、これはあなたが記述するアルゴリズムです。 Pythonの文字列スプライシングは、インデックス境界エラーを許容しません。したがって、 'a = 'abc''を持つ場合、[2:100]は単に' c 'だけです。だから私はその素晴らしい大会を乱用していた。しかし、ブルートフォースアルゴリズムはこれをやっています。 – jlarks32

+0

彼らのために人々の宿題をしないでください。 – user2357112

関連する問題