+ 1

How can I make a function that receives an integer and counts the number of odd and even digits?

Must be using the lambda function.

14th Mar 2020, 8:21 PM
Marco Agüero
Marco Agüero - avatar
4 Réponses
+ 3
I think you should define the range of the numbers, then use the range to create two diff list, one for even digits, and another for odd digits, then print the length of each list... Happy coding 😊 Keep coding 🙏
14th Mar 2020, 8:43 PM
Alfred Juma
Alfred Juma - avatar
+ 2
If you need to use lambda, you are probably meant to use a higher order function, like filter() You can convert a number to a list of digits first: digits = list(map(int, str(number))) Then use filter to take only odd ir even digits, and count them with the len() function. odds = len(list(filter(lambda d: d%2==1, digits))) Alternatively you could achieve the same by using sum() and map(), converting each digit to True or False, based on them being odd or even.
15th Mar 2020, 10:39 AM
Tibor Santa
Tibor Santa - avatar
14th Mar 2020, 11:28 PM
Sahil Shingate
Sahil Shingate - avatar
+ 1
def count(num): o=0 e=0 print(" number : ",num) for i in num: if int(i)%2==0 : e+=1 else: o+=1 print(" even : ",e) print(" odd : ",o) n=input() count(n)
14th Mar 2020, 11:30 PM
Sahil Shingate
Sahil Shingate - avatar