Skip to content

@bcarl /main.py
Last active

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
#!/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
Something went wrong with that request. Please try again.