Top level of a recursive function

Schachner, Joseph (US) Joseph.Schachner at Teledyne.com
Tue Dec 13 12:19:07 EST 2022


Reducing repetitiveness has made this code harder to read. I had to think about what it is doing.  It might be slightly faster, but in my opinion it is not worth it.  

--- Joseph S.


Teledyne Confidential; Commercially Sensitive Business Data

-----Original Message-----
From: Stefan Ram <ram at zedat.fu-berlin.de> 
Sent: Tuesday, December 13, 2022 10:25 AM
To: python-list at python.org
Subject: Re: Top level of a recursive function

Supersedes: <reduce-20221213161758 at ram.dialup.fu-berlin.de>

ram at zedat.fu-berlin.de (Stefan Ram) writes:
>def rest( s ):
>    return "(" + s[ 0 ] +( rest( s[1:] ) if len( s )> 1 else '' )+ ')'
>def nest( s ):
>    return( s[ 0 ] if s else '' )+( rest( s[1:] )if len( s )> 1 else '' )

  Below, I have tried to reduce repetitiveness a bit.

  (PS: Now, one "if" remains; less ifs are not possible
  in the case of controlled recursion.)

def rest( s ):
    return '(' + nest( s )+ ')'

def nest( s ):
    return s[ :1 ]+( rest( s[ 1: ])if s[ 1: ]else '' )

fred = nest




More information about the Python-list mailing list