Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python将未知整数分成给定数量的偶数部分_Python_Python 2.7 - Fatal编程技术网

如何使用Python将未知整数分成给定数量的偶数部分

如何使用Python将未知整数分成给定数量的偶数部分,python,python-2.7,Python,Python 2.7,我需要帮助将一个未知整数分成给定数量的偶数部分,或者至少尽可能地偶数。各部分之和应为原始值,但各部分应为整数,且应尽可能接近 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

我需要帮助将一个未知整数分成给定数量的偶数部分,或者至少尽可能地偶数。各部分之和应为原始值,但各部分应为整数,且应尽可能接近

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.
这就是我所拥有的

def split_整数(num,parts):
如果(数量<零件):
打印(-1)
elif(数量%parts==0):
对于范围内的i(零件):
打印(数量//部分),
其他:
剩余零件数=零件数-(零件数%)
商=num//parts
对于范围内的i(零件):
如果(i>=剩余部分):
打印(商+1),
其他:
打印(商),
分割整数(10,1)
这是样本测试

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])
预期产出的例子

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]
我发现了错误

Failure
AssertionError: None != [10]

第一个问题是,您正在打印结果,而不是返回结果。默认情况下,在Python中,任何不显式返回任何内容的函数都将返回
None

在任何情况下,有一种更简洁的方法,即使用理解:

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

第一个问题是,您正在打印结果,而不是返回结果。默认情况下,在Python中,任何不显式返回任何内容的函数都将返回
None

在任何情况下,有一种更简洁的方法,即使用理解:

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(10,1)
场景,您处理的分区大小为1,即
parts=1
,直观的解决方案是:
partition=[10]
。当然,这假定
余数=0
部分=1或0

如果这是基本情况的一般概念,那么可以通过递归的方式计算总分区,其中
num
部分
连续减少,如下所示:

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()


这个问题与“给钱”问题非常相似

让我们先看看最简单的
split(10,1)
场景,您处理的分区大小为1,即
parts=1
,直观的解决方案是:
partition=[10]
。当然,这假定
余数=0
部分=1或0

如果这是基本情况的一般概念,那么可以通过递归的方式计算总分区,其中
num
部分
连续减少,如下所示:

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()

或者简单地说

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]
或者简单地说

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]

该函数将返回结果。到目前为止,它只打印它。顺便说一句,预期结果是列表而不是数组。预期函数将返回结果。到目前为止,它只打印它。顺便说一句,预期的结果是列表而不是数组。这是一个很好的答案,您可以进一步简化:
[范围内i的商+整数(i<余数)]
这是一个很好的答案,您可以进一步简化:
[范围内i的商+整数(i<余数)]
Nice one@elayiraNice one@elayira