From 7174bf0608b28101f0b8921943b83b7045d3235f Mon Sep 17 00:00:00 2001 From: Gabriela Ibarra Date: Wed, 28 Dec 2022 19:10:55 -0600 Subject: [PATCH] Implement newman_conway function --- lib/newman_conway.py | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/lib/newman_conway.py b/lib/newman_conway.py index 7f5341a..c93cb79 100644 --- a/lib/newman_conway.py +++ b/lib/newman_conway.py @@ -1,6 +1,22 @@ -def newman_conway(num): - """ Returns a list of the Newman Conway numbers for the given value. - Time Complexity: ? - Space Complexity: ? +def newman_conway(num, sol = None): + """ + Returns a list of the Newman Conway numbers for the given value. + Time Complexity: ? + Space Complexity: ? + """ - pass + if num <= 0: + raise ValueError("Number must be greater than 0") + + if num == 1: + return "1" + + solutions = [0, 1, 1] + + for i in range(3, num + 1): + result = solutions[solutions[i - 1]] + solutions[i - solutions[i - 1]] + solutions.append(result) + + solutions.pop(0) + + return (" ").join(map(str, solutions))