Python 3: How do I determine the re-occuring sub-string/pattern within a string? -


i'm working on checkio coding problem involves decoding message. have find key given encrypted , decrypted message i've done; pythonpythonpythonpythonp

however, when use same key try , decrypt message longer string fails can start again p, , not y. so, either need find way find substring python within key, or else continue above key starting y.

i started off trying find each instance of initial letter of key , trying build string there on assumption each occurrence of first letter start of key.this problem on checkio marked simple i'm missing somewhere.

here have doesn't seem terribly efficient , relies on key being single word repeated. falls on if letter repeated within phrase, e.g. checkiocheckioch fails. there better way of doing this?

# assumes single word repetition in string  def find_string(strtest):     = []     x = []     y = []     b in range(0, len(strtest)):         a.append(strtest[b])     c in range(0, len(a)):         if a[c] == a[0]:             x.append(c)     d in range(0, len(x)-1):         y.append(x[d+1] - x[d])     if y.count(y[0]) == len(y):         intwholestrings = len(strtest) // x[len(x) - 1]         return find_sub(intwholestrings, y[0], strtest)     else:         return false  def find_sub(intcount, intlen, strtest):     a1 = strtest[:2]     in range(2, intlen):         j in range(1, intcount+1):             if a1 in strtest[:intlen * j]:                 a1 = a1 + strtest[i]     return a1  print(find_string('pythonpythonpythonpythonpyth')) 

for finding largest substring of given base string i'd use this:

import re      def get_largest_substring(string):         length = 0         x = 0         y = 0          y in range(len(string)):             x in range(len(string)):                 substring = string[y:x]                 if len(list(re.finditer(substring,string))) > 1  , len(substring) > length:                     match = substring                     length = len(substring)         return match          print get_largest_substring("pythonpythonpythonionpythonionspythonionsd") 

this allows possible alterations in string , won't throw wobbly given changes algorithm would


Comments

Popular posts from this blog

sequelize.js - Sequelize group by with association includes id -

android - Robolectric "INTERNET permission is required" -

java - Android raising EPERM (Operation not permitted) when attempting to send UDP packet after network connection -