# [CentralOH] Python Parsing Puzzle

Nick Albright nick.albright at gmail.com
Sat Dec 6 18:53:08 CET 2014

```Oooo!  I like puzzles! :)

This isn't at all safe for public usage/non sanitized formulas, BUT...  I'd
parse out each group.  And then once I have a group, I'd parse out each the
master formula, and the H1-H4 formulas.

And since they are nearly in python ready format, I'd do a little
manipulation (like converting "TanH" to "math.tanh") and have python "eval"
it for me!

Dirty Laundry Proof of Concept can be found here:   (Uses the supplied data
file as input)

http://pastebin.com/JNFFXSjJ

(Next step would be to generalize the H1 - H4 bits/stop hard coding, and
stop the "copy and paste" pain :)

Peace!
-Nick

On Sat, Dec 6, 2014 at 10:59 AM, Eric Floehr <eric at intellovations.com>
wrote:

> 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
>
>
> _______________________________________________
> 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/20141206/831b75a1/attachment.html>
```