From 1e0b33d3dd7eda32a97fe73df09df20e2004eb98 Mon Sep 17 00:00:00 2001 From: Sanders Lin <45224617+SandersLin@users.noreply.github.com> Date: Sat, 13 Jul 2019 15:04:43 +0800 Subject: [PATCH] Update 3n+1.py (#996) * Update 3n+1.py Made variable names more meaningful and removed nested functions. * Update 3n+1.py * Update 3n+1.py * Update 3n+1.py --- maths/3n+1.py | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/maths/3n+1.py b/maths/3n+1.py index 6424fe0d8..d6c14ff0f 100644 --- a/maths/3n+1.py +++ b/maths/3n+1.py @@ -1,19 +1,30 @@ -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] +from typing import Tuple, List - return l , c - print(n31(43)) - print(n31(98)[0][-1])# = a - print("It took {0} steps.".format(n31(13)[1]))#optional finish +def n31(a: int) -> Tuple[List[int], int]: + """ + Returns the Collatz sequence and its length of any postiver integer. + >>> n31(4) + ([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)) + + path = [a] + while a != 1: + if a % 2 == 0: + a = a // 2 + else: + a = 3*a +1 + path += [a] + return path, len(path) + +def main(): + num = 4 + path , length = n31(num) + print("The Collatz sequence of {0} took {1} steps. \nPath: {2}".format(num,length, path)) if __name__ == '__main__': main()