Question: Please write in Python 3, Thanks! The sum of the first n natural numbers is equal to 1+2+...+n=n(n + 1)/2. Suppose that you are given



Please write in Python 3, Thanks!
The sum of the first n natural numbers is equal to 1+2+...+n=n(n + 1)/2. Suppose that you are given a list t of length n = n(n+1)/2 where the list contains only integer values. Write the recursive helper function to_triangular_grid_impl(t, n) where t is a list as described in the previous paragraph and n is the value of n as described in the previous paragraph. The function returns a list of n sublists where the first sublist contains the first n elements of t the second sublist contains the next n - 1 elements of t, the third sublist contains the next n - 2 elements of t, and so on the nth sublist contains the last element of t You should not call the function to_triangular_grid_impl directly, instead call the function to triangular_grid(t) which computes the value of n given a list t. See the assignment module a3 where to_triangular_grid(t) is already given to you. Some examples of what the function should return are: t = list(range(0, 1)) print(a3.to_triangular_grid(t)) should print [[@]] t = list (range (@, 3)) print(a3.to_triangular_grid(t)) should print [[0, 1], [2]] t = list(range(0, 6)) print(a3.to_triangular_grid(t)) should print [[0, 1, 2], [3, 4], [5]] t = list (range (@, 15)) print(a3.to_triangular_grid(t)) should print [[0, 1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11], [12, 13], [14]] t = [9, -3, 4, 1, -2, 5] print(a3.to_triangular_grid(t)) should print [[9, -3, 4], (1, -2], [5]] The function is called to_triangular_grid because if you stack the sublists on top of one another and print the elements they form a triangular pattern: t = [9, -3, 4, 1, -2, 5] a3.pretty_print(a3.to_triangular_grid(t)) prints 9 4 -3 -2 1 5 def pretty_print(t): for sublist in t: for elem in sublist: print('{:>4}'.format(elem), end='') print() def to_triangular_grid(t): k len(t) if k == 0: return [[]] n = (-1 + math.sqrt(1 + 8 k)) / 2 if not n.is_integer(): raise ValueError("') return to_triangular_grid_impl(t, int(n)) def to_triangular_grid_impl(t, n)
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
