You are viewing a single comment's thread. Return to all comments →
Need Help! Can anyone help me understand what is wrong with my code? I keep trying to understand but i cant get it right.
def count_substring(string, sub_string): cout = 0 flag = False l2 = list(sub_string) n2 = len(sub_string) l1 = list(string) n1 = len(string) for i in range(n1-n2+1): if l1[i]==l2[0]: for x in range(n2): if l1[i+x] == l2[x]: flag = True else: flag = False if flag: cout+=1 flag = False return cout if __name__ == '__main__': string = input().strip() sub_string = input().strip() count = count_substring(string, sub_string) print(count)
I think the issue is that you are overcomplicating it (too many lines), so you'll likely make lots of hard to fix small errors.
I recommend restarting from scratch with online help.
I've already done it. Yep had to start from the scratch. BTW thanks for the response. 😊
Seems like cookies are disabled on this browser, please enable them to open this website
I agree to HackerRank's Terms of Service and Privacy Policy.
Find a string
You are viewing a single comment's thread. Return to all comments →
Need Help!
Can anyone help me understand what is wrong with my code? I keep trying to understand but i cant get it right.
I think the issue is that you are overcomplicating it (too many lines), so you'll likely make lots of hard to fix small errors.
I recommend restarting from scratch with online help.
I've already done it. Yep had to start from the scratch. BTW thanks for the response. 😊