Recursion on odd to be front, even in the back

Question:

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:

Asked By: Ming

||

Answers:

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!

Answered By: Sabrina
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.