Skip to main content
Code Review

Return to Question

Notice removed Improve details by Community Bot
Bounty Ended with Snowbody's answer chosen by Community Bot
Notice added Improve details by NinjaG
Bounty Started worth 50 reputation by NinjaG
fixed the typo for the code
Source Link
NinjaG
  • 2.6k
  • 2
  • 29
  • 61
def index_of(text, pattern):
 assert isinstance(text, str), 'text is not a string: {}'.format(text)
 assert isinstance(pattern, str), 'pattern is not a string: {}'.format(text)
 """In the Boyer-Moore algorithm, you start comparing pattern characters to text characters from the end of the pattern.""" 
 # Check if empty; if so, return 0, earliest bit
 # Use the shift rule according to boyer-moore
 if len(pattern) == 0:
 return 0
 pattern = list(pattern)
 pattern_len = len(pattern)
 text = list(text)
 textext_len = len(text)
 shift = 0
 while(shift <= textext_len - pattern_len):
 index = pattern_len - 1
 # Keep going until it all meets
 while index >= 0 and pattern[index] == text[shift+index]:
 index -= 1
 # If we get past index, then return where the shift is
 if index < 0:
 return shift
 else:
 if text[shift+pattern_len-1] in pattern:
 shift += pattern_len - pattern.index(text[shift+pattern_len-1]) - 1
 else:
 shift += pattern_len
 return None
def main():
 import sys
 args = sys.argv[1:] # Ignore script file name
 if len(args) == 2:
 text = args[0]
 pattern = args[1]
 test_string_algorithms(text, pattern)
 else:
 script = sys.argv[0]
 index = index_of(text, pattern)
 print('index_of({!r}, {!r}) => {}'.format(text, pattern, index))
if __name__ == '__main__':
 main()
def index_of(text, pattern):
 assert isinstance(text, str), 'text is not a string: {}'.format(text)
 assert isinstance(pattern, str), 'pattern is not a string: {}'.format(text)
 """In the Boyer-Moore algorithm, you start comparing pattern characters to text characters from the end of the pattern.""" 
 # Check if empty; if so, return 0, earliest bit
 # Use the shift rule according to boyer-moore
 if len(pattern) == 0:
 return 0
 pattern = list(pattern)
 pattern_len = len(pattern)
 text = list(text)
 textext_len = len(text)
 shift = 0
 while(shift <= textext_len - pattern_len):
 index = pattern_len - 1
 # Keep going until it all meets
 while index >= 0 and pattern[index] == text[shift+index]:
 index -= 1
 # If we get past index, then return where the shift is
 if index < 0:
 return shift
 else:
 if text[shift+pattern_len-1] in pattern:
 shift += pattern_len - pattern.index(text[shift+pattern_len-1]) - 1
 else:
 shift += pattern_len
 return None
def main():
 import sys
 args = sys.argv[1:] # Ignore script file name
 if len(args) == 2:
 text = args[0]
 pattern = args[1]
 test_string_algorithms(text, pattern)
 else:
 script = sys.argv[0]
 index = index_of(text, pattern)
 print('index_of({!r}, {!r}) => {}'.format(text, pattern, index))
if __name__ == '__main__':
 main()
def index_of(text, pattern):
 assert isinstance(text, str), 'text is not a string: {}'.format(text)
 assert isinstance(pattern, str), 'pattern is not a string: {}'.format(text)
 # Check if empty; if so, return 0, earliest bit
 if len(pattern) == 0:
 return 0
 pattern = list(pattern)
 pattern_len = len(pattern)
 text = list(text)
 textext_len = len(text)
 shift = 0
 while(shift <= textext_len - pattern_len):
 index = pattern_len - 1
 # Keep going until it all meets
 while index >= 0 and pattern[index] == text[shift+index]:
 index -= 1
 # If we get past index, then return where the shift is
 if index < 0:
 return shift
 else:
 if text[shift+pattern_len-1] in pattern:
 shift += pattern_len - pattern.index(text[shift+pattern_len-1]) - 1
 else:
 shift += pattern_len
 return None
added tag reinventing-the-wheel
Link
NinjaG
  • 2.6k
  • 2
  • 29
  • 61
remove the confusion in the comment section
Source Link
NinjaG
  • 2.6k
  • 2
  • 29
  • 61
