Is there a way to subtract 3 from every digit of a number?

Ming ming at pgp.cool
Sat Feb 20 22:12:24 EST 2021


On Sat, Feb 20, 2021 at 09:40:48AM -0500, C W wrote:
> Hello everyone,
> 
> I'm curious if there is a way take number and back each digit by 3 ?
> 
> 2342 becomes 9019
> 8475 becomes 5142
> 5873 becomes 2540
> 
> The tricky part is that 2 becomes 9, not -1.
> [...]

I just wrote a very short code can fulfill your needs:

a = 2342
b = int("".join(map(lambda x: str((int(x)-3)%10) ,list(str(a)))))

It does the following things:
1. Convert a number into a string, and then convert this string into 
a list of single characters.
2. Write a lamdba expression to apply your conversion rules to a
single-character type number (just subtract 3 and then modulo 10).
3. Apply the lambda expression to the above string list through map.
4. Finally join the modified list into a string and convert it into an
integer.

-- 
OpenPGP fingerprint: 3C47 5977 4819 267E DD64  C7E4 6332 5675 A739 C74E
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <https://mail.python.org/pipermail/python-list/attachments/20210221/323b05f1/attachment.sig>


More information about the Python-list mailing list