From f08d11fb3472c43e4d9f5f9d9a569fd3dcee0297 Mon Sep 17 00:00:00 2001 From: Sanders Lin Date: Sun, 6 Nov 2022 00:06:49 +0800 Subject: [PATCH 1/2] Update 3n_plus_1.py 1. Minor issue with ValueError message: Given integer should be positive, not greater than 1, as 1 is allowed. 2. += calls underlying list extend method which might be slower. Calling apend seems more appropriate. --- maths/3n_plus_1.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/maths/3n_plus_1.py b/maths/3n_plus_1.py index e455a158e619..6dae3cafa017 100644 --- a/maths/3n_plus_1.py +++ b/maths/3n_plus_1.py @@ -11,15 +11,15 @@ def n31(a: int) -> tuple[list[int], int]: if not isinstance(a, int): raise TypeError(f"Must be int, not {type(a).__name__}") if a < 1: - raise ValueError(f"Given integer must be greater than 1, not {a}") + raise ValueError(f"Given integer must be positive, not {a}") path = [a] while a != 1: if a % 2 == 0: - a = a // 2 + a //= 2 else: a = 3 * a + 1 - path += [a] + path.append(a) return path, len(path) From 218f449f1d6fd0ac7ccbe41e42a7abd40bfa932c Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Sat, 5 Nov 2022 16:09:01 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- maths/3n_plus_1.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/maths/3n_plus_1.py b/maths/3n_plus_1.py index 6dae3cafa017..59fdec48e100 100644 --- a/maths/3n_plus_1.py +++ b/maths/3n_plus_1.py @@ -16,7 +16,7 @@ def n31(a: int) -> tuple[list[int], int]: path = [a] while a != 1: if a % 2 == 0: - a //= 2 + a //= 2 else: a = 3 * a + 1 path.append(a)