Last active
December 10, 2015 20:39
-
-
Save bcarl/4489948 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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