Crunch Python Coding Test: Coconut Island ========================================= .. figure:: ./Coconut_Palms_on_the_Beach_5800249336.jpg You are shipwrecked on a desert island by yourself without any internet or electronic communications. Your resources include: - a laptop computer with 2 hours of battery life left (so try to complete this test in that amount of time or less) - coconut trees and lots of fallen coconuts - a big, flat, sandy beach You know there are airplanes flying overhead searching for you. You decide to spell the word **HELP** in big letters on the beach so it can be seen from above. You decide to write the letters by placing coconuts on the beach at specific coordinates. Using a palm tree on the edge of the beach as the origin, you will walk a certain integral number of paces east (x-axis) and then a certain integral number of paces south (y-axis) to place each coconut. You are very good at making each pace repeatably the same length. Part 1: Design the font ----------------------- Create a module named ``font.py``. Design a data structure to encode a font. The font will be rendered as coconuts placed at integer coordinates. Although your font only needs four letters (HELP), as a software developer you must generalize. So design your data structure so that you can easily extend your font to include more characters. In the ``font`` module create an instance of your font data structure and populate it with the letters H, E, L, and P. Each letter should fill a rectangle 5 paces wide and 7 paces tall. Define a function in the ``font`` module also named ``font()`` that returns this font instance. Part 2: Emit the render instructions ------------------------------------ Create another module named ``render.py``. Define in it a function with this signature:: def render_text(font, text): """ Given a font and a text string, return a list of coordinates that renders that text using that font. Assume a constant character width of 5 paces and put 3 paces of horizontal space between each character rendered. Do not worry about newlines or other control characters. font: Font data structure returned by font.font() text: string containing text to be rendered Return: A list of integer coordinates at which coconuts should be placed. Each coordinate pair is in the form (x, y) where x is the number of paces west from the reference tree, and y is the number of paces south. Example return value: [(1, 1), (1, 2), (1, 3), ... (27, 4), (26, 4)] """ Implement the ``render_text()`` function according to the docstring. Part 3: Test the rendered instructions -------------------------------------- Once you render the list of coordinates at which to place coconuts to form the letters, you realize that you don't want to blindly follow those instructions, pacing all over the beach, without knowing if the instructions are correct. You must test them on your computer first. Create a module named ``plot.py``. In the ``plot`` module define a function with this signature:: def plot_instructions(instructions): """ instructions: Sequence of (x, y) tuples as returned by render_instructions(). Clear the screen, then plot each coordinate location in the instructions by moving the cursor to a calculated row and column location and printing the '*' character. Do not worry about any distortion due to the fact that character cells are not square. """ Implement the ``plot_instructions()`` function according to the docstring. Implementation Hint ................... Most operating systems can run programs in a terminal emulater or console that handles basic ANSI escape sequences for cursor movement. Here are two helpful escape sequences you can print to control the terminal: +-------------------+-----------------------------------------------------+ | Escape sequence | Effect | +===================+=====================================================+ | ``\x1b[2J`` | Clear the screen | +-------------------+-----------------------------------------------------+ | ``\x1b[Y;XH`` | Move the cursor to location X, Y where X is the | | | column number starting at 1 and Y is the row number.| | | Note: the order of numbers in the escape sequence is| | | reversed: Y, X instead of X, Y. | +-------------------+-----------------------------------------------------+ Final Check ........... Write a ``main.py`` script with this code:: import font import plot import render instructions = render.render_text(font.font(), "HELP") plot.plot_instructions(instructions) For your test to pass, the ``main.py`` script must clear the screen and the print HELP in large letters made of '*' characters. Then finally you will have a hope of being rescued off of the island. Good luck!