Write the function most_factors(numbers) that returns the integer from the list numbers that has the most factors (divisors without remainder). For example:
>>> most_factors([5,10,16,20,25]) 20 # because 20 has the most factors of any of these numbers # 6 factors, i.e., [1, 2, 4, 5, 10, 20] >>> most_factors([1, 2, 3, 4, 5]) 4 # because 4 has the most factors of any of these numbers # 3 factors, i.e., [1, 2, 4]
Hints:
For each element in numbers, call your get_factors(n) function (from above) to obtain the list of its factors.
Use a list comprehension to build a list-of-lists, in which you will count the number of factors for each number. This part should produce a list containing sublists, where each sublist relates to one element from numbers.
Use the built-in max() function find the sublist with the most factors.
Don’t be afraid to print out intermediary values/lists to discover/understand what’s going on inside the function.
#Python code
def most_factors(li):
sub_li = []
for i in li:
count = 0
for j in range(1, max(li)):
if i % j == 0:
count += 1
sub_li.append(count)
# print("most factors sub list: ", sub_li)
most_index = sub_li.index(max(sub_li))
return li[most_index]
# test
result = most_factors([5, 10, 16, 20, 25])
print(result)
#output
//If you need any help regarding this solution...... please leave a comment..... thanks
Get Answers For Free
Most questions answered within 1 hours.