M12) Writing a Markdown report
Markdown is a lightweight markup language with plain-text formatting syntax. It is often used to format README files, write messages in online discussion forums, and create rich text using a plain-text editor. Listing 1 presents a main function that tests a function matrix_to_markdown that converts a matrix and headers (see Figure 1) to a Markdown table (see Figure 1 (b)).
matrix_to_markdown function to convert the matrix and headers shown in Figure 1 (a) into the Markdown table shown in Figure 1 (b).
The main function does the following:
- Calls the
read_datafunction to return the supplied nested listdata. - Initializes the list
headerswith the table headers:OrderId,OrderLine, andQuantity. - Calls the
matrix_to_markdownfunction to convert the matrix and headers to Markdown format (as shown in Figure 1 (b)) and writes the result toorders.md, replacing any existing contents. The file is closed on leaving thewithblock. All file operations succeed.
List headers:
\[ \begin{bmatrix} OrderId & OrderLine & Quantity \end{bmatrix}. \]
Nested list data:
\[ \begin{bmatrix} 1 & 1 & 2 & 2 & 3 & 3 \\ A & B & A & B & A & B \\ 10 & 20 & 15 & 25 & 30 & 40 \end{bmatrix}. \]
The nested list has three outer items, one for each attribute. Each inner list contains the values of that attribute for the six data points. Therefore, attributes are selected with the first index and data points with the second index.
main function to test the matrix_to_markdown function (columns represent data points and rows represent attributes).
matrix_to_markdown function should transform the input data and headers (as shown in Figure 1 (a)) into a Markdown table (as shown in Figure 1 (b)).
In Listing 3, we have four alternative implementations of the matrix_to_markdown function. The function receives a nested list data and a list headers with the column headers and returns a string in Markdown format representing the table. It should handle an empty matrix by returning an empty string. The header is created by the get_header_row function, which returns this string, including the newline after the alignment row:
See Listing 2.
Which implementation of the matrix_to_markdown function in Listing 3 correctly converts the matrix and headers (e.g., as shown in Figure 1 (a)) to the complete Markdown table written to orders.md (as shown in Figure 1 (b)) when main (in Listing 1) is executed?
matrix_to_markdown function. The function receives a nested list data and a list headers and returns a string in Markdown format representing the table. The function creates the header using get_header_row and then iterates over the data matrix to create the data rows. The string "\n" inserts a newline character (Question 1).
read_data()is a supplied helper with no arguments. It returns the complete nested list shown in Figure 1 (a); for example, its first inner list is[1, 1, 2, 2, 3, 3].get_header_row(headers)returns the Markdown header and alignment rows, with a newline after each. Withheaders = ["Item"], it returns"| Item |\n|:---:|\n".main()takes no arguments and writes the converted string toorders.md; it has no explicit return value.len(sequence)returns its number of items;len([2, 4])is2.range(stop)visits0up to, but excluding,stop;range(0)is empty.str(4)returns the string"4".open(path, "w", encoding="utf-8")opens a text file for writing, replacing its contents, and returns a file object.file.write("Hello\n")writes that text and returns its character count,6. Thewithblock closes the file when it ends.
Evaluate the bounds of the loops and the way the data matrix is accessed.