Created
March 12, 2022 06:59
-
-
Save mrmartin/ee2ede2a9593ade7272b39371cba2ceb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ops = '+-*/^' | |
def calc(nums): | |
if(nums==[10958]): | |
print('hooray!') | |
raise TypeError("found 10958") | |
for space in range(1,len(nums)): | |
for op in ops: | |
if op == '+': | |
calc(nums[:space-1] + [nums[space-1]+nums[space]] + nums[space+1:]) | |
if op == '-': | |
calc(nums[:space-1] + [nums[space-1]-nums[space]] + nums[space+1:]) | |
if op == '*': | |
calc(nums[:space-1] + [nums[space-1]*nums[space]] + nums[space+1:]) | |
if op == '/': | |
if nums[space]!=0: | |
try: | |
calc(nums[:space-1] + [nums[space-1]/nums[space]] + nums[space+1:]) | |
except: | |
a=1 #do nothing, dead end | |
if op == '^': | |
if(nums[space]<1000): | |
try: | |
calc(nums[:space-1] + [nums[space-1] ** nums[space]] + nums[space+1:]) | |
except: | |
a=1 #do nothing, dead end | |
def splitnum(depth, root, num): | |
#reversing the place where the space is put means that those with fewwer spaces are processed first | |
for p in reversed(range(1,len(num))): | |
lnum = num[0:p] | |
rnum = num[p:] | |
#convert to array of numbers | |
txtnum = root+' '+lnum+' '+rnum | |
nums = [int(x) for x in txtnum.split()] | |
print(depth, nums) | |
calc(nums) | |
if(depth<6):#ship those with too many spaces, because they take too long to check all ops on | |
splitnum(depth+1,root+' '+lnum, rnum) | |
orig = '123456789' | |
splitnum(1,'',orig) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Exhaustive search (with some cases skipped) No solution ¯_(ツ)_/¯