All questions carry equal weightage. All Python code is assumed to be executed using Python3. You may submit as many times as you like within the deadline. Your final submission will be graded.
Note:
If the question asks about a value of type string, remember to enclose your answer in single or double quotes.
If the question asks about a value of type list, remember to enclose your answer in square brackets and use commas to separate list items.
What does f(27182818) return, for the following function definition?
def f(x):
d=0
while x > 1:
(x,d) = (x/2,d+1)
return(d)
25
Solution
For the given function definition, f(27182818)
returns 25
.
What is h(60)-h(45), given the definition of h below?
def h(n):
s = 0
for i in range(2,n):
if n%i == 0:
s = s+i
return(s)
75
Solutions
For what value of n would g(375,n) return 4?
def g(m,n):
res = 0
while m >= n:
(res,m) = (res+1,m/n)
return(res)
15
To achieve a return value of 4 from g(375, n)
, we need to find a value of n
that can successfully divide 375 exactly 4 times within the function's loop. Here's the reasoning:
- Division Focus: The function repeatedly divides
m
(which is 375 in this case) byn
untilm
becomes less thann
. - Counting Divisions: The variable
res
keeps track of the number of timesm
is successfully divided byn
. - Return Value: The function ultimately returns the value of
res
, which represents the number of divisions that occurred.
To find the appropriate n
, we can follow these steps:
- List Divisors: Identify all the positive divisors of 375, which are 1, 3, 5, 15, 25, 75, and 375.
- Test Each Divisor: For each divisor, simulate the function's behavior by repeatedly dividing 375 by the divisor until it's no longer divisible. Count the divisions.
- Find Four Divisions: The
n
that allows for exactly 4 divisions is the answer.
By testing each divisor, we discover that n = 15 is the solution. Here's how it works:
- Dividing 375 by 15 yields 25, and dividing 25 by 15 yields 1, resulting in 2 divisions.
- To achieve 4 divisions, we need to consider a multiple of 15. Multiplying 15 by itself twice gives us 225.
- Dividing 375 by 225 results in 1, counting as the 3rd and 4th divisions.
Therefore, n = 15
is the value that makes g(375, n)
return 4.
Consider the following function mys:
def mys(m):
if m == 1:
return(1)
else:
return(m*mys(m-1))
Which of the following is correct?
The function always terminates with mys(n) = factorial of n
The function always terminates with mys(n) = 1+2+...+n
The function terminates for non-negative n with mys(n) = factorial of n
The function terminates for positive n with mys(n) = factorial of n
The correct statement is: The function terminates for positive n with mys(n) = factorial of n.
Here's a breakdown of why this is the case:
- Base Case: The function has a base case of
mys(1)
, which directly returns 1. This base case is essential for halting the recursion. - Recursive Call: For any input
m
greater than 1, the function calls itself recursively withmys(m-1)
and multiplies the result bym
. This pattern aligns with the definition of factorials. - Factorial Calculation: The recursive calls continue until they reach the base case of
mys(1)
, at which point the multiplications start unfolding in reverse order. This process effectively calculates the factorial ofn
. - Positive Inputs: The function only terminates for positive values of
n
. Ifn
is 0 or negative, the recursive calls would continue indefinitely without reaching a base case, leading to infinite recursion.
Therefore, the function mys(m)
accurately calculates the factorial of n
for positive values of n
, ensuring termination in those cases.
THANKING YOU FOR YOUR SUPPORT EVERY ANSWER IS CORRECT THANK YOU ONCE AGAIN
ReplyDelete