Hello,
my code works fine when I don't have white spaces but I can't make it work when the string has.

I am trying sth like:

def lengthR(mystring):

    count=0
    if mystring=='':
        return 0
    while (mystring!='') and (' ' not in mystring):
        mystring=mystring[0:-1]
        lengthR(mystring)
        count+=1    
    return count

but it returns 0 if there is space in the string.

After the while statement I define a new string which is reduced by 1 character (but it is the last character , so I don't know what to do with white space)

Any help?
Thanks!

Recommended Answers

All 9 Replies

Use either recursion or a while loop, but not both:

def lengthR(mystring, count=0):
    '''
    recursive function to count char in mystring
    excluding spaces
    '''
    if mystring == '':
        return count

    mystring = mystring[0:-1]
    count += 1
    if mystring.endswith(' '):
        count -= 1
    print(mystring, count)  # test
    return lengthR(mystring, count)

mystring = "Hello Earth"

print(len(mystring))
print('-'*18)
print(lengthR(mystring))

Οκ,thanks!One think though.
When we are calling return lenghtR(mystring,count) ,
count goes=0? Or it has the last value?

Because if I run the above function without the count=0 argument , but insert the count=0 above if mystring=='': ,the function returns always 0 length.

The argument count=0 is the default value in case count is not used, like when you call the function initially. But in your calls inside the recursive function you give count the updated value.

Then , why it doesn't work the same?
I mean,if I use this:

def lengthR(mystring):
    '''
    recursive function to count char in mystring
    excluding spaces
    '''
    count=0
    if mystring == '':
        return count

    mystring = mystring[0:-1]
    count += 1
    if mystring.endswith(' '):
        count -= 1
    return lengthR(mystring)

mystring = "Hello Earth"

print(lengthR(mystring))

You are not passing the count to the function, so it does nothing similar, count is normally given value in return statement, in which case it's value is not 0 at the beginning of the function. The effect of the default parameter is only to take out the need for passing 0 as length in first call to the function at last line of code. The effect could be imitated by having another function in between:

def lengthR_aux(mystring, count):
    '''
    recursive function to count char in mystring
    excluding spaces
    '''
    if mystring == '':
        return count

    mystring = mystring[0:-1]
    count += 1
    if mystring.endswith(' '):
        count -= 1
    print(mystring, count)  # test
    return lengthR_aux(mystring, count)

def lengthR(mystring):
    return lengthR_aux(mystring, 0)

mystring = "Hello Earth"

print(len(mystring))
print('-'*18)
print(lengthR(mystring))

You could also make whole logic in single line with 'ternary if' version of Python:

def lengthR(mystring, count=0):
    '''
    recursive function to count char in mystring
    excluding spaces
    '''
    return lengthR(mystring[1:], (count + 1 if mystring[0]!=' ' else count)) if mystring else count

mystring = "Hello Earth"

print(len(mystring))
print('-'*18)
print(lengthR(mystring))

I still don't get why the above code I posted doesn't work.
Sorry.

You have not done anything to find out what is going on so that is why you don't know. Two added print statements will show what pyTony said, i.e. you don't accumulate the count but just return each individual count from each recursion.

def lengthR(mystring):
    '''
    recursive function to count char in mystring
    excluding spaces
    '''
    count=0
    if mystring == '':
        return count

    mystring = mystring[0:-1]
    count += 1
    if mystring.endswith(' '):
        count -= 1
    print "testing", mystring, count
    x = lengthR(mystring)
    print " x =", x
    return x

mystring = "Hello Earth"
print(lengthR(mystring))

The first line in your function is
count=0
so that is what will happen each time you call the function.
If you use it as an argument, then it is only used when the function is defined, as a default value.

A temporary test print is your friend!

Ok , thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.