chiark / gitweb /
Script to tabulate results as a web page.
[matchsticks-search.git] / tabulate.py
1 #!/usr/bin/env python
2
3 import sys, os, re, fractions
4 from fractions import Fraction
5
6 import bounds
7
8 header_re = re.compile(r'^(\d+) into (\d+).*:.* ([\d/]+)')
9
10 def read(n,m):
11     best = 0
12
13     # Look for appropriate file(s).
14     for filename, nature in [("data/main.%d.%d" % (n,m), "e"),
15                              ("data/partition.%d.%d" % (n,m), "p"),
16                              ("data/manual.%d.%d" % (n,m), "m")]:
17         if os.path.exists(filename):
18             with open(filename) as f:
19                 header = f.readline()
20                 match = header_re.match(header)
21                 assert match is not None
22                 assert int(match.group(1)) == n
23                 assert int(match.group(2)) == m
24                 this_best = Fraction(match.group(3))
25                 if this_best > best:
26                     best = this_best
27                     best_nature = nature
28
29     bound, bound_type = bounds.upper_bound(n, m)
30
31     if best == bound:
32         tdclass = "known"
33         show_bound = ""
34     elif best_nature == 'e':
35         tdclass = "believed"
36         show_bound = ""
37     elif best_nature == 'p':
38         tdclass = "probable"
39         show_bound = " (– %s)"
40     else:
41         tdclass = "range"
42         show_bound = " – %s"
43     sys.stdout.write("<td class=\"%s\">" % tdclass)
44     sys.stdout.write(str(best))
45     if show_bound != "":
46         sys.stdout.write(show_bound % str(bound))
47     sys.stdout.write("</td>\n")
48
49 def main(args):
50     limit = 18 # FIXME: configurable
51     sys.stdout.write("""\
52 <html>
53 <head>
54 <title>Known bounds for stick-dissecting problem</title>
55 <style type="text/css">
56 table, td, th {
57     border: 1px solid black;
58     border-collapse: collapse;
59 }
60 td.known {
61     background-color: #00ff00;
62 }
63 td.believed {
64     background-color: #44ff44;
65 }
66 td.probable {
67     background-color: #88ff88;
68 }
69 td.range {
70     background-color: #ff8888;
71 }
72 </style>
73 </head>
74 <body>
75 <table>
76 """)
77     sys.stdout.write("<tr>\n")
78     sys.stdout.write("<th>n \\ m</th>\n")
79     for m in range(2,limit-1):
80         sys.stdout.write("<th>%d</th>\n" % m)
81     sys.stdout.write("</tr>\n")
82     for n in range(2,limit):
83         sys.stdout.write("<tr>\n")
84         sys.stdout.write("<th>%d</th>\n" % n)
85         for m in range(2,n):
86             read(n, m)
87         sys.stdout.write("</tr>\n")
88     sys.stdout.write("</table>\n")
89     sys.stdout.write("</body></html>\n")
90
91 if __name__ == "__main__":
92     main(sys.argv[1:])