Created
October 30, 2015 16:15
-
-
Save jackboot7/6eee0bd13623a024f291 to your computer and use it in GitHub Desktop.
FizzBuzz type problem: add commas to an integer.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import division | |
def addcomas(n, sep=1000): | |
cad = '' | |
if n < sep: | |
return n | |
while(n > 0): | |
cad = str(n % sep) + cad | |
n = n // sep | |
if n > 0: | |
cad = ',' + cad | |
return cad | |
def addcomas(n): | |
cad = str(n) | |
l = len(cad) | |
start = 3 | |
while start < l: | |
cad = cad[:l-start] + ',' + cad[l-start:] | |
start += 3 | |
return cad | |
print addcomas(1234567890) | |
print addcomas(90) | |
print addcomas(12111111234567890) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment