Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
doubling a number
#3
Maybe some simple doubling function?

def double(text):
    doubled = []
    carry = 0
    for num in text[::-1]:
        if num in ('.', ','):
            doubled.append(num)
        else:
            quotient, reminder = divmod(2 * int(num), 10)
            doubled.append(str(reminder+carry))
            carry = quotient
    if carry:
        doubled.append(str(carry))

    return ''.join(doubled[::-1])


for num in ("1", "10", "12.5", "1.9", "99", "99.7899", "99.999"):
    print(f"{num}, {double(num)}")

1, 2
10, 20
12.5, 25.0
1.9, 3.8
99, 198
99.7899, 199.5798
99.999, 199.998
Gribouillis likes this post
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
doubling a number - by Skaperen - Jul-15-2023, 04:55 PM
RE: doubling a number - by Gribouillis - Jul-15-2023, 07:08 PM
RE: doubling a number - by perfringo - Jul-16-2023, 08:36 AM
RE: doubling a number - by Gribouillis - Jul-16-2023, 03:56 PM
RE: doubling a number - by Skaperen - Jul-16-2023, 11:25 PM
RE: doubling a number - by ibreeden - Jul-17-2023, 08:39 AM
RE: doubling a number - by Skaperen - Jul-19-2023, 01:53 AM
RE: doubling a number - by ibreeden - Jul-19-2023, 06:41 AM
RE: doubling a number - by Skaperen - Jul-25-2023, 10:20 PM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020