From f28efd6cdbe9928966018f68f11e4b0b20565974 Mon Sep 17 00:00:00 2001 From: Sanders Lin <45224617+SandersLin@users.noreply.github.com> Date: Thu, 11 Jul 2019 01:34:17 +0800 Subject: [PATCH 1/4] Update 3n+1.py Made variable names more meaningful and removed nested functions. --- maths/3n+1.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/maths/3n+1.py b/maths/3n+1.py index 6424fe0d8f15..ef897fa40b18 100644 --- a/maths/3n+1.py +++ b/maths/3n+1.py @@ -1,19 +1,19 @@ -def main(): - def n31(a):# a = initial number - c = 0 - l = [a] - while a != 1: - if a % 2 == 0:#if even divide it by 2 - a = a // 2 - elif a % 2 == 1:#if odd 3n+1 - a = 3*a +1 - c += 1#counter - l += [a] +def n31(a):# a = initial number + counter = 0 + path = [a] + while a != 1: + if a % 2 == 0: + a = a // 2 + else: + a = 3*a +1 + counter += 1 + path += [a] + return path , counter - return l , c - print(n31(43)) - print(n31(98)[0][-1])# = a - print("It took {0} steps.".format(n31(13)[1]))#optional finish +def main(): + num = 43 + path , length = n31(num) + print("The Collatz sequence of {0} took {1} steps. \nPath: {2}".format(num,length, path)) if __name__ == '__main__': main() From 81962a31b8443ceb70fa468fc1f1a1c301f20bd8 Mon Sep 17 00:00:00 2001 From: Sanders Lin <45224617+SandersLin@users.noreply.github.com> Date: Thu, 11 Jul 2019 03:07:55 +0800 Subject: [PATCH 2/4] Update 3n+1.py --- maths/3n+1.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/maths/3n+1.py b/maths/3n+1.py index ef897fa40b18..f5749464f4a9 100644 --- a/maths/3n+1.py +++ b/maths/3n+1.py @@ -1,4 +1,11 @@ -def n31(a):# a = initial number +def n31(a): + """ + Returns Collatz sequence of number a + + >>> n31(4) + ([4,2,1],3) + + """ counter = 0 path = [a] while a != 1: From 79ddd13a4a86080c3c082176dfe6e9bf1b6f0dff Mon Sep 17 00:00:00 2001 From: Sanders Lin <45224617+SandersLin@users.noreply.github.com> Date: Thu, 11 Jul 2019 11:34:42 +0800 Subject: [PATCH 3/4] Update 3n+1.py --- maths/3n+1.py | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/maths/3n+1.py b/maths/3n+1.py index f5749464f4a9..a4f9d056fcbf 100644 --- a/maths/3n+1.py +++ b/maths/3n+1.py @@ -1,11 +1,17 @@ -def n31(a): +from typing import Tuple, List + +def n31(a: int) -> Tuple[List[int], int]: """ - Returns Collatz sequence of number a - + Returns Collatz sequence of a number >>> n31(4) - ([4,2,1],3) - + ([4, 2, 1], 3) """ + + if not isinstance(a, int): + raise TypeError('Must be int, not {0}'.format(type(a).__name__)) + if a < 1: + raise ValueError('Given integer must be greater than 1, not {0}'.format(a)) + counter = 0 path = [a] while a != 1: @@ -15,10 +21,10 @@ def n31(a): a = 3*a +1 counter += 1 path += [a] - return path , counter + return path, counter + 1 def main(): - num = 43 + num = 4 path , length = n31(num) print("The Collatz sequence of {0} took {1} steps. \nPath: {2}".format(num,length, path)) From 9f220f133b13c6910668552b8f6fbcbf7aae15e8 Mon Sep 17 00:00:00 2001 From: Sanders Lin <45224617+SandersLin@users.noreply.github.com> Date: Thu, 11 Jul 2019 13:27:37 +0800 Subject: [PATCH 4/4] Update 3n+1.py --- maths/3n+1.py | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/maths/3n+1.py b/maths/3n+1.py index a4f9d056fcbf..d6c14ff0f47d 100644 --- a/maths/3n+1.py +++ b/maths/3n+1.py @@ -2,7 +2,7 @@ def n31(a: int) -> Tuple[List[int], int]: """ - Returns Collatz sequence of a number + Returns the Collatz sequence and its length of any postiver integer. >>> n31(4) ([4, 2, 1], 3) """ @@ -11,17 +11,15 @@ def n31(a: int) -> Tuple[List[int], int]: raise TypeError('Must be int, not {0}'.format(type(a).__name__)) if a < 1: raise ValueError('Given integer must be greater than 1, not {0}'.format(a)) - - counter = 0 + path = [a] while a != 1: if a % 2 == 0: a = a // 2 else: a = 3*a +1 - counter += 1 path += [a] - return path, counter + 1 + return path, len(path) def main(): num = 4