1. Wap to print product of first ten numbers.
Answers
Answer:
Handwritten methods:
#just a helper method to not have to write this more than once
def value_checker(n):
if isinstance(n, str):
try:
n=int(n) #raises an ValueError
except ValueError:
return False
if not isinstance(n, int): return False
return n>=0
#recursive method
#the helping bool variable avoids running the valuecheck serval times
def fac_rec(n, valueChecked=False):
if valueChecked or value_checker(n):
return 1 if n==0 else n*fac_rec(n-1, True)
return "NaN"
#iterativ method
def fac_iter(n):
if value_checker(n):
r=1
for i in range(2, n+1):
r*=i
return r
return "NaN"
from functools import reduce
fac_lambda=lambda n:"NaN" if not value_checker(n) else reduce(lambda a, b:a*b, range(1, n+1))
Hope it helped u..✌️