Home > Blockchain >  Recursion on odd to be front, even in the back
Recursion on odd to be front, even in the back

Time:12-02

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:

CodePudding user response:

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!

  • Related