Question: add test cases as well:Counting only { + , - , * , / , % , > , < , = = , <
add test cases as well:Counting only determine the exact number of basic operations that would be performed when the following function foo is invoked on the given value of n
def foon:
if n :
return
elif n :
v foon
t
for i in rangen:
t t v
return t
else:
v foon
return v
Input Format
A line containing the integer t
Each of the next t lines contains a single integer ni
Constraints
Output Format
t lines, each of which is a single integer indicating the number of basic operations that would have been executed by foo on the corresponding value of n read from the input.
Sample Input
Sample Output
Explanation
In the first test n and when we invoke foo on it checks if incurring operation, then it goes on to check whether which incurs more operations for a total of Since the condition is true, the consequent expressions must now be performed. The recursive call invokes one so now our runnig total is so far
In the recursive call, n so once again, it checks the predicates of the if and elif conditional statements, which costs operations in total now the total is and since both are false, executes the alternative clause introduced by else That code fragment invokes the operation for a total of now then a recursive call. On the recursive call, n only basic operation is performed, bringing the total to Finally an invocation of the operation brings the total to
When the code returns to the previous invocation n the value of v has been determined, and the code can resume to perform a for loop for n iterations. This causes two more invocations of taking the total to
For the n case, the condition that gets executed is the else clause, by which time basic operations have already been performed two and one The recursive call to n incurs operations as we just learned and the and operations add more. So that makes it a total of operations.
#binpython
import os
import sys
# Complete the function below.
def countFooOpsn:
# Write your code here.
if namemain:
f openosenvironOUTPUTPATH'w
t intinput
for ti in ranget:
n intinput
result countFooOpsn
fwritestrresult
fclose
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
