Create a gist now

Instantly share code, notes, and snippets.

@bcarl /
Last active Dec 10, 2015

What would you like to do?
#!/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)
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__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment