Here is Python code written to perform the following operations:
- Find each occurrence of a three-letter sequence in a character array (e.g. a sequence such as
('a','b','c')), including overlapping sequences of up to 2 shared characters. - Count the characters between the start of each sequence and the start of all identical sequences following it.
- Every time the same number of characters results from #2, increment a counter for that specific number of characters (regardless of which character sequence caused it).
- Return a dictionary containing all accumulated counters for character distances.
The code is shown below.
counts = {}
# repeating groups of three identical chars in a row
for i in range(len(array)-2):
for j in range(i+1,len(array)-2):
if ((array[i] == array[j]) & (array[i+1] == array[j+1]) & (array[i+2] == array[j+2])):
if counts.has_key(j-i) == False:
counts[j-i] = 1
else:
counts[j-i] += 1
This code was originally written in another programming language, but I would like to apply any optimizations or improvements available in Python. Thank you for your time.