[CentralOH] Python Parsing Puzzle
Eric Floehr
eric at intellovations.com
Sat Dec 6 16:59:21 CET 2014
I was talking with Jim about a problem I had faced recently with some work
with a client, and he thought it would make an interesting group puzzle.
I had a text document with 28 groups of 5 equations. Each group of
equations followed the same form, namely 4 formulas with 13 inputs all
following the same form, and an equation with the results of those four
equations.
So if we call the four formulas H1 through H4, each of the 'H' equations
are of the form:
TanH(0.5 + (Constant + CoefficientA * A + ... + CoefficientM * M)
for each of 13 inputs passed in (A-M, save I for readability).
And the final equation has the form:
Constant + Coefficient1 * H1 + Coefficent2 * H2 + Coefficient3 * H3 +
Coefficient4 * H4
For for each of the 28 groups, you pass in 13 inputs and get one output,
the solution to the final equation.
Each of the constants and coefficients are numbers, but vary between
formulas. Here is a sample text document with one set of formulas:
https://www.dropbox.com/s/keckps3x8wpy4iy/formulas.txt?dl=0
The challenge is that you have to turn these 140 formulas into usable
Python code.
My first solution, on a set of far fewer formulas, was to create a
dictionary with the constants and coefficients, and a single set of Python
functions to solve given the specific dictionary entry, like:
consts = { 'GROUP_1' :
{ 'FORMULA':
{'CONST': 1.23, 'COEFH1': 2.34, 'COEFH2': 3.45, 'COEFH3': 4.56,
'COEFH4': 6.55},
'H1':
{'CONST': 5.67, 'COEFA': 5.44, ... , 'COEFM': 9.33},
'H2':
{ ... },
...
},
'GROUP_28':
...
}
And I manually copied the numbers in, cutting and pasting. With 140
formulas with a total of 1708 constants, this became impractical, so I did
something different.
How would you solve this problem?
-Eric
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/centraloh/attachments/20141206/b53ea922/attachment.html>
More information about the CentralOH
mailing list