1

This code just find the largest number by divide and conquer algorithm. The code by itself works perfectly and finds the larges number and stores in my_data[0]. It prints the largest number correctly. But when I try to return it and store in 'val' variable, 'None' gets stored and not the largest number. Why the return statement is returning only 'none' instead my_data[0].. ?? I am not able to proceed the code since the return value is not correct. Can anyone say what fault is this ? Thanks in advance..

def largest(my_data):
    if len(my_data) == 1:
        print my_data[0]
        return my_data[0]
    else:
        alist = []
        blist = []
        for i in range(0,len(my_data),2):
            if my_data[i] > my_data[i+1]:
                alist.append(my_data[i])
                blist.append(my_data[i+1])
            else:
               alist.append(my_data[i+1])
               blist.append(my_data[i])
        my_data = list(alist)
        largest(my_data)
my_data = [12,45,63,13,76,87,23,85,56,123,7856,132,786,124,756,25366468]
val = largest(my_data)
print val
3
  • Also stackoverflow.com/questions/17173822/… Commented May 16, 2014 at 6:12
  • This isn't really a divide and conquer algorithm. Divide and conquer splits a problem into multiple subproblems; for example, for this problem, divide and conquer would split the list in two, find the max of each half, and then return whichever sub-maximum is bigger. Commented May 16, 2014 at 6:12
  • Why are you going through all this, anyway? Even if you can't just use max(my_data), it'd be simpler to just iterate through the list, keeping track of the biggest element you've seen so far. Commented May 16, 2014 at 6:13

2 Answers 2

3

When you write recursive code, you need to make sure that the value returned by the recursive call has to be propagated back. So,

    largest(my_data)

has to be returned like this

    return largest(my_data)

Also,

my_data = list(alist)

this statement is not necessary, if you are really trying to convert alist to a list. Because it already is a list. So, you can directly write

    return largest(alist)

Just curious, you are discarding the values added to blist, then why bother adding?

So, if you really don't need blist, then your code can be written with list comprehension, like this

return largest([my_data[i] if my_data[i] > my_data[i+1] else my_data[i+1]
                  for i in range(0,len(my_data),2)])
Sign up to request clarification or add additional context in comments.

1 Comment

heyy.. thanks got it.. and now returning correctly.. Thanks a lot.. By the way this is just a staring code.. There are operations to be done on the blist also :) :)
0

If you read your code, you'll notice that nothing is returned in the else clause. If you want this to be a recursive function, the usual thing is to return the value from the recursive call.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.