Question

In: Computer Science

############callbacks ##def function_1( x ) : return x ** 2 ##def function_2( x ) : return...

############callbacks

##def function_1( x ) : return x ** 2
##def function_2( x ) : return x ** 3
##def function_3( x ) : return x ** 4
##
###### create a list of callbacks to each of the functions
######by referencing their names
##
##callbacks = [ function_1 , function_2 , function_3 ]
##
######display a heading and the result of passing a value to each of the
######named functions:
##
##print( '\nNamed Functions:' )
##for function in callbacks : print( 'Result:' , function( 3 ) )
##
####Task 3. Run the code above. Understand it. Display callback for
####the situation when you need to run 2*function_1, 2*function_2,2*function_3,
####for x = 10, show that your code works.

Solutions

Expert Solution

#source code:

import dis
def function_1(x):
   return x**2
def function_2(x):
   return x**3
def function_3(x):
   return x**4
callbacks=[function_1,function_2,function_3] #callback fucntions
x=[3,6,8] #for passing values purpose
for i in range(len(callbacks)): #iterate lenght of list 3
       print("Result:,function({})={}".format(x[i],callbacks[i](x[i]))) #pass each value to the function and print the output
for i in range(len(callbacks)):
       print("Result:,function({})={}".format(10,2*callbacks[i](10))) #pass the 10 for each function and print the output

#output:

#if you have any doubts comment below.


Related Solutions

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...
Inheritance - Method Calls Consider the following class definitions. class C1(): def f(self): return 2*self.g() def...
Inheritance - Method Calls Consider the following class definitions. class C1(): def f(self): return 2*self.g() def g(self): return 2 class C2(C1): def f(self): return 3*self.g() class C3(C1): def g(self): return 5 class C4(C3): def f(self): return 7*self.g() obj1 = C1() obj2 = C2() obj3 = C3() obj4 = C4() For this problem you are to consider which methods are called when the f method is called. Because the classes form part of an inheritance hierarchy, working out what happens will...
Inheritance - Method Calls Consider the following class definitions. class C1(): def f(self): return 2*self.g() def...
Inheritance - Method Calls Consider the following class definitions. class C1(): def f(self): return 2*self.g() def g(self): return 2 class C2(C1): def f(self): return 3*self.g() class C3(C1): def g(self): return 5 class C4(C3): def f(self): return 7*self.g() obj1 = C1() obj2 = C2() obj3 = C3() obj4 = C4() For this problem you are to consider which methods are called when the f method is called. Because the classes form part of an inheritance hierarchy, working out what happens will...
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...
python def create_fourier_dataset(x, max_val=7): """ Return the sum of sin(n*x)/n where n = 1,2,3,4,5... max_val Remember,...
python def create_fourier_dataset(x, max_val=7): """ Return the sum of sin(n*x)/n where n = 1,2,3,4,5... max_val Remember, n is a scalar quantity (float/int). x is a NumPy array and you should return an equal length NumPy array :param x: numpy array :param max_val: The maximum value :return: a tuple with two numpy arrays x and the sum """
from PIL import Image def stringToBits(string):     return str(bin(int.from_bytes(string.encode(), 'big')))[2:] def bitsToString(bits):     if bits[0:2] != '0b':         bits.
from PIL import Image def stringToBits(string):     return str(bin(int.from_bytes(string.encode(), 'big')))[2:] def bitsToString(bits):     if bits[0:2] != '0b':         bits = '0b' + bits     value = int(bits, 2)     return value.to_bytes((value.bit_length() + 7) // 8, 'big').decode() def writeMessageToRedChannel(file, message):     image = Image.open(file)     width, height = image.size     messageBits = stringToBits(message)     messageBitCounter = 0     y = 0     while y < height:         x = 0         while x < width:             r, g, b, a = image.getpixel((x, y))             print("writeMessageToRedChannel: Reading pixel %d, %d - Original values (%d, %d, %d, %d)"...
def num_to_digit_rec(num, base): """ Return a list of digits for num with given base; Return an...
def num_to_digit_rec(num, base): """ Return a list of digits for num with given base; Return an empty list [] if base < 2 or num <= 0 """ # Write your code here return [] def digit_sum(num, base): """ Return the sum of all digits for a num with given base Your implementation should use num_to_digit_rec() function """ # Write your code here return 0 def digit_str(num, base): """ Given a number and a base, for base between [2, 36]...
def stateTax(amount): taxRate = 0.05 taxAmount = taxRate * amount print('State tax: ',taxAmount) return taxAmount def...
def stateTax(amount): taxRate = 0.05 taxAmount = taxRate * amount print('State tax: ',taxAmount) return taxAmount def countyTax(amount): taxRate = 0.025 taxAmount = taxRate * amount print('County tax: ',taxAmount) return taxAmount def totalTax(state,county): taxAmount = state + county print('Total tax: ',taxAmount) return taxAmount def totalAmount(amount, tax): total = amount + tax return total def main(): amount = float(input('Enter the amount of purchase: ')) sTax = stateTax(amount) cTax = countyTax(amount) tTax = totalTax(sTax,cTax) print('Total amount including tax is: ' ,total) main() I...
Using Python def specialAverage():    Print the number of 0s and then return the average of...
Using Python def specialAverage():    Print the number of 0s and then return the average of either the positive values in the list (if the optional parameter has value 1) or the negative values in the list (if the optional parameter has value 0).    Arguments: a list of numbers : the list can contain any numeric values an integer : this integer is an optional parameter and only takes value 0 or 1, and defaults to 1 if the...
Probability Future Return - X Future Return - Y .1 -10% -35% .2 2% 0% .4...
Probability Future Return - X Future Return - Y .1 -10% -35% .2 2% 0% .4 12% 20% .2 20% 25% .1 38% 45% A. Given RX = 12%, find RY B. Given σy = 20.35%, find σX Calculate CVX and CVY Compare their σ and CV to decide which one is more risky per dollar of return
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT