Ash is now an expert in python function topic. So, he decides to teach others what he knows by making a question on it. Problem statement of his question is as follows.
Your task is to write a function outer_layer(num) that returns function inner_layer, where num is a positive integer. Function inner_layer(div) check whether num is divisible by div or not, its return type is bool (True/ False).
num is of type string
div is of type int and belongs to {2, 5, 9, 10}
Functions are Objects - Since functions are just like variables, they can be returned from a function!
def outer_layer():
print 'This is outer layer'
def inner_layer():
print 'This is inner layer'
return inner_layer
def __name__ == '__main__':
func_obj = outer_layer() # func_obj now becomes inner_layer, and This is outer layer is printed on the screen.
func_obj()
Output:
This is outer layer
This is inner layer
def outer_layer(num):
def inner_layer(div):
for i in range(len(div)):
if (num % div[i]) != 0:
return False
return True
return inner_layer
if __name__ == '__main__':
num = int(input("Input num: "))
print("Output:")
func_obj = outer_layer(num)
print(func_obj([2, 5, 9, 10]))
Comments
Leave a comment