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]...
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.
Use Python Write a function that takes a mobile phone number as a string and returns...
Use Python Write a function that takes a mobile phone number as a string and returns a Boolean value to indicate if it is a valid number or not according to the following rules of a provider: * all numbers must be 9 or 10 digits in length; * all numbers must contain at least 4 different digits; * the sum of all the digits must be equal to the last two digits of the number. For example '045502226' is...
Can someone do this python program? Write a function that takes a number as a parameter...
Can someone do this python program? Write a function that takes a number as a parameter and then prints out all of the factors for that number.
On Python Write a function that accepts a relative file path as parameter and returns number...
On Python Write a function that accepts a relative file path as parameter and returns number of non-empty lines in the file. Test your function with the provided sample file studentdata.txt.
Python 3 Write the definition of a function that take one number, that represents a temperature...
Python 3 Write the definition of a function that take one number, that represents a temperature in Fahrenheit and prints the equivalent temperature in degrees Celsius. Write the definition of another function that takes one number, that represents speed in miles/hour and prints the equivalent speed in meters/second. Write the definition of a function named main. It takes no input, hence empty parenthesis, and does the following: - prints Enter 1 to convert Fahrenheit temperature to Celsius - prints on...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT