Question: Question 3 This question requires you to solve computational problems using structured programming. (a) Write a function factorial1() that takes ONE (1) argument: a number,

 Question 3 This question requires you to solve computational problems using

structured programming. (a) Write a function factorial1() that takes ONE (1) argument:

Question 3 This question requires you to solve computational problems using structured programming. (a) Write a function factorial1() that takes ONE (1) argument: a number, n. The function computes and returns the factorial of the number, n, computed using the formula: factoriall(n) = 1 X2 X3 X...x(n-1)xn (5 marks) (6) Write a function factorial2() that takes TWO (2) arguments: n, a number to compute factorial for, and lookup Table, a dictionary which stores numbers and their factorials that are already computed for future retrieval. The function returns the factorial of the number, n. There are two cases in getting the factorial of n. Case 1 The function checks that the lookup table already contains n as key. The function returns the factorial value associated with the key n. For example, suppose lookup Table = {1:1, 2:2, 3:6} and n has the value 3, then the function returns 6, the value associated with the key 3. Case 2 The function checks that the lookup table does not contain n as key. The function locates the largest key, k in the lookup table, computes and records the factorials of numbers from k +1 ton, and stores each calculated factorial in the lookup table. For example, the function is called with n having the value 5. As the function is unable to locate the key 5 in lookupTable, it performs the following steps: Find the largest key in lookupTable, 3 and the associated value, 6 Compute the factorial of 4 by multiplying factorial 3, obtained in the lookup table, and 4, i.e., 6 x 4. Record 4 and the factorial of 4 in lookup Table. Compute the factorial of 5 by multiplying factorial 4, obtained in the lookup table, and 5, i.e., 24 x 5. Record 5 and the factorial of 5 in lookupTable. When the function completes execution and returns 120, two elements would have been added to lookup Table, resulting in: {1: 1, 2: 2, 3: 6, 4: 24, 5: 120) (14 marks) . Write a function readFromFile(). The function reads from a file, fact.txt, and composes a dictionary, lookup Table from the file content. The function returns the dictionary. An example content of fact.txt is shown here: 11 22 36 For this example, the function should return the dictionary (1: 1, 2:2, 3: 6). (6 marks)

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!