Cotton fabric used to be an expensive and scarce product. One dealt with it very economically: remainders or old pieces of clothing weren't thrown away, they were recycled. Old pieces were stitched together to serve as a protection against the cold. For example, soldiers in the Middle Ages wore quilted tunics over their clothing in order to stay warm. The Romans knew a kind of quilted bedding they called culcita. This word was later corrupted to cowlte, of which the word quilt was deducted.
Like so many workmanship and handiwork techniques, one made a virtue a necessity when quilting. Quilting a couple of layers of fabric became more and more decorative and was done by patterns, one more artistic than the other. If the upper layer of the quilt consisted of small, sown together pieces of fabric, it is called a patchwork-quilt. Today, the terms patchwork and quilt are both used to indicate the same thing, since most quilts are constructed from patchwork.
Today, there is an abundance of cheap cotton fabrics and there are affordable warm blankets and one of course no longer depends on remainders or old clothing. Making patchwork hasn't been a necessity for a while now, but it is still done as a hobby.
Define a class Quilt with which the patterns of a quilt can be represented. Every quilt corresponds with a representation of a number of characters in the format of a grid. For example, a $$2\times 4$$ quilt could look as follows:
//-\
++||
The class must implement the following methods:
An initializing method __init__ to which three arguments must be given: i) the number of rows $$m$$, ii) the number of columns $$n$$ and iii) a string. The quilt is filled out from left to right and from top to bottom with the consecutive characters from the given string. This string must have as many characters as the product of the number of rows $$m$$ and the number of columns $$n$$ that the quilt consists of. The initializing method must raise an AssertionError with the text invalid configuration if these conditions are not met. A quilt can only consist of valid characters, and the initializing method should also raise an AssertionError with the text invalid configuration if this should not be the case. By valid characters, we mean these symbols that we can still represent by a character from an ASCII character set if we rotate them 90 degrees: \,/,+,*,-,|,o,x.
A method __str__ that prints a string representation of the quilt. This string representation consists of $$m$$ lines, where every line has $$n$$ characters. The string representation itself does not end in a newline.
A method __repr__ that prints a string representation of the quilt. This string representation reads like a Python expression that makes a new object of the class Quilt has the same state.
A method rotate that returnss a new Quilt object, with a pattern that is rotated 90 degrees clockwise with regard to the pattern of the object on which this method is called.
A method __add__ that allows "sewing" two quilts together. Only quilts with an equal height can be sewn together. The method must raise an AssertionError with the text quilts do not have equal height if two quilts do not have an equal height. Otherwise the method must print a new Quilt object, of which the pattern consists of the patterns that were sewn together of the two objects that are summed up.
Objects of the class Quilt must be immutable. No other method may adjust the internal state of an object from the class Quilt. In other words, the internal state of the objects may not be changed after being initialized, which only allows making new objects.
Remark: if you wish to use the interactive Python session below as a doctest, all backslashes of the output (that have already been escaped once below), must be escaped. In other words, all backslashes in the actual result must be replaced by a double backslash.
>>> quilt = Quilt(2, 2, '//++')
>>> print(quilt)
//
++
>>> quilt += Quilt(2, 2, '-\\||')
>>> print(quilt)
//-\
++||
>>> quilt
Quilt(2, 4, '//-\\++||')
>>> quilt = quilt.rotate()
>>> print(quilt)
+\
+\
-|
-/
>>> quilt
Quilt(4, 2, '+\\+\\-|-/')
>>> quilt += Quilt(2, 2, '-\\||')
Traceback (most recent call last):
AssertionError: quilts do not have an equal height
>>> quilt = quilt.rotate()
>>> print(quilt)
||++
\-//
>>> Quilt(2, 3, '++++')
Traceback (most recent call last):
AssertionError: invalid configuration
>>> Quilt(2, 3, 'oxooXo')
Traceback (most recent call last):
AssertionError: invalid configuration