Question

In: Computer Science

This is a python question. Write a function mult_table(n) that consumes a natural number n and...

This is a python question.

Write a function mult_table(n) that consumes a natural number n and returns the n+1 by n+1 multiplication table (where each entry in the inner list is equal to the product of which list it is and the inner list position number, or in other words, the product of the row and column numbers). Use accumulative recursion.

def mult_table(n)
'''
Returns the n+1 by n+1 multiplication table
  
mult_table: Nat => (listof (listof Nat))
  
Examples:
mult_table(0) => [[0]]
mult_table(1) => [[0, 0],
[0, 1]]
mult_table(2) => [[0, 0, 0],
[0, 1, 2],
[0, 2, 4]]
mult_table(5) => [[0, 0, 0, 0, 0, 0],
[0, 1, 2, 3, 4, 5],
[0, 2, 4, 6, 8, 10],
[0, 3, 6, 9, 12, 15],
[0, 4, 8, 12, 16, 20],
[0, 5, 10, 15, 20, 25]]
'''

No loops! Only accumulative recursion can be used!

Solutions

Expert Solution

1).ANSWER:

GIVEN BELOW:

#code
# required method
def mult_table(n):
# we are using abstract method map with lambda to construct an n+1 x n+1 list
# containing the required multiplication table.
# range(n+1) will return an iterable that contains value between 0 and n
# list(map(lambda col: row * col, range(n + 1))) will go through each value
# between 0 and n for col, multiply the value with row and returns a list
# list(map(lambda row: list(map(lambda col: row * col, range(n + 1))),range(n+1))) will
# simply loop the above process for row value between 0 and n, adding each returned list
# into another list and returns it
r = list(map(lambda row: list(map(lambda col: row * col, range(n + 1))), range(n + 1)))
return r


print(mult_table(5))

'''
consider below example, if you want to understand how above function works
suppose n=5 and row=3 (means we are currently processing row 3)
the below statement will print [0, 3, 6, 9, 12, 15]

print(list(map(lambda col: row * col, range(n + 1))))

explanation:
range(n + 1) returns [0,1,2,3,4,5]
the map function loops through each item in above list, multiply value with 3 (current row)
so [0, 1, 2, 3, 4, 5] becomes [0, 3, 6, 9, 12, 15], this list is returned

similarly, this process is repeated for rows between 0 and n, and the returned list from each
inner call to list(map(lambda col: row * col, range(n + 1))) is stored in a list of lists

'''
#output

[[0, 0, 0, 0, 0, 0], [0, 1, 2, 3, 4, 5], [0, 2, 4, 6, 8, 10], [0, 3, 6, 9, 12, 15], [0, 4, 8, 12, 16, 20], [0, 5, 10, 15, 20, 25]]


Related Solutions

PYTHON QUESTION: - Write the body of a function most_ending_digit(L) that consumes a non-empty list of...
PYTHON QUESTION: - Write the body of a function most_ending_digit(L) that consumes a non-empty list of natural numbers L and return the single digit that occurs most frequently at the end of the numbers in the list. The function returns the smallest digit in the case of a tie. Your function should run in O(n) time. Do not mutate the passed parameter. def most_ending_digit(L) ''' Returns the single digit that occurs most frequently as the last digit of numbers in...
Python question Write a function int(lofi, alofi) that consumes two sorted lists of distinct integers lofi...
Python question Write a function int(lofi, alofi) that consumes two sorted lists of distinct integers lofi and alofi, and returns a sorted list that contains only elements common to both lists. You must obey the following restrictions: No recursion or abstract list functions, intersect must run in O(n) where n is the combined length of the two parameters. sort function is not allowed as well as list comprehensions math is the only library that can be imported Example: int([4, 13,...
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer...
I have a python coding question: Write the function: eAapproximately (n), that takes a positive integer value n and returns an approximation of e as (1 + 1/n)^n I am not even sure what the question is asking me to do but I think it is asking me to code that function. Does anyone know how to do this?
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should...
PYTHON: Write a function insertInOrder that takes in a list and a number. This function should assume that the list is already in ascending order. The function should insert the number into the correct position of the list so that the list stays in ascending order. It should modify the list, not build a new list. It does not need to return the list, because it is modifying it.   Hint: Use a whlie loop and list methods lst = [1,3,5,7]...
Write a function in python that takes in an integer n and computes the left hand...
Write a function in python that takes in an integer n and computes the left hand Riemann sum of the function f(x) = x^2 on the interval [0,1]. Hint: compute the error from the true answer
Write a Python program tat asks the user for the number of equations (n) to be...
Write a Python program tat asks the user for the number of equations (n) to be solved and fill the coefficient matrix A and the right matrix b with random integers. Limit the values of the elements of A to numbers between 0 and 50 and elements of b to 0 and 500. Lastly, put the matrices into an Excel file.
USING PYTHON Write a program to create a number list. It will call a function to...
USING PYTHON Write a program to create a number list. It will call a function to calculate the average values in the list. Define main ():                        Declare variables and initialize them                        Create a list containing numbers (int/float)                        Call get_avg function that will return the calculated average values in the list.                                       Use a for loop to loop through the values in the list and calculate avg                        End main()
Write a function such that given a number N, display the N*N multiplication matrix from 1...
Write a function such that given a number N, display the N*N multiplication matrix from 1 to N. Then, write a C++ program such that, it prints the multiplication matrices of numbers 1,4,7, and 10 using a loop concept. Check the sample output below, to see how the program should work. Make sure to have your output exactly the same as the below output.
Write a Python function next_Sophie_Germain(n), which on input a positive integer n return the smallest Sophie...
Write a Python function next_Sophie_Germain(n), which on input a positive integer n return the smallest Sophie Germain prime that is greater or equal to n.
Write a python function average_sample_median(n,P), that return the average of 1000 samples of size n sampled...
Write a python function average_sample_median(n,P), that return the average of 1000 samples of size n sampled from the distribution P. * Sample run : print(average_sample_median(10,[0.2,0.1,0.15,0.15,0.2,0.2])) print(average_sample_median(10,[0.3,0.4,0.3])) print(average_sample_median(10,P=[0.99,0.01]) * Expected Output : 3.7855 2.004 1
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT