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

Recursion on odd to be front, even in the back

I am new to python.
I am writing a recusion to returns a COPY of the list with odds at front, evens in the back.
For example: [3,4,5,6] returns [3,5,6,4].
How should I break the problem into small pieces.

def oddsevens(thelist):
    if thelist == []:
        return []
    if thelist[0] % 2 == 0:

>Solution :

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

try this:

if thelist == []:
    return []
if thelist[0] % 2 == 0:
    return oddsevens(thelist[1:]) + thelist[:1]
else:
    return thelist[:1] + oddsevens(thelist[1:])

Let me know if you have any further questions!

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