Jimmy
Jimmy

Reputation: 182

Find maximum degree of divisibility given list of number

I'm trying to write a function that find degree of divisibility of a list of number.

Example given list : [2,4,8,2]

So the result of maximum divisibility here is 4. I tried doing this but it return 8 instead of 4

    def find_most_divisors(input_list):
        test_list=[]

        for i in input_list:
            c=0
            for n in range (1,i+1):
                if i%n==0:
                   c+=1
            test_list.append(c)
        return input_list[test_list.index(max(test_list))]

    x = [2,4,8,2]
    print (find_most_divisors(x))

Upvotes: 1

Views: 2735

Answers (2)

ppwater
ppwater

Reputation: 2277

You can change your return statement to max(test_list). like this: return max(test_list)

The whole code:

def find_most_divisors(input_list):
        test_list=[]
        for i in input_list:
            c=0
            for n in range (1,i+1):
                if i%n==0:
                   c+=1
            test_list.append(c)
        return max(test_list)

x = [2,4,8,2]
print (find_most_divisors(x))

Reason: input_list[test_list.index(max(test_list))] -> input_list[test_list.index(4)] -> input_list[2] -> 8

Upvotes: 1

Just change your return statement to max(test_list), and it should work!

Upvotes: 1

Related Questions