Skip to content

Instantly share code, notes, and snippets.

@jackboot7
Created October 30, 2015 16:15
Show Gist options
  • Save jackboot7/6eee0bd13623a024f291 to your computer and use it in GitHub Desktop.
Save jackboot7/6eee0bd13623a024f291 to your computer and use it in GitHub Desktop.
FizzBuzz type problem: add commas to an integer.
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