Problem 3

Draw the Swiss flag:

-----------------------------------
|                                 |
|                                 |
|             +++++++             |
|             +++++++             |
|             +++++++             |
|      +++++++++++++++++++++      |
|      +++++++++++++++++++++      |
|      +++++++++++++++++++++      |
|             +++++++             |
|             +++++++             |
|             +++++++             |
|                                 |
|                                 |
-----------------------------------

(We extend the exercise a little bit so that it is a tidy bit more complex and fun to solve.) You are given the size of the image (as width and height).

Both the width and the height are multiples of 5.

Functions:

draw(width, height)

Draw the pattern of the size width x height and return the text lines.

draw(width: int, height: int) Lines[source]

Draw the pattern of the size width x height and return the text lines.

Requires
  • height % 5 == 0

  • height > 0

  • width % 5 == 0

  • width > 0

Ensures
  • len(result) > 0

  • all(len(line) == width for line in result)

  • len(result) == height

  • ONLY_DASHES_RE.match(result[-1])

    (bottom border)

  • ONLY_DASHES_RE.match(result[0])

    (top border)

  • all(
        line.startswith('|') and line.endswith('|')
        for line in result[1:-1]
    )
    

    (Vertical border)

  • all(
        (
                center := len(line) // 2,
                line[:center] == line[center + 1:][::-1]
                if len(line) % 2 == 1
                else line[:center] == line[center:][::-1]
        )[1]
        for line in result
    )
    

    (Horizontal symmetry)

  • all(
        result[i - 1] == result[len(result) - i]
        for i in range(1, int(len(result) / 2))
    )
    

    (Vertical symmetry)