def index_of(text, pattern):
 assert isinstance(text, str), 'text is not a string: {}'.format(text)
 assert isinstance(pattern, str), 'pattern is not a string: {}'.format(text)
 """In the Boyer-Moore algorithm, you start comparing pattern characters to text characters from the end of the pattern. 
 If you find a mismatch, you have a configuration of the""" type"""
 # Check if empty; if so, return 0, earliest bit
 # Use the shift rule according to boyer-moore
 if len(pattern) == 0:
 return 0
 pattern = list(pattern)
 pattern_len = len(pattern)
 text = list(text)
 textext_len = len(text)
 shift = 0
 while(shift <= textext_len - pattern_len):
 index = pattern_len - 1
 # Keep going until it all meets
 while index >= 0 and pattern[index] == text[shift+index]:
 index -= 1
 # If we get past index, then return where the shift is
 if index < 0:
 return shift
 else:
 if text[shift+pattern_len-1] in pattern:
 shift += pattern_len - pattern.index(text[shift+pattern_len-1]) - 1
 else:
 shift += pattern_len
 return None
def main():
 import sys
 args = sys.argv[1:] # Ignore script file name
 if len(args) == 2:
 text = args[0]
 pattern = args[1]
 test_string_algorithms(text, pattern)
 else:
 script = sys.argv[0]
 index = index_of(text, pattern)
 print('index_of({!r}, {!r}) => {}'.format(text, pattern, index))
if __name__ == '__main__':
 main()
def index_of(text, pattern):
 assert isinstance(text, str), 'text is not a string: {}'.format(text)
 assert isinstance(pattern, str), 'pattern is not a string: {}'.format(text)
 """In the Boyer-Moore algorithm, you start comparing pattern characters to text characters from the end of the pattern. 
 If you find a mismatch, you have a configuration of the type"""
 # Check if empty; if so, return 0, earliest bit
 # Use the shift rule according to boyer-moore
 if len(pattern) == 0:
 return 0
 pattern = list(pattern)
 pattern_len = len(pattern)
 text = list(text)
 textext_len = len(text)
 shift = 0
 while(shift <= textext_len - pattern_len):
 index = pattern_len - 1
 # Keep going until it all meets
 while index >= 0 and pattern[index] == text[shift+index]:
 index -= 1
 # If we get past index, then return where the shift is
 if index < 0:
 return shift
 else:
 if text[shift+pattern_len-1] in pattern:
 shift += pattern_len - pattern.index(text[shift+pattern_len-1]) - 1
 else:
 shift += pattern_len
 return None
def main():
 import sys
 args = sys.argv[1:] # Ignore script file name
 if len(args) == 2:
 text = args[0]
 pattern = args[1]
 test_string_algorithms(text, pattern)
 else:
 script = sys.argv[0]
 index = index_of(text, pattern)
 print('index_of({!r}, {!r}) => {}'.format(text, pattern, index))
if __name__ == '__main__':
 main()
def index_of(text, pattern):
 assert isinstance(text, str), 'text is not a string: {}'.format(text)
 assert isinstance(pattern, str), 'pattern is not a string: {}'.format(text)
 """In the Boyer-Moore algorithm, you start comparing pattern characters to text characters from the end of the pattern.""" 
 # Check if empty; if so, return 0, earliest bit
 # Use the shift rule according to boyer-moore
 if len(pattern) == 0:
 return 0
 pattern = list(pattern)
 pattern_len = len(pattern)
 text = list(text)
 textext_len = len(text)
 shift = 0
 while(shift <= textext_len - pattern_len):
 index = pattern_len - 1
 # Keep going until it all meets
 while index >= 0 and pattern[index] == text[shift+index]:
 index -= 1
 # If we get past index, then return where the shift is
 if index < 0:
 return shift
 else:
 if text[shift+pattern_len-1] in pattern:
 shift += pattern_len - pattern.index(text[shift+pattern_len-1]) - 1
 else:
 shift += pattern_len
 return None
def main():
 import sys
 args = sys.argv[1:] # Ignore script file name
 if len(args) == 2:
 text = args[0]
 pattern = args[1]
 test_string_algorithms(text, pattern)
 else:
 script = sys.argv[0]
 index = index_of(text, pattern)
 print('index_of({!r}, {!r}) => {}'.format(text, pattern, index))
if __name__ == '__main__':
 main()
Tweeted twitter.com/StackCodeReview/status/925570664584695808
edited tags
Link
Phrancis
  • 20.5k
  • 6
  • 69
  • 155
Loading
added 34 characters in body
Source Link
NinjaG
  • 2.6k
  • 2
  • 29
  • 61
Loading
edited tags
Link
200_success
  • 145.5k
  • 22
  • 190
  • 478
Loading
deleted 78 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238
Loading
Source Link
NinjaG
  • 2.6k
  • 2
  • 29
  • 61
Loading
lang-py

AltStyle によって変換されたページ (->オリジナル) /