Interleave the letters of the words SHOE and COLD and you get the word SCHOOLED.
The same procedure can also be applied to interleave the letters of three words to produce a fourth.
However, interleaving the letters of a series of words is not limited to words having equal length. The general procedure repeatedly takes the next letter of the next word, where the last word is again followed by the first word. If all letters of a given word have been taken, that word is ignored in the remainder of the procedure. The procedure ends if all letters of all words have been taken.
Define a class Alternade that can be used to interleave the letters of a series of words. The objects of the class Alternade must at least support the following methods:
An initialization method that takes a string or a list of strings as its argument. After initialization, each newly created object must have a property words that refers to a copy of the given list of strings. If the argument passed to the initialization method is a string, this is just a shorthand notation for a list that contains only a single string.
A method __repr__ that returns a string representation of the object. This string representations contains a Python expression that can be used to instantiate a new object having the same value as the current object. The string representation is formatted as Alternade(li), where li is the string representation of the list of strings that is referenced by the property words of the object. However, if the list of strings contains only a single string, li is the string representation of the only string in the list.
A method __add__ that allows to use the + operator to add the current object $$s$$ to another object $$o$$ of the class Alternade. The result of the expression $$s + o$$ is a new object of the class Alternade whose property words refers to the list of strings that results from concatenating the lists of words of the objects $$s$$ and $$o$$.
A method __str__ that returns the string that results from interleaving the letters of the list of strings referenced by the property words of the current object.
To determine whether a given object has a certain type of specifications, you can of course use the built-in function type(o), that prints the type of specification of the object o. However, it is better to use the built-in function isinstance(o, t). This function prints a Boolean value that indicates whether the object o belongs to type t, or not.
>>> type(3) == int
True
>>> isinstance(3.14, int)
False
>>> isinstance(3.14, float)
True
>>> isinstance([1, 2, 3], list)
True
>>> word1 = Alternade('SHOE')
>>> word1.words
['SHOE']
>>> word1
Alternade('SHOE')
>>> print(word1)
SHOE
>>> words = ['COLD']
>>> word2 = Alternade(words)
>>> word2.words
['COLD']
>>> id(words) != id(word2.words)
True
>>> word2
Alternade('COLD')
>>> word3 = word1 + word2
>>> isinstance(word3, Alternade)
True
>>> word3.words
['SHOE', 'COLD']
>>> word3
Alternade(['SHOE', 'COLD'])
>>> print(word3)
SCHOOLED
>>> word1
Alternade('SHOE')
>>> word2
Alternade('COLD')
>>> word4 = Alternade('DEMO') + Alternade('RABAT')
>>> word4
Alternade(['DEMO', 'RABAT'])
>>> print(word4)
DREAMBOAT
>>> word5 = Alternade('SIR') + Alternade('ILL') + Alternade('MAY')
>>> word5.words
['SIR', 'ILL', 'MAY']
>>> word5
Alternade(['SIR', 'ILL', 'MAY'])
>>> print(word5)
SIMILARLY