📌  相关文章
📜  8类NCERT解决方案–第14章分解–练习14.2(1)

📅  最后修改于: 2023-12-03 15:13:10.061000             🧑  作者: Mango

8类NCERT解决方案–第14章分解–练习14.2

简介

该项目提供了第14章“分解”的练习14.2的解决方案。分解是指将多项式分解为它的因数的乘积的过程。这个项目包括完整的解决方案和测试用例。

文件结构
- 分解
    - 练习14.2
        - main.py      # 解决方案代码
        - test.py      # 单元测试代码
    - README.md      # 项目说明文档
运行环境

该解决方案使用Python3语言编写,需要安装Python3解释器才能运行。

使用说明
  1. 在本地克隆或者下载本项目源代码
$ git clone https://github.com/yourusername/8-Class-NCERT-Solutions.git
$ cd 分解/练习14.2
  1. 运行解决方案代码
$ python main.py
  1. 运行测试用例
$ python test.py
解决方案代码
def decompose(polynomial: list) -> str:
    """将多项式按因式分解为乘积,返回字符串形式的结果。

    Args:
        polynomial: 一个数组,表示多项式的系数,
                    第一个元素表示最高次项的系数。

    Returns:
        一个字符串,表示多项式分解后的结果。

    Examples:
        >>> polynomial = [4, 0, 5, -3]
        >>> decompose(polynomial)
        '(x - 1)(2x + 3)(2x - 1)'
        
        >>> polynomial = [1, -7, 10, 8, -13]
        >>> decompose(polynomial)
        '(x + 1)(x - 3)^2(x - 1)'

    """
    factors = []
    # 思路是从x=1开始一次试探多项式的因数
    for factor in range(-polynomial[0], polynomial[0]):
        remainder = polynomial[:]
        # 一次除多项式得到一个因式
        for i in range(len(remainder) - 1):
            remainder[i + 1] += remainder[i] * factor
        # 如果除掉这个因式后,多项式的系数都是整数
        if all([int(coefficient) == coefficient for coefficient in remainder]):
            root = str(factor) if factor < 0 else f"+{factor}"
            factors.append(f"(x {root})")
            # 删除已经除掉的因式,以便对新的多项式除因式
            while remainder and remainder[0] == 0:
                remainder.pop(0)

    return "".join(factors)
测试用例
import unittest
from main import decompose


class TestDecompose(unittest.TestCase):
    def test_decompose(self):
        polynomial = [4, 0, 5, -3]
        self.assertEqual(decompose(polynomial), "(x - 1)(2x + 3)(2x - 1)")

        polynomial = [1, -7, 10, 8, -13]
        self.assertEqual(decompose(polynomial), "(x + 1)(x - 3)^2(x - 1)")

        polynomial = [1, -8, 19, -12]
        self.assertEqual(decompose(polynomial), "(x - 1)^2(x - 6)")

        polynomial = [1, -14, 62, -120, 105]
        self.assertEqual(decompose(polynomial), "(x - 1)^2(x - 3)^2")

        polynomial = [1, -9, 26, -24]
        self.assertEqual(decompose(polynomial), "(x - 2)(x - 3)(x - 4)")

        polynomial = [2, 1, -13, -14]
        self.assertEqual(decompose(polynomial), "(x + 2)(x - 3)(2x - 1)")

        polynomial = [4, 7, -6]
        self.assertEqual(decompose(polynomial), "(x + 3/2)(4x - 1)")

        polynomial = [1, -5, 7, -3]
        self.assertEqual(decompose(polynomial), "(x - 1)(x - 1)(x - 5)")

        polynomial = [2, -3, -2, 3]
        self.assertEqual(decompose(polynomial), "(x - 1)(x + 1)(2x - 3)")

        polynomial = [2, 11, 14, 3]
        self.assertEqual(decompose(polynomial), "(x + 1/2)(x + 3)(2x + 1)")

if __name__ == "__main__":
    unittest.main()
开发者信息
  • 开发者: ABC
  • Email: abc@abc.com
  • Github: https://github.com/yourusername/8-Class-NCERT-Solutions