In the previous assignment, we have
represented a polymer as a list containing the positions of the successive
monomers of the polymer. Each position is represented as a tuple
Write a function showPolymer which prints the structure of a polymer for a given list of positions of the monomers of a polymer (which is to be passed to the function as argument). To do this, just follow these steps:
Define a two-dimensional matrix
The even rows and columns of the matrix are used to indicate where the
monomers of the polymer are located. Monomers are indicated by the
letter o, with the exception of the first monomer that is designated by
the letter x and the last monomer which is indicated with an asterisk
(*). For each monomer at position
(0, 0), (1, 0), (2, 0), (3, 0), (4, 0), (4, 1), (4, 2), (4, 3), (3, 3), (2, 3), (2, 2), (2, 1), (3, 1), (3, 2)
looks like
The empty cells in the matrix representation still contain a string that consists of a single space.
The output is generated by printing the rows of the matrix one by one (from top to bottom), in which the various characters in the columns of the same row are written one after the other. The preceding example then delivers the following output.
o-o-o | | o * o | | | o-o o | x-o-o-o-o
>>> polymer1 = [(0, 0), (1, 0), (2, 0), (2, 1), (2, 2), (1, 2), (0, 2),
... (0, 1), (1, 1)]
>>> showPolymer(polymer1)
o-o-o
| |
o-* o
|
x-o-o
>>> polymer2 = [(0, 0), (1, 0), (2, 0), (2, -1), (3, -1), (4, -1), (4, 0),
... (4, 1), (3, 1), (3, 0)]
>>> showPolymer(polymer2)
o-o
| |
x-o-o * o
| |
o-o-o
>>> polymer3 = [(0, 0), (0, -1), (1, -1), (1, -2), (1, -3), (1, -4), (0, -4),
... (0, -3), (-1, -3), (-2, -3), (-2, -2), (-2, -1), (-1, -1),
... (-1, -2), (0, -2)]
>>> showPolymer(polymer3)
x
|
o-o o-o
| | |
o o-* o
| |
o-o-o o
| |
o-o