If i had the number 123 how would i make it 321?
I'll bite - >>> int(str('123')[::-1])
-
321
-
>>>
6 12879 bvdet 2,851
Expert Mod 2GB
I'll bite - >>> int(str('123')[::-1])
-
321
-
>>>
Add 2 to the first digit and subtract 2 from the last digit?
or add 198 to the first number??
This code was done for some reason some time ago but I don't remember why. - from collections import deque
-
-
x = 123
-
new_x = 0
-
while x > 0:
-
x, y = divmod(x, 10)
-
new_x = new_x*10 + y
-
print new_x
-
-
x = 123
-
d = deque()
-
for num in str(x):
-
d.appendleft(num)
-
print "using deque",
-
print "".join(d)
bvdet 2,851
Expert Mod 2GB
One more for the sake of completeness for any searchers. - x = 12345
-
new_str = ""
-
for ch in str(x):
-
new_str = ch + new_str
-
print int(new_str)
bvdet 2,851
Expert Mod 2GB
Here's another way, also using built-in function divmod(): - >>> def digits(n):
-
... results = []
-
... while n:
-
... n, x = divmod(n, 10)
-
... results.insert(0, x)
-
... return results
-
...
-
>>> digits(123456789)
-
[1, 2, 3, 4, 5, 6, 7, 8, 9]
-
>>> sum([d*10**i for i, d in enumerate(digits(123456789))])
-
987654321
-
>>>
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Radioactive Man |
last post by:
In python 2.3 (IDLE 1.0.3) running under windows 95, I get the
following types of errors whenever I do simple arithmetic:
1st example:
>>> 12.10 + 8.30
20.399999999999999
>>> 1.1 - 0.2...
|
by: Brian van den Broek |
last post by:
Hi all,
I guess it is more of a maths question than a programming one, but it
involves use of the decimal module, so here goes:
As a self-directed learning exercise I've been working on a...
|
by: Tuvas |
last post by:
I've been thinking about writing a program to generate the world's
largest prime numbers, just for the fun of it. This would require being
able to hold an 8000000 digit number into memory (25...
|
by: Kosio |
last post by:
Hello,
I know of a way to extract digits from a number using the %10 and
divide by 10. But I am wondering if there is an algorithm out there
that does not use a divide by 10 feature.
The...
|
by: Frinton |
last post by:
Hi,
I am trying to do some calculations on large numbers (ie
7,768,489,957,892,578,474,792,094 / 12,280) and no matter what I do it
doesn't get it quite right. Its always somewhere between 10...
|
by: ocean |
last post by:
Write a program that prompts the user to input an integer and then outputs both the individual digits of the number and the sum of the digits.
For example, it should output the individual digits...
|
by: Umesh |
last post by:
Is there any way by which i can do it? Thanks.
|
by: thehobbit |
last post by:
Hi,
Could anyone give ideas on how to add 4 20 digit numbers in ANSI C and
pass the result back to a calling program in COBOL? We were able to
add up to 15 digit numbers without any problems,...
|
by: Abedin |
last post by:
I have 9 digits in form of "111020402". Then I have another 9 digits
in form of "111020403". I have 100,000 records of these two 9-digit
numbers.
I want to filter this as follows:
District:...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
| |