python - Finding most sequences of specified length -


i'm trying write python code take string , length, , search through string tell me sub-string of particular length occurs most, prioritizing first if there's tie.

for example, "cadabra abra" 2 should return ab

i tried:

import sys  def main():     inputstring = str(sys.argv[1])     length = int(sys.argv[2])     analyze(inputstring, length)       def analyze(inputstring, length):     count = 0;     runningcount = -1;     sequence = ""     substring = ""     in range(0, len(inputstring)):             substring = inputstring[i:i+length]         j in range(i+length,len(inputstring)):             #print(runningcount)             if inputstring[j:j+2] == substring:                 print("runcount++")                 runningcount += 1                 print(runningcount)                          if runningcount > count:                     count = runningcount                     sequence = substring       print(sequence)                main() 

but can't seem work. know i'm @ least doing wrong counts, i'm not sure what. first program in python too, think problem more algorithm syntax.

try use built-in method, make life easier, way:

>>> s = "cadabra abra" >>> x = 2 >>> l = [s[i:i+x] in range(len(s)-x+1)] >>> l ['ca', 'ad', 'da', 'ab', 'br', 'ra', 'a ', ' a', 'ab', 'br', 'ra'] >>> max(l, key=lambda m:s.count(m)) 'ab' 

edit:

much simpler syntax per stefan pochmann comment:

>>> max(l, key=s.count) 

Comments

Popular posts from this blog

sublimetext3 - what keyboard shortcut is to comment/uncomment for this script tag in sublime -

java - No use of nillable="0" in SOAP Webservice -

ubuntu - Laravel 5.2 quickstart guide gives Not Found Error -