user5861486 user5861486 - 5 months ago 6
Python Question

Generate character count of sequences

I have a string like '....(((...((...' for which I have to generate another string 'ss(4)h5(3)ss(3)h2(2)ss(3)'.

'.' corresponds to 'ss' and the number of continous '.' is in the bracket.

'(' corresponds to 'h5' and the number of continuos '(' is in the bracket.

Currently I'm able to get the output 'ss(4)h5(3)ss(3)' and my code ignores the last two character sequences.
This is what I have done so far

def main():
stringInput = raw_input("Enter the string:")
ssCount = 0
h5Count = 0
finalString = ""
ssString = ""
h5String = ""
ssCont = True
h5Cont = True
for i in range(0, len(stringInput), 1):
if stringInput[i] == ".":
h5Cont = False
if ssCont:
ssCount = ssCount + 1
ssString = "ss(" + str(ssCount) + ")"
ssCont = True
else:
finalString = finalString + ssString
ssCont = True
ssCount = 1
elif stringInput[i] == "(":
ssCont = False
if h5Cont:
h5Count = h5Count + 1
h5String = "h5(" + str(h5Count) + ")"
h5Cont = True
else:
finalString = finalString + h5String
h5Cont = True
h5Count = 1

print finalString
main()


How to modify the code to get the desired output?

Answer

I don’t know about modifying your existing code, but to me this can be done very succinctly and pythonically using itertools.groupby. Note that I’m not sure if the 'h2' in your expected output is a typo or if it should be 'h5', which I’m assuming.

from itertools import chain, groupby

string = '....(((...((...'

def character_count(S, labels): # this allows you to customize the labels you want to use
    for K, G in groupby(S):
        yield labels[K], '(', str(sum(1 for c in G)), ')' # sum() counts the number of items in the iterator G

output = ''.join(chain.from_iterable(character_count(string, {'.': 'ss', '(': 'h5'}))) # joins the components into a single string
print(output)

# >>> ss(4)h5(3)ss(3)h5(2)ss(3)