Skip to content

Instantly share code, notes, and snippets.

@bcarl
Last active December 10, 2015 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bcarl/4489948 to your computer and use it in GitHub Desktop.
Save bcarl/4489948 to your computer and use it in GitHub Desktop.
#!/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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment