When two or more different monomers unite together to polymerize, the result is called a heteropolymer or a copolymer. Polymers that contain only a single type of repeat unit are known as homopolymers. Commercially relevant copolymers include acrylonitrile butadiene styrene (ABS) plastic, styrene-butadiene rubber (SBR), nitrile rubber, styrene-acrylonitrile, styrene-isoprene-styrene (SIS) and ethylene-vinyl acetate.

styrene-butadiene
Chemical structure of styrene-butadiene.

Since a  copolymer consists of at least two types of constituent units (also structural units), they can be classified based on how these units are arranged along the chain. Periodic copolymers, for example, are copolymers whose structural units are arranged in a repeating sequence. If we represent structural units by uppercase letters, the shorthand notation for periodic copolymers reads as $$(ABC)_n$$. In this notation, $$ABC$$ indicates the period and $$n \in \mathbb{N}$$ ($$n \geq 2$$) indicates that the periodic copolymer consists of $$n$$ repetitions of the period.

Assignment

In this exercise, copolymers are represented as strings containing uppercase letters only. Each uppercase letter represents one of the structural units used as building blocks of the copolymer. Your task is to check whether or not a given copolymer is periodic. If this is the case, you also have to derive the shorthand notation for the periodic copolymer. This is done in the following way:

Example

>>> copolymer('(AB)_18')
'ABABABABABABABABABABABABABABABABABAB'
>>> copolymer('(ABBA)_9')
'ABBAABBAABBAABBAABBAABBAABBAABBAABBA'
>>> copolymer('(ABABBAAAABBB)_3')
'ABABBAAAABBBABABBAAAABBBABABBAAAABBB'
>>> copolymer('ABABBAAAABBBABABBAAAABBBBBABBAAAABBB')
'ABABBAAAABBBABABBAAAABBBBBABBAAAABBB'

>>> is_periodic('ABABABABABABABABABABABABABABABABABAB', 'AB')
True
>>> is_periodic('ABABABABABABABABABABABABABABABABABAB', 'ABA')
False
>>> is_periodic('ABABABABABABABABABABABABABABABABABAB', 'ABAB')
True
>>> is_periodic('ABABBAAAABBBABABBAAAABBBABABBAAAABBB', 'ABABBAAAABBB')
True

>>> period('ABABABABABABABABABABABABABABABABABAB')
'AB'
>>> period('ABABABABABABABABABABABABABABABABABAB', minimal_repetition=10)
'AB'
>>> period('ABABABABABABABABABABABABABABABABABAB', 20)
''
>>> period('ABBAABBAABBAABBAABBAABBAABBAABBAABBA')
'ABBA'
>>> period('ABABBAAAABBBABABBAAAABBBABABBAAAABBB')
'ABABBAAAABBB'
>>> period('ABABBAAAABBBABABBAAAABBBBBABBAAAABBB')
'ABABBAAAABBBABABBAAAABBBBBABBAAAABBB'
>>> period('ABABBAAAABBBABABBAAAABBBBBABBAAAABBB', 2)
''

>>> shorthand('ABABABABABABABABABABABABABABABABABAB')
'(AB)_18'
>>> shorthand('ABBAABBAABBAABBAABBAABBAABBAABBAABBA')
'(ABBA)_9'
>>> shorthand('ABABBAAAABBBABABBAAAABBBABABBAAAABBB')
'(ABABBAAAABBB)_3'
>>> shorthand('ABABBAAAABBBABABBAAAABBBBBABBAAAABBB')
'ABABBAAAABBBABABBAAAABBBBBABBAAAABBB'