Created
April 13, 2019 18:53
-
-
Save phalbert/21e3f3c998306a328be148a60a6eb4a8 to your computer and use it in GitHub Desktop.
Split an integer into a roughly equal number of parts
This file contains hidden or 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
def splitInteger(num,parts): | |
array = [] | |
for i in range(parts): | |
if i < num % parts: | |
value = num // parts + 1 | |
else: | |
value = num // parts | |
array.append(value) | |
return sorted(array) |
This file contains hidden or 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
import unittest | |
from split_integer import splitInteger | |
class Test(unittest.TestCase): | |
def test_cases(self): | |
self.assertEqual(splitInteger(10, 1), [10]) | |
self.assertEqual(splitInteger(2, 2), [1, 1]) | |
self.assertEqual(splitInteger(20, 5), [4, 4, 4, 4, 4]) | |
self.assertEqual(splitInteger(10, 5), [2, 2, 2, 2, 2]) | |
if __name__ == '__main__': | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment