Avi Gross at 2018/12/8 UTC+8 PM2:09:20 wrote: > [[READERS DIGEST CONDENSED ANSWER: use int("string") ]] > > Since we all agree python will not make notations like "05" work > indefinitely, and the need expressed is how to solve a symbolic puzzle (see > message below) then it makes sense to look at alternate representations. > > I have a question first. How are you solving your puzzles? > > ab + aa + cd == ce
Try all the combinations:-) The only way I can think of is try-error. It takes no more 10 lines to go from "ab + aa + cd == ce" to yield one correct answer, such as "03 + 00 + 15 == 18", using itertools' permutations(), string's translate() and re. > Why does 05 ever even appear in your solution? I don't know. There is total 192 answers for this puzzle anyway. > Are you generating all possible answers by setting each variable to one of 0 > to 9 then the second to any of the remaining nine choices then the third to > the remaining 8 and so on? For any method like that, you can presumably make > each component like > > ab = 10*a + b > > in the loop. > > Similarly for aa and cd and ce. If the equality above is true, you found the > solution and break out. If there can be multiple solutions, note the > solution and keep going. But note for the 5 variables above, you are testing > 10*9*8*7*6 combinations. > > Another idea is to use strings like "05" as bizarrely, the function int() > seems to be an ex eption that does NOT care about leading whitespace or > zeroes: > > >>> int("05") > 5 > >>> int(" 0005") > 5 > > And even handles all zeroes: > > >>> int("000000") > 0 > > You can also use lstrip() with an argument to remove zeros: > > >>> a = eval("05".lstrip("0")) > > >>> a > > 5 > > If you are in a situation where you only want to remove leading zeroes if > the following character is a digit and not "o" or "b" or "x", use regular > expressions or other techniques. As far as the leading zero problem was concerned, the simplest way is using re.sub() > I will just toss in the possible use of the SymPy module to do actual > symbolic computations to solve some of these. Perhaps a tad advanced. I don't know SymPy and if it can shorten the execution time. But I am very curious about if there is other algorithm which can apply to this problem:-) --Jach -- https://mail.python.org/mailman/listinfo/python-list