Last active
September 30, 2016 14:19
-
-
Save MattyAyOh/e527db50e578419d390b to your computer and use it in GitHub Desktop.
Determine if a string is a substring of another string
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#NOTE: #### denotes additions to the code between questions | |
#Find the length of a string | |
#Nerf toss question to warm them up | |
#Write a function that takes two strings, and find if the first string contains the second string | |
def findSubstring( main, sub ): | |
lengthMain = len(main) | |
if( len(sub) > len(main) ): | |
return False | |
for i in range(0, lengthMain): | |
if main[i] == sub[0]: | |
matchFlag = True | |
for j in range(0,len(sub)): | |
if( (i+j) > lengthMain-1 ): | |
matchFlag = False | |
break | |
if( sub[j] != main[i+j] ): | |
matchFlag = False | |
break | |
if matchFlag: | |
return True | |
break | |
return False | |
if( findSubstring( "abbbcd", "te\st" ) ): | |
print "YES" | |
#Add in functionality for the escape character , where any character after the \ is ignored | |
def findSubstring2( main, sub ): | |
lengthMain = len(main) | |
print main | |
print sub | |
if( len(sub) > len(main) ): | |
return False | |
for i in range(0, lengthMain): | |
if main[i] == sub[0]: | |
matchFlag = True | |
continueFlag = False ##### | |
newIterator = i ##### | |
for j in range(0,len(sub)): | |
if( continueFlag ): ##### | |
continueFlag = False ##### | |
continue ##### | |
if( sub[j] == "\\" ): ##### | |
continueFlag = True ##### | |
newIterator-=2 ##### | |
continue ##### | |
if( (newIterator+j) > lengthMain-1 ): ##### | |
matchFlag = False | |
break | |
if( sub[j] != main[newIterator+j] ): ##### | |
matchFlag = False | |
break | |
if matchFlag: | |
return True | |
break | |
return False | |
if( findSubstring2( "te\st", "te\st" ) ): | |
print "YES7" | |
if( findSubstring2( "te\st", "te\st\\" ) ): | |
print "YES8" | |
if( findSubstring2( "te\st", "te\st" ) ): | |
print "YES9" | |
#Add functionality for a single wildcard "?" in the second argument, where the "?" can be replaced with any single character | |
def findSubstring3( main, sub ): | |
lengthMain = len(main) | |
if( len(sub) > len(main) ): | |
return False | |
for i in range(0, lengthMain): | |
if main[i] == sub[0] or sub[0] == "?": ##### | |
matchFlag = True | |
for j in range(0,len(sub)): | |
if( (i+j) > lengthMain-1 ): | |
matchFlag = False | |
break | |
if( sub[j] == "?" ): #####important to go second | |
continue ##### | |
if( sub[j] != main[i+j] ): | |
matchFlag = False | |
break | |
if matchFlag: | |
return True | |
break | |
return False | |
if( findSubstring3( "test", "??" ) ): | |
print "YES3" | |
if( findSubstring3( "test", "t?st" ) ): | |
print "YES4" | |
if( findSubstring3( "test", "st?" ) ): | |
print "NO5" | |
if( findSubstring3( "test", "?????" ) ): | |
print "NO6" | |
if( findSubstring3( "test", "t*" ) ): | |
print "YES6" | |
if( findSubstring3( "test", "**" ) ): | |
print "YES7" | |
if( findSubstring3( "test", "****" ) ): | |
print "YES8" | |
if( findSubstring3( "testing", "t*g" ) ): | |
print "YES9" | |
if( findSubstring3( "testing", "t*t*g" ) ): | |
print "YES9" | |
if( findSubstring3( "testing", "t*g*g" ) ): | |
print "YES9" | |
#Implement the multiple character wildcard "*", where if found in the second argument, it could represent any number of any possible characters |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment