python - Calculating all possible combinations using a list of random numbers and ALL simple math operators to reach a given target -


i writing simple python script generates 6 numbers @ random (from 1 100) , larger number (from 100 1000). goals script to:

  1. calculate of possible combinations using @ least 2 numbers , of simple math operations (adding, subtracting, multiplying , dividing)

  2. output of combinations total within 10 above or below larger number 'matches'

the list of numbers need not exhausted, repeating numbers isn't accepted. plus don't care if code efficient or not (if decides post - can post mine far if needs - preferably post in python); long works, i'm happy optimize it.

i have attempted myself, fail program ended runtime error. tried putting in counter stop loop after x passes (where x small number such 50), makes matters worse keeps on going infinitely.

i've done research, , found (computing target number numbers in set - second last answer) closest found meet requirements hasn't got quite there yet.

thanks help! :-)

edit: here code:

import random, time, operator  = 0 numlist = [] while != 6:     number = random.randint(1, 100)     numlist.append(number)     += 1  largenumber = random.randint(100, 1000) print(numlist) print(largenumber)  def operationtesting():     a, c, m, total = 0, 0, 0, 0     totalnums = 0     operators = ['+', '-', '*', '/']     while total != largenumber:         in numlist[m]:             c in numlist[m+1]:                 print(a)                 print(c)                 if == c:                     operationtesting()                 else:                     b = random.choice(operators)                     if b == '+':                         summednums = operator.add(int(a), int(c))                         print(summednums)                         totalnums = totalnums + summednums                     elif b == '-':                         summednums = operator.sub(int(a), int(c))                         print(summednums)                         totalnums = totalnums + summednums                     elif b == '*':                         summednums = operator.mul(int(a), int(c))                         print(summednums)                         totalnums = totalnums + summednums                     elif b == '/':                         summednums = operator.floordiv(int(a), int(c))                         print(summednums)                         totalnums = totalnums + summednums                     print(totalnums)                     systemexit(none)  operationtesting() 

a neat way using reverse polish notation or postfix notation. notation avoids need brackets want if doing using conventional arithmetic operator precedence etc.

you can brute force if not bothered time efficiency. need consider want division - if 2 numbers not divide exactly, want return result 'invalid' in way (i guess so), or return floored division? note latter might give invalid answers...

consider test case of numlist = [1,2,3,4,5,6]. in rpn, this

rpn           equivalent 123456+++++   (1+(2+(3+(4+(5+6))))) 123456++++-   (1-(2+(3+(4+(5+6))))) 123456+++-+   (1+(2-(3+(4+(5+6))))) ... 12345+6+-++   (1+(2+(3-((4+5)+6)))) 12345+6-+++   (1+(2+(3+((4+5)-6)))) ... 

and on. can see sufficient combinations, can combinations of numbers, operators , brackets. brackets important - take 3 numbers obviously

1+2*6  

is interpreted

(1 + (2*6)) == 13 

and quite different

((1+2)*6) == 18 

in rpn, these 126*+ , 12+6* respectively.

so, you've got generate combinations in rpn, develop rpn calculator evaluate them.

unfortunately, there quite lot of permutations 6 numbers (or subset thereof). first can have numbers in order, thats 6! = 720 combinations. need n-1 == 5 operators , can 1 of 4 operators. that's 4**5 == 1024 permutations. 5 operators can in 1 of 5 positions (after first pair of numbers, after first 3, after 4 , on). can have maximum 1 operator in first position, 2 in second , on. that's 5! == 120 permutations. in total have 720*1024*120 == 88473600 permutations. thats 9 * 10**7 not beyond realms of computation @ all, might take 5 minutes or generate them on quick computer.

you improve on "chopping" search tree

  1. loads of rpn combinations arithmetically identical (e.g. 123456+++++ == 12345+6++++ == 1234+5+6+++ etc) - use prior knowledge improve generate_rpn_combinations didn't generate them
  2. identifying intermediate results show combinations never satisfy criterion , not exploring further combinations down road.

you have send each string rpn calculator. these easy code , typical programming exercise - push values onto stack , when come operators, pop top 2 members stack, apply operator , push result onto stack. if don't want implement - google minimal python rpn calculator , there resources there you.

note, don't have use 6 numbers. rather implementing separately, suggest checking intermediate results when evaluating combinations 6 numbers, if satisfy criterion, keep them too.


Comments

Popular posts from this blog

css - SVG using textPath a symbol not rendering in Firefox -

Java 8 + Maven Javadoc plugin: Error fetching URL -

order - Notification for user in user account opencart -