[CentralOH] DoJo Mumbilings for March 28, 2019

neil ludban neil.ludban at gmail.com
Thu Mar 28 22:09:20 EDT 2019


#!/usr/bin/env python3.7

import re
from itertools import chain

def load_table(text):
    for row in text.splitlines():
        m = re.match(r'[0-9]+\s+([1-4])\s+([A-D])\s+([0-9])', row)
        if not m:
            continue
        yield m.groups()
    return

def make_report(data):
    sep = '+----------+-----+-----+-----+-----+'
    yield sep
    headers = [ 'A', 'B', 'C', 'D' ]
    it = chain(headers, data)
    sep = sep.replace('-', '=')
    for Category in ( 'Category', '1', '2', '3', '4' ):
        A, B, C, D = next(it), next(it), next(it), next(it)
        yield F'| {Category:>8} |  {A}  |  {B}  |  {C}  |  {D}  |'
        yield sep
        sep = sep.replace('=', '-')
    return

sample_table_1 = '''
===  =========  =====  =====
ID   Category   Type   Value
===  =========  =====  =====
1    1          A      2
2    3          D      5
3    4          B      7
4    2          C      8
5    4          D      9
6    4          C      1
7    1          D      3
8    3          C      9
9    2          A      5  XXX
10   3          A      4
11   1          B      6
12   4          A      2
13   2          D      6
14   3          B      3
15   2          B      8
16   1          C      0
===  =========  =====  =====
'''

sample_report_1 = '''
+----------+-----+-----+-----+-----+
| Category |  A  |  B  |  C  |  D  |
+==========+=====+=====+=====+=====+
|        1 |  2  |  6  |  0  |  3  |
+----------+-----+-----+-----+-----+
|        2 |  5  |  8  |  8  |  6  |
+----------+-----+-----+-----+-----+
|        3 |  4  |  3  |  9  |  5  |
+----------+-----+-----+-----+-----+
|        4 |  2  |  7  |  1  |  9  |
+----------+-----+-----+-----+-----+
'''

rows = [ row[2] for row in sorted(load_table(sample_table_1)) ]
report = '\n'.join(make_report(rows))

print(report)
assert report.strip() == sample_report_1.strip()

#--#


On Thu, Mar 28, 2019 at 3:53 PM Travis Risner <deeppunster at gmail.com> wrote:

> Hi folks,
>
> Here is a problem proposed by one of our own members.  (The problem is
> in the attached PDF.)
>
> Travis
>
> _______________________________________________
> CentralOH mailing list
> CentralOH at python.org
> https://mail.python.org/mailman/listinfo/centraloh
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/centraloh/attachments/20190328/f500fa5c/attachment.html>


More information about the CentralOH mailing list