Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python string length recursion

I'm blanking out trying to write a simple function to count the string length recursively.

I can do sums, fibonacci, and factorial easy but I'm trying to create the simplest function with only one parameter, I don't like having a second just as a counter index..

Can any post something small for me?

like image 848
John Redyns Avatar asked Apr 14 '11 21:04

John Redyns


1 Answers

Is this what you are looking for?

def recursiveLength(theString):
    if theString == '': return 0
    return 1 + recursiveLength(theString[1:])
like image 150
Donovan Avatar answered Oct 01 '22 04:10

Donovan