1

I am creating a program to figure out the highest number of decimals in a list of numbers. Basically, a list with [123, 1233] would return 4 because 1233 has four numbers in it and it is the largest. Another example would be that [12, 4333, 5, 555555] would return 6 because 555555 has 6 numbers.

Here is my code.

def place(listy):  
    if len(listy) == 1:  
        decimal = len(str(listy[0]))    
        print(decimal)  
    else:  
        if len(str(listy[0])) >= len(str(listy[1])):  
            new_list = listy[0:1]  
            for i in listy[2:]:  
                new_list.append(i)  
            place(new_list)  
        else:   
            place(listy[1:]) 

Now, when I use print(decimal) it works, but if I change print(decimal) to return decimal, it doesn't return anything. Why is this? How do I fix this? I have come across these return statements which doing run a lot of times. Thanks in advance!

11
  • 4
    Please note that you seem to be mixing tabs and spaces in your source code. That is not good... Commented Mar 22, 2013 at 18:46
  • 1
    If you just call place(listy) with a return statement, it won't print. If you instead do print place(listy) it will print the returned value. Is this what you're doing? Your question seems a bit vague to me Commented Mar 22, 2013 at 18:46
  • 1
    What do you mean by "does not return anything"? Please provide the code where you call this function. Commented Mar 22, 2013 at 18:48
  • 1
    Just fyi, the code would be much simpler to use a list comprehension or generator expression like max(len(str(number)) for number in numbers) Commented Mar 22, 2013 at 18:49
  • 3
    maybe I'll not answer your question, but a simpler way to do this could be len(str(max(list_variable))) Commented Mar 22, 2013 at 18:49

5 Answers 5

9

When you do a recursive call (i.e. when place calls place, and the called place returns a value, then the calling place must return it as well (i.e. the return value "bubbles up" to the initial caller).

So you need to replace every recursive call

place(...)

with

return place(...)

As others have said, there are easier solutions, such as using max(). If you want to keep a recursive approach, I would refactor your code as follows:

def place2(listy):
    if len(listy) < 1:
        return None
    elif len(listy) == 1:
        return len(str(listy[0]))
    else:
        v0, v1 = listy[0], listy[1]
        if v1 > v0:
            return place2(listy[1:])
        else:
            return place2([listy[0]]+listy[2:])

Although this is tail-recursive, Python does not really care so this approach will be inefficient. Using max(), or using a loop will be the better solution in Python.

Sign up to request clarification or add additional context in comments.

Comments

1

It's not that the return doesn't do anything, it's that you don't propagate the return from your recursive call. You need a few more returns:

def place(listy):  
    if len(listy) == 1:  
        decimal = len(str(listy[0]))    
        return decimal
    else:  
        if len(str(listy[0])) >= len(str(listy[1])):  
            new_list = listy[0:1]  
            for i in listy[2:]:  
                new_list.append(i)  
            return place(new_list)  # <-- return added
        else:   
            return place(listy[1:]) # <-- return added

You can see the print at any level, but to get it back to the caller it needs to be propagated.

Comments

1

The function does return the value, but it's not printing it out.
A simple way to solve this is, just call the function within a print statement.
That is:

print(place(listy))

Comments

0

If all you want is to find the maximum length of a list of integers, consider:

max([len(str(n)) for n in N])

For example

N = [1,22,333,4444]
max([len(str(n)) for n in N]) # Returns 4

N = [12, 4333, 5, 555555]
max([len(str(n)) for n in N]) # Returns 6

Note: This will only work for positive integers.

Or more simply:

len(str(max(N)))

Which will also only work for positive integers.

Comments

-2

Use ''global variable'' (google it) to access and change a variable defined outside of your function.

1 Comment

When someone arrives at this site, it should be the last stop before finding a solution. "Google it" is not helpful. Please, read How to Answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.