public
Last active

  • Download Gist
main.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
#!/usr/bin/env python
 
"""
Another fun python variable manipulation question is: given an int, write a one
liner that returns the string representation of the int, but with a comma every
three decimal places starting from the right. (i.e., 1 return "1", and 1000
returns "1,000")
 
(Rules: the one line cannot use anything that has to be imported, and cannot use
any string formatting)
 
http://python.reddit.com/comments/15z6xg/q/c7re8uf
"""
 
import unittest
 
def stringify_with_commas(the_int):
return ''.join(str(the_int)[::-1][i:i+3] + ',' for i in range(0, len(str(the_int)), 3))[::-1][1:]
 
class TestSequenceFunctions(unittest.TestCase):
 
def setUp(self):
self.func = stringify_with_commas
 
def test_one(self):
self.assertEqual(self.func(1), '1')
 
def test_one_thousand(self):
self.assertEqual(self.func(1000), '1,000')
 
def test_one_million(self):
self.assertEqual(self.func(1000000), '1,000,000')
 
def test_one_million_and_change(self):
self.assertEqual(self.func(1234567), '1,234,567')
 
if __name__ == '__main__':
unittest.main()

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.