Why Python don't accept 03 as a number?

Avi Gross avigross at verizon.net
Sat Dec 8 01:08:59 EST 2018


[[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

Why does 05 ever even appear in your solution?

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.

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.


-----Original Message-----
From: Python-list <python-list-bounces+avigross=verizon.net at python.org> On
Behalf Of jfong at ms4.hinet.net
Sent: Friday, December 7, 2018 11:25 PM
To: python-list at python.org
Subject: Re: Why Python don't accept 03 as a number?

Ian at 2018/12/8 UTC+8 AM11:28:34 wrote:
> What is it exactly that you're trying to accomplish with this? Perhaps 
> there's a better way than using eval.

This problem comes from solving a word puzzle,
    ab + aa + cd == ce
Each character will be translate to a digit and evaluate the correctness,
    03 + 00 + 15 == 18

--
https://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list