Skip to Content

Archived discussions are read-only. Learn more about SAP Q&A

Here's one you might find fun ...

1) Generate two random strings of characters over the alphabet {E,T,H,G,B,_}, one string Sa of length 250 and one string Sb of length 150.

2) write a regex program that finds the two substrings of length 50 in Sa and Sb which have the highest percentage of matches between letters. Only absolute matches are allowed - E matches E, _ matches _, etc.

Your code will be reviewed by Bill Mann, who will either nod in approval or gently point out that the length of your instruction path can be reduced by at least one order of magnitude.

Former Member replied

Not a regex in sight in this Python code but I think it works!

import random

dict = ['E','T','H','G','B','_']
Sa = ""
Sb = ""
highest_match = 0
Sa_best = ""
Sb_best = ""

for i in range(0, 250):
    Sa += dict[random.randint(0, len(dict)-1)]
    if i < 150:
        Sb += dict[random.randint(0, len(dict)-1)]

print "Sa ->", Sa, "\nSb ->", Sb

for i in range(0, 100):
    Sb_sub = Sb[i:i+50]
    for n in range(0, 200):
        Sa_sub = Sa[n:n+50]
        match = 0
        for x in range(0,50):
            if Sb_sub[x:x+1] == Sa_sub[x:x+1]:
                match += 1
        if match > highest_match:
            highest_match = match
            Sa_best = Sa_sub
            Sb_best = Sb_sub
print "Best match:", highest_match, "\nSa section ->", Sa_best, "\nSb_section ->", Sb_best

0 View this answer in context
Not what you were looking for? View more on this topic or Ask a question