Question

In: Computer Science

def mystery(L, x): if L==[]: return False if L[0] == x: return True L.pop(0) return mystery(L,...

def mystery(L, x):
    if L==[]:
        return False
    if L[0] == x:
        return True
    L.pop(0)
    return mystery(L, x)

What is the input or length size of the function mystery?

What is the final output of mystery([1,3,5,7], 0)?

Explain in one sentence what mystery does?

What is the smallest input that mystery can have? Does the recursive call have smaller inputs? Why?

Assuming the recursive call in mystery is correct, use this assumption to explain in a few sentences why mystery is correct?

Solutions

Expert Solution

What is the input or length size of the function mystery?
Input is a list L and an element x

What is the final output of mystery([1,3,5,7], 0)?
final output of mystery([1,3,5,7], 0) is False.
Because value 0 not found in the list [1,3,5,7]

Explain in one sentence what mystery does?
mystery checks for the value of x in the list L.
if x found in L then returns True, otherwise returns False

What is the smallest input that mystery can have?
smallest input that mystery can have is []

Does the recursive call have smaller inputs? Why?
Yes.
For example if mystery([1],2) is getting called then the first recursive call is mystery([], 2)
mystery([], 2) is the smaller input

Assuming the recursive call in mystery is correct, use this assumption to explain in a few sentences why mystery is correct?
For example
mystery([1,3,5,7], 0)
This compares the value of 1 with 0. and both are not equals and then it makes a recursive call mystery([3,5,7], 0) 
This compares the value of 3 with 0. and both are not equals and then it makes a recursive call mystery([5,7], 0)
This compares the value of 5 with 0. and both are not equals and then it makes a recursive call mystery([7], 0)
This compares the value of 7 with 0. and both are not equals and then it makes a recursive call mystery([], 0)
mystery([], 0) return False


Related Solutions

import Stack def stack_mystery(myStack):      mystery=0     while not myStack.isEmpty():          value=myStack.pop()          if v
import Stack def stack_mystery(myStack):      mystery=0     while not myStack.isEmpty():          value=myStack.pop()          if value> mystery:              mystery=value                return mystery def main():    s=Stack.Stack()        s.push(20)        s.push(10)        s.push(50)        s.push(100)        s.push(40)        print(stack_mystery(s)) main() Answer the following questions a) What is the output of the print(stack_mystery(s)) statement in the main method? b) What is the task of the stack_mystery function? What does the mystery variable represent?
Create a function that will return true if numbers X and Y exist within S such that X + Y = Z, return false otherwise.
Given:   S = [ 2, 1, 3, 4, 7, 5 ] Z = 8   Create a function that will return true if numbers X and Y exist within S such that X + Y = Z, return false otherwise.   Code in Java 8. 
Consider the Sturm-Liouville problem X′′(x) + λX(x) = 0 subject toX′(0) = 0, X(l) = 0....
Consider the Sturm-Liouville problem X′′(x) + λX(x) = 0 subject toX′(0) = 0, X(l) = 0. Are the boundary conditions symmetric? Do these boundary conditions yield negative eigenvalues? Determine the eigenvalues and eigenfunctions, Xn(x). (It is enough in some cases to provide the equation that determines the eigenvalues rather than an explicit formula.) Are the eigenfunctions orthogonal?
The Case of the Mystery Investor Madame X is known in Vancouver as the “mystery investor”;...
The Case of the Mystery Investor Madame X is known in Vancouver as the “mystery investor”; in real life she goes by Mia and jokes with her assistant (Miss Robin) of the past 2 years that it stands for “missing in action”. Miss Robin handles virtually all of Madame X’s business/investment communications. Indeed, Madame X is a wealthy angel investor: once each year she provides funding for a local business wishing to expand based on two key motives: (a) doing...
def annoying_factorial(n): if n == 0 or n == 1: return 1 if n == 2:...
def annoying_factorial(n): if n == 0 or n == 1: return 1 if n == 2: return 2 if n == 3: return 6 if n == 4: return 4 * annoying_factorial(3) if n == 5: return 5 * annoying_factorial(4) if n == 6: return 6 * annoying_factorial(5) else: return n * annoying_factorial(n-1) def annoying_fibonacci(n): if n==0: return 0 if n==1: return 1 if n==2: return 1 if n==3: return 2 if n==4: return annoying_fibonacci(4-1)+annoying_fibonacci(4-2) if n==5: return annoying_fibonacci(5-1)+annoying_fibonacci(5-2) if...
Bound state in potential. Where V(x)=inf for x<0 V(x)=0 for 0 ≤ x ≤ L V...
Bound state in potential. Where V(x)=inf for x<0 V(x)=0 for 0 ≤ x ≤ L V (x) = U for x > L Write down the Schrödingerequation and solutions (wavesolutions) on general form for the three V(x).
hich of the following statements are FALSE? ΔGo > 0 for Br2(l) + 2 Hg(l) --->...
hich of the following statements are FALSE? ΔGo > 0 for Br2(l) + 2 Hg(l) ---> 2 Br-(aq) + Hg22+(aq). ΔGo < 0 for MnO2(s) + SO2(g) ---> Mn2+(aq) + SO42-(aq). The cell potential for Co(s)|Co2+(aq, 0.1 M)||Fe3+(aq, 1.5 M)|Fe2+(aq,1.5 M)|Pt(s) is less than 1.05 V. The cell potential for Co(s)|Co2+(aq, 1.5 M)||Fe3+(aq, 0.5 M)|Fe2+(aq, 0.1 M)|Pt(s) is greater than 1.05 V. The cell potential for Cu(s)|Cu2+(aq, 0.5 M)||Cl2(g, 2.0 atm)|Cl-(aq, 2.0 M)|Pt(s) is equal to 1.02 V.
Which of the following statements are FALSE? ΔGo > 0 for Br2(l) + 2 Hg(l) --->...
Which of the following statements are FALSE? ΔGo > 0 for Br2(l) + 2 Hg(l) ---> 2 Br-(aq) + Hg22+(aq). ΔGo < 0 for MnO2(s) + SO2(g) ---> Mn2+(aq) + SO42-(aq). The cell potential for Co(s)|Co2+(aq, 0.1 M)||Fe3+(aq, 1.5 M)|Fe2+(aq,1.5 M)|Pt(s) is less than 1.05 V. The cell potential for Co(s)|Co2+(aq, 1.5 M)||Fe3+(aq, 0.5 M)|Fe2+(aq, 0.1 M)|Pt(s) is greater than 1.05 V. The cell potential for Cu(s)|Cu2+(aq, 0.5 M)||Cl2(g, 2.0 atm)|Cl-(aq, 2.0 M)|Pt(s) is equal to 1.02 V.
static int product(int x,int y){ if(x==0||y==0){//checking if x or y is 0 return 0;//if x or...
static int product(int x,int y){ if(x==0||y==0){//checking if x or y is 0 return 0;//if x or y is 0, then the return value and x*y will be zero. }else if(y<0&&x<0){ x=-x;//Changing the sign of x y=-y;//Changing the sign of y }else if(x>=1){ return (y+product(x-1,y)); } return (x+product(x,y-1)); } find the space complexity and the time complexity of the above algorithm.
1. Prove by contraction that    L = {0^x 1^y 0^x+y | x >= 1 and...
1. Prove by contraction that    L = {0^x 1^y 0^x+y | x >= 1 and y >=1} is not Regular.    Must use the Pumping Lemma. [ HINT: Describe the language in English first. Use example 3 from the lecture notes ] a) Choose one S from L where S is longer than N       (Describe S in terms of N and M)       S = **??** b) List all places v can be in S: (i.e. all possible...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT