[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

re: regex



I figured out a faily efficient non-regex solution.


sl = len(s)
tmps = s
for i in range(1,sl / 2 + 1):
    if sl % i == 0:
	g1 = tmps[:i]
	tmps = tmps[i:]
	match = 1
	while len(tmps) > 0:
	    g = tmps[:i]
	    tmps = tmps[i:]
	    if g != g1:
		match = 0
		break
	if match:
	    tmps = g1
	    break
	else:
	    tmps = s

This goes through all numbers from 1 to sl/2 and tries to reduce for all
divisors of sl. First, split off the first i (x in previous posts) characters
from the string then pull the next i characters from the string until the
string is empty or the current i characters don't match the first i characters.
If the string is exhausted, then the first i characters is the simplified form
of the string. This runs quite fast(no noticible speed difference than
regex's)on the strings I'm using since they are rarly longer than 20 or so
characters.
------------------
Trevor R.H. Clarke                     Computer Science House
Rochester Institute of Technology      Systems Programmer for ISC
retrev@csh.rit.edu                     trcsys@rit.edu
http://www.csh.rit.edu/~retrev/        finger retrev@csh.rit.edu for PGP key