Issue
How can I convert these two functions to use lambda notation?
def sum_digits(number):
if number == 0:
return 0
else:
return (number % 10) + sum_digits(number / 10)
def count_digit(number):
if number == 0:
return 0
else:
return 1 + count_digit(number/10)
Solution
sum_digits = lambda number: 0 if number == 0 else (number % 10) + sum_digits (number / 10)
count_digit = lambda number: 0 if number == 0 else 1 + count_digit(number/10)
Incidentally, this is a bad time to use lambdas, since you need the function names in order for them to call themselves. The point of lambdas is that they're anonymous.
Answered By - Arya McCarthy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.