Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is 'eovdedn' doing in this function?

I ran across this code while doing some coding challenges and don’t understand how it is working. I don’t understand what the ‘eovdedn’ part is doing. The challenge was to return odd or even if the number was even or odd.

def isEvenOrOdd(num):
    return 'eovdedn'[num % 2::2]

My solution was:

def isEvenOrOdd(num):
    return "even" if num%2==0 else "odd"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

num %2 returns False or True, which can be understood as 0 or 1.

Using the slicing notation ‘eovdedn'[num % 2::2] we can have the following results:

  • If num % 2 equals False (0) :
    'eovdedn'[0::2] = even (returns every 2 characters starting from 0)

  • If num % 2 equals True (1) :
    'eovdedn'[1::2] = odd (returns every 2 characters starting from 1)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading