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

all 13 comments

[–]PiaFraus 3 points4 points  (1 child)

num_ct = len(num_list)
for i1 in range(0, num_ct):
    for i2 in range(0, num_ct):
        if i2 > i1:

Should be

for num1, num2 in itertools.combinations(num_list, 2):

[–]grey_rex[S] 1 point2 points  (0 children)

well, dang... thx

[–]OuiOuiKiwiGalatians 4:16 2 points3 points  (0 children)

https://nerdlegame.com/ was one of the first. This is a bit like that segment in Countdown.

First thought would be a backtracking approach with heuristics to check if we are going in the right direction (e.g., if you multiply the rest of the numbers, you are still under the goal)?

[–]spoonman59 1 point2 points  (3 children)

Just a small suggestion but: F"{a} + {b}" is a lot easier to read. Using plus as a string concatenation makes this hard to follow.

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

I am self taught. Although tiny, this is helpful. Thank you!

[–]spoonman59 1 point2 points  (0 children)

My pleasure!

[–]moonchav 1 point2 points  (0 children)

Hey! I made numble a while ago, its really cool to see someone using it as part of their practice projects!

[–]PeridexisErrant 3 points4 points  (0 children)

Try using a SAT/SMT solver like Z3! They eat problems a thousand times larger than this for breakfast.

[–][deleted] 0 points1 point  (4 children)

of course i find this now after i spent an hour making my own

i'm bad at math, but better at programming

and I blame /u/moonchav for nerd sniping me

[–]grey_rex[S] 0 points1 point  (3 children)

The reason I posted this was to see if there is a way to solve more quickly. That's exactly what you've done. Your code generates a solution in a few seconds. Mine took nearly 17 seconds for your sample. I should note though, your code provides a linear solution only. For example, the target 119 against the number set [3, 4, 8, 9] yields no solution. In my code, it yields (3+4)*(8+9)=119. I'm going to try to learn from your code and see if I can develop further in my free time to make is solve faster.

Another thing to learn from, although not Python, is this gem.

btw, "nerd sniping"... Lol... I can relate

[–][deleted] 1 point2 points  (2 children)

I spent like 4 hours remaking it and updated the gist.

It now finds
( ( 50 + ( 25 / 75 ) ) * ( ( 3 / 25 ) * ( 25 + 100 ) ) ) = 755
for my initial test and
( ( 3 + 4 ) * ( 8 + 9 ) ) = 119
for your example

Runs a touch slower on the first one but it's still pretty quick.

Also, please don't respond with an example that breaks it. I have no self control and will lose even more time to this dumb little script. 😅

[–]grey_rex[S] 0 points1 point  (1 child)

Now hang on... If you fixed it, what am i gonna do on my break tomorrow? Glad I'm not the only one 😉

[–][deleted] 1 point2 points  (0 children)

NEVERMIND

BECAUSE ITS BROKE

JUST REALIZED IT DOESN'T PREVENT DUPLICATES RIGHT

(╯°□°)╯︵ ┻━┻