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...
Can you answer these question using python and recursive functions Write a function that consumes two...
Can you answer these question using python and recursive functions Write a function that consumes two numbers N1 and N2 and prints out all the even numbers between those numbers, using recursion. Using any number of list literals of maximum length 2, represent the data from your Survey List. So you cannot simply make a list of 15 values. Make a comment in your code that explicitly describes how you were able to represent this. Write a recursive function that...
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]...
PYTHON CODE - Write the body of a function second_instance(s, c) which consumes a string s...
PYTHON CODE - Write the body of a function second_instance(s, c) which consumes a string s and a length 1 string c that is contained at least twice in s and returns the index of the second location of c. second_instance: Str Str -> Nat Requires: len(c) == 1 c occurs at least twice in s    Examples: second_instance("banana", "a") => 3 second_instance("bb", "b") => 1 - Write the body of a function make_list(n) which consumes a natural number n...
Python language: Write a function dice(n) that returns a list with the results of n dice...
Python language: Write a function dice(n) that returns a list with the results of n dice rolls conducted with a six sided dice. Use Numpy's random number functions to do this (Hint: use randint() within numpy's random module). i.e. dice(3) returns something like [2,3,5] to indicate the first dice obtained a 2, the second a 3 etc
Write a Python function ???????? that takes in a nonnegative semiprime number ? which is the...
Write a Python function ???????? that takes in a nonnegative semiprime number ? which is the product of two prime numbers ? and ? and returns the tuple ( ?, ? ) where ?≤? . Example: ????????(22)=(2,11) Example: ????????(3605282209)=(59447,60647) This problem has a time-out limit of 1 second and a memory limit of 1MB. The number ? in all test-cases will satisfy 4≤?≤800000000000000 For example: Test Result print(factorMe(22)) (2, 11) print(factorMe(3605282209)) (59447, 60647)
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT