How to divide an unknown integer into a given number of even parts using Python

divide a number into n parts
divide a number into n equal parts in c++
c# divide number into equal parts
divide n into k parts
java split number into chunks
split a number
java split array into n parts
divide number into equal parts calculator

I need help with the ability to divide an unknown integer into a given number of even parts — or at least as even as they can be. The sum of the parts should be the original value, but each part should be an integer, and they should be as close as possible.

Parameters
num: Integer - The number that should be split into equal parts

parts: Integer - The number of parts that the number should be split 
into

Return Value
List (of Integers) - A list of parts, with each index representing the part and the number contained within it representing the size of the part. The parts will be ordered from smallest to largest.

This is what I have

def split_integer(num,parts):
    if (num < parts):
      print(-1)

    elif (num % parts == 0):
      for i in range(parts):
        print(num // parts),
    else: 
      parts_remaining = parts - (num % parts)
      quotient = num // parts 

      for i in range(parts):
        if (i >= parts_remaining):
          print(quotient + 1),
        else:
          print(quotient),

split_integer(10, 1)

This is the sample tests

import unittest

class Test(unittest.TestCase):
    def test_should_handle_evenly_distributed_cases(self):
        self.assertEqual(split_integer(10, 1), [10])
        self.assertEqual(split_integer(2, 2), [1,1])
        self.assertEqual(split_integer(20, 5), [4,4,4,4,4])

Examples of the expected output

num parts   Return Value
Completely even parts example   10  5   [2,2,2,2,2]
Even as can be parts example    20  6   [3,3,3,3,4,4]

I am getting the error

Failure
AssertionError: None != [10]

The first problem is that you are printing your results instead of returning them. By default, in Python, any function that does not explicitly return anything will return None.

In any case, there is a more concise way, using comprehensions:

def split_integer(num, parts):
    quotient, remainder = divmod(num, parts)
    lower_elements = [quotient for i in range(parts - remainder)]
    higher_elements = [quotient + 1 for j in range(remainder)]
    return lower_elements + higher_elements

Split the number into N parts such that difference between the , Try like this. var splitInteger = function(num, parts) { // Complete this function var val; var mod = num % parts; if(mod == 0){ val = num/parts;  I need help with the ability to divide an unknown integer into a given number of even parts — or at least as even as they can be. The sum of the parts should be the original value, but each part should be an integer, and they should be as close as possible. Parameters num: Integer - The number that should be split into equal parts. parts

This problem is very similar to the "giving change" problem.

Let us start by looking at the simplest scenario of split(10, 1) where you're dealing with a partition size of 1 i.e parts = 1, an intuitive solution would be: partition = [10]. Of course, this assumes that the remainder = 0 and parts = 1 or 0.

If that is a generic idea of a base case, then the total partitions can be calculated by way of recursion where both the num and parts are continuesly reduced as shown below:

def split_integer(num, parts):
    """
    split_integer(integer, parts) -> [ value[, values] ]
    divides an integer into an ""even as can be"" number of parts.
    >>> split_integer(10, 1)
    [10]
    >>> split_integer(2, 2)
    [1, 1]
    >>> split_integer(20, 5)
    [4, 4, 4, 4, 4]
    >>> split_integer(10, 5)
    [2, 2, 2, 2, 2]
    >>> split_integer(20, 6)
    [3, 3, 3, 3, 4, 4]
    >>> split_integer(5, 4)
    [1, 1, 1, 2]
    """
    lower_bound, remainder = divmod(num, parts)
    sub_partition = [lower_bound ] * (parts - remainder)
    num -= len(sub_partition) * lower_bound
    if remainder:
        sub_partition += split_integer(num, remainder)
    return sub_partition

if __name__ == "__main__":
    import doctest
    doctest.testmod()

Divide number into two parts divisible by given numbers , Given two integers 'X' and 'N', the task is to split the integer 'X' into exactly 'N' parts will always be '0' and the sequence will contain all equal numbers i.e. x/n . Split the number into N parts such that difference between the smallest and the largest part is minimum. X1 + X2 + X3 + + Xn = X and the difference between the maximum and the minimum number from the sequence is minimized. Print the sequence in the end, if the number cannot be divided into exactly ‘N’ parts then print ‘-1’ instead.

Or simply,

In [1]: n,p=20,6                                                                
In [2]: c,r=divmod(n,p)                                                         
In [3]: [c]*(p-r) + [c+1]*r                                                     
Out[3]: [3, 3, 3, 3, 4, 4]

Namwanza/divide-numbers-into-even-parts: divide an , Given a large number in string format and we are also given two numbers f and s. We need to divide the large number into two continuous parts such that the first  Find the number of ways to divide number into four parts such that a = c and b = d; Divide N into K unique parts such that gcd of those parts is maximum; Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i] Break the number into three parts; Divide a number into two parts; Divide a big number into two parts that differ by k

How to divide a given number into a given number of parts randomly , divide an unknown integer into a given number of even parts — or at least as even GitHub is home to over 50 million developers working together to host and  A number is even if it is perfectly divisible by 2. When the number is divided by 2, we use the remainder operator % to compute the remainder. If the remainder is not zero, the number is odd. Source Code # Python program to check if the input number is odd or even. # A number is even if division by 2 gives a remainder of 0.

Split a number into equal parts given the number of parts, How do I divide a given number into a given number of parts randomly and uniformly? Resultp which sum to equal N. The amounts of each number should be random , ie: How do I generate a random number in Python without it repeating? How many random bits does it take to generate a uniformly random integer  We need to divide the large number into two continuous parts such that the first part is divisible by f and the second part is divisible by s. Examples: Input: num = “246904096” f = 12345 s = 1024 Output: Yes We can divide num into “24680” and “4096” which are divisible by f and s respectively.

Divide an array of integers into nearly equal sums, In general, building a list using a loop of the form. some_list = [] for …: some_list.​append(…) … would be better written using a list  Given a positive integer n, find number of ways to divide n in four parts or represent n as sum of four positive integers. Here n varies from 0 to 5000.

Comments
  • It is expected that the function returns the result. So far it it only printing it.
  • And BTW the expected results are lists not arrays.
  • This is a good answer and you can boil it down further: [quotient + int(i < remainder) for i in range(parts)]
  • Nice one @elayira