In hsum
, you reset s
to 0
in every loop iteration. You should probably move that up, outside the loop.
In your code to find the best fit, you're appending the p values to P, but you don't keep any information about the quality of the fit. min(P)
finds the lowest p, not the one that fits best. Store (p, hsum(p))
tuples and find the minimum by the H(p) values. The min function takes a key
argument that can help you with that. (With a bit of simplification, you wouldn't even need to make the tuples.)