Skip to content

Instantly share code, notes, and snippets.

@phalbert
Created April 13, 2019 18:53
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 phalbert/21e3f3c998306a328be148a60a6eb4a8 to your computer and use it in GitHub Desktop.
Save phalbert/21e3f3c998306a328be148a60a6eb4a8 to your computer and use it in GitHub Desktop.
Split an integer into a roughly equal number of parts
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)
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