Higher order functions and closures - 2 - Hands On
1654
2. Higher order functions and closures - 2
• Define a function factory, with a variable n initialized to zero, and two inner functions current and counter.
• current must return the current value of n.
• counter must increment the value of n by one.
Hint: Check the closure functions f_current and f_counter, and return the current and counter functions from factory accordingly.
• Use the 'Test against custom input' box to output the result for debugging. Provide an integer (refer test case samples) to display the output / error.
Given Input:
2
Expected Output:
2
3
Code Hints:
#!/bin/python3
import sys
import os
# Add the factory function implementation here
f_current, f_counter = factory(int(input()))
if __name__ == "__main__":
with open(os.environ['OUTPUT_PATH'], 'w') as fout:
func_lst = [f_current, f_counter]
res_lst = list()
for func in func_lst:
res_lst.append(func())
fout.write("{}\n{}".format(*res_lst))
Program:
#!/bin/python3
import sys
import os
# Add the factory function implementation here
def factory(v):
n=0
def current():
n=v
print(n)
return n
def counter():
n = v + 1
print(n)
return n
return current, counter
f_current, f_counter = factory(int(input()))
if __name__ == "__main__":
with open(os.environ['OUTPUT_PATH'], 'w') as fout:
func_lst = [f_current, f_counter]
res_lst = list()
for func in func_lst:
res_lst.append(func())
fout.write("{}\n{}".format(*res_lst))
Output:
Test Case Input:
89
Test Case Expected Output:
89
90
This Particular section is dedicated to Programs only. If you want learn more about Python. Then you can visit below links to get more depth on this subject.
# C Tutorials
# JAVA Tutorials
# HTML Tutorials
# Computer Fundamental
# Data Structure
# DBMS Tutorials
SQL
# C# Language
# R Language
# PHP
# Python
# Vue JS
2 3
Code Hints:
#!/bin/python3
import sys
import os
# Add the factory function implementation here
f_current, f_counter = factory(int(input()))
if __name__ == "__main__":
with open(os.environ['OUTPUT_PATH'], 'w') as fout:
func_lst = [f_current, f_counter]
res_lst = list()
for func in func_lst:
res_lst.append(func())
fout.write("{}\n{}".format(*res_lst))
Program:
#!/bin/python3
import sys
import os
# Add the factory function implementation here
def factory(v):
n=0
def current():
n=v
print(n)
return n
def counter():
n = v + 1
print(n)
return n
return current, counter
f_current, f_counter = factory(int(input()))
if __name__ == "__main__":
with open(os.environ['OUTPUT_PATH'], 'w') as fout:
func_lst = [f_current, f_counter]
res_lst = list()
for func in func_lst:
res_lst.append(func())
fout.write("{}\n{}".format(*res_lst))
Output:
Test Case Input:
89
Test Case Expected Output:
89
90
This Particular section is dedicated to Programs only. If you want learn more about Python. Then you can visit below links to get more depth on this subject.
# C Tutorials
# JAVA Tutorials
# HTML Tutorials
# Computer Fundamental
# Data Structure
# DBMS Tutorials
SQL
# C# Language
# R Language
# PHP
# Python
# Vue JS
Test Case Input:
89
Test Case Expected Output:
89
90
89 90
This Particular section is dedicated to Programs only. If you want learn more about Python. Then you can visit below links to get more depth on this subject.