Skip to content

Instantly share code, notes, and snippets.

@vikaskumar185
Last active June 2, 2018 11:13
Show Gist options
  • Save vikaskumar185/ef9556b0d8d8956dcfb03bc95183c948 to your computer and use it in GitHub Desktop.
Save vikaskumar185/ef9556b0d8d8956dcfb03bc95183c948 to your computer and use it in GitHub Desktop.
#Bruteforce Patter Matching Multiple Occurence
#Bruteforce Pattern Matching - Multiple Occurence
#Author: VikasKumar<vikaskumar185@gmail.com>
#string: abcdababcgvabcabcddabcdaadabdwgdabdfg
#pattern: abcd
string = "70123456789basdhosdln,asbhiwueoywqieyowqoeho697wqdhosdln,97e9wuedhosdln,dhosdln,dhosdln,dhosdln,"
pattern = "dhosdln,"
#Finding Patter Method()
def FindPattern(string, pattern):
occurence = 0
flag = False
_pos = list()
S = list(string)
P = list(pattern)
SL = len(S)
PL = len(P)
L = SL - PL + 1
print("String: " + string + " >> Length: " + str(SL))
print("Pattern: " + pattern + " >> Length: " + str(PL))
print("Loop Length: " + str(L))
for i , val in enumerate(S) :
if(i < L):
#print("i = " + str(i))
pos = 0
for j, val in enumerate(P):
#print("\tj = " + str(j))
if(P[j] == S[i]):
#print("\t\tP[" + str(j) + "] = " + P[j] + "\tS[" + str(i) + "] = " + S[i] + " >> Matched")
i=i+1
pos = pos + 1
if(pos == PL):
occurence = occurence + 1
flag = True
pos = i - pos
_pos.append(pos);
while(occurence > 0):
if(flag == True):
return _pos
else:
print("Pattern Not Found")
return 0
#Calling Matching Method()
result = FindPattern(string, pattern)
if(result == None):
print("Pattern Not Found")
else:
print("\nMatching Found at Position: " + str(result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment