Welcome to the Get Help category!
This is where you can ask questions about your code. Some important things to remember when posting in this category
Learn how to ask a good question and get a good answer!
Remember to include a link to the exercise you need help with!
If someone answers your question, please mark their response as a solution
Once you understand a new concept, come back and try to help someone else!
So I was cruising through the lesson when I found that #1 couldn’t be solved properly, I got the answer but could someone explain why it doesn’t work?
https://www.codecademy.com/paths/computer-science/tracks/cspath-cs-102/modules/pattern-searching/lessons/naive-pattern-search-python-implementation/exercises/counting-pattern-matches
that’s the link to the question
here’s the link to the “wrong answer”
def pattern_search(text, pattern):
print("Input Text:", text, "Input Pattern:", pattern)
for index in range(len(text)):
print("Text Index:", index)
for char in range(len(pattern)):
print("Pattern Index:", char)
if pattern[char] == text[index + char]:
print("Matching index found")
break
text = "HAYHAYNEEDLEHAYHAYHAYNEEDLEHAYHAYHAYHAYNEEDLE"
pattern = "NEEDLE"
pattern_search(text, pattern)
Here’s the fixed version
def pattern_search(text, pattern):
print("Input Text:", text, "Input Pattern:", pattern)
for index in range(len(text)):
print("Text Index:", index)
for char in range(len(pattern)):
print("Pattern Index:", char)
if pattern[0] == text[index + 0]:
print("Matching index found")
break
text = "HAYHAYNEEDLEHAYHAYHAYNEEDLEHAYHAYHAYHAYNEEDLE"
pattern = "NEEDLE"
pattern_search(text, pattern)
Why is the first strategy wrong? I get the logic behind it but the check answer seems to say the first version was correct, any explanation plz? and why oddest it work?
Hi so I think that you may be doing a mistake somewhere I will try to find it asap. Thanks!
system
Closed
May 24, 2023, 11:58am
3
This topic was automatically closed 41 days after the last reply. New replies are no longer allowed.