Question: Write an iterative Python function called generate_i( n ) that generates a series or rows of numbers stored as sublists according to the following rules:

Write an iterative Python function called generate_i( n ) that generates a series or rows of numbers stored as sublists according to the following rules:

  • Start by returning [1] for n=1
  • Continue by returning [1, 1] for n=2
  • Each remaining row of values will thus continue to have one more value than the previous row. For example, the fifth row will have five values, while the fourth row has four. The values on both ends of the current row will always be 1 for n > 1. Starting with the second row, each internal cell will be the sum of the two cells diagonally right above that cell.
  • Hence, n such rows should be generated by generate_i( n ).

Below is a pictorial description of the values that should be generated with the call generate_i( 6 ):

For example, take the row before last from the bottom. Both ends have 1, as required. The first leftmost value after 1 is 4, because, above it are cells 1 and 3, whose sum is 4. The middle element of the last row to be generated for n=6 is 6, because above it are two 3s.

Your generate_i( n ) function will return a list of lists, in which each sublist will store one of these rows of numbers, starting with 1 at the top. Therefore, the return value of generate_i( 6 ) should be

[[1], [1, 1], [1, 2, 1], [1, 3, 3, 1], [1, 4, 6, 4, 1], [1, 5, 10, 10, 5, 1]] 

where each row is represented by a sublist. So there are 6 sublists for n=6.

  • As mentioned above, your generate_i() function must be iterative, that is, it must strictly use looping to do its job.
  • You are not allowed to use import statements or the zip() function. If you do, your grade will automatically be zero.
  • You are not allowed to define additional functions! So you should only define generate_i() in your solution. If you define additional helperfunctions external or internal to generate_i(), they will be automatically detected, and, unfortunately, your grade will be zero.
  • You will use the Jupyter Notebook file magic directive (%%file ...) to store your solution as generate_i.py. Therefore, do not remove this directive from the top of the following code cell

%%file generate_i.py

def generate_i( n ): """ Read above specifications carefully """ # YOUR CODE HERE

pass

This is a test case please try and see if it works for you

# # T E S T #1.1 ..... (10 points) # # from generate_i import generate_i

student_answer = generate_i( 1 ) correct_answer = [[1]]

assert student_answer == correct_answer

Write an iterative Python function called generate_i( n ) that generates a

1 1 1 1 2 1 1 3 3 1 1 4 6 4 1 1 | 5 10 16 5 1 1

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!