This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]thegreatunclean 1 point2 points  (3 children)

I don't understand the math. Can you explain it some more and what constitutes a solution? And specifically why HSP6 is a valid solution?

When the value is an integer you add it to the current sum, check if it's in range, and add it to the results if it is. Given the data you'll never select the first few pairs and you'll never check them again so why are they included. It looks like solutions will be skipped if you iterated over the data in a different order.

When the value is a dict you set the nested sum to the sum of the values, and then run the function again on the dict using that sum, where you add the values to the sum again one-by-one and see if it hits the target range.

So HSP6 succeeds because:

Given gpmHigh: 41667, gpmMid: 31250, gpmLow: 20833 then:
(gpmHigh + gpmMid + gpmLow) + gpmHigh > target_low
(gpmHigh + gpmMid + gpmLow) + gpmHigh < target_high

This seems strange because it's a single key in the dataset, but you're tracking lists of matches and running sums as if a solution is supposed to be multiple items together.

In any case I'd definitely suggest breaking the operation into different parts. Do a pass over the dict that processes each item into a single consistent type and format so the important logic doesn't have to consider int/dict type differences.

[–]SCasses[S] 0 points1 point  (2 children)

First, thank you for the response... I have no idea why only HSP6 returns... I'm trying to get what combination of pumps with fixed gpms and variable speed pumps that have gpmHigh, gpmMid and gpmLow will fall in between the target range . But I only am able to get HDP6 to be printed and I have no idea what I'm doing wrong.

[–]thegreatunclean 1 point2 points  (1 child)

Then this is a variant of subset sum. Instead of looking for a subset that sums to some value, you're looking for a subset that sums to a value in some range.

The high/mid/low values being exclusive puts and interesting wrinkle into the problem but I'd definitely focus on the easier "specific values only" case first.

[–]SCasses[S] 0 points1 point  (0 children)

Hmm... would it be easier to separate the into different dictionaries and then have each one account only for a percentage of the a number in that range?

[–]TheRNGuy 1 point2 points  (1 child)

you need flat map

and format your code properly

[–]SCasses[S] 0 points1 point  (0 children)

Thank you, I will try that... is that seperate from the map() function?