Question

Write the function most_factors(numbers) that returns the integer from the list numbers that has the most...

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.

Homework Answers

Answer #1

#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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Create a function called lists_Sum(lists) that takes in a list of lists, each element in each...
Create a function called lists_Sum(lists) that takes in a list of lists, each element in each sublist will be a number. Your function will return the product of all the numbers in the inner lists added together. solve for python EX.  lists_sum ([[2,2],[3,5],[5,8]]) returns 59
Write the function decipher(s, frequencies), which will decipher a Caesar cipher encrypted string s using the...
Write the function decipher(s, frequencies), which will decipher a Caesar cipher encrypted string s using the letter frequencies in frequencies, and return the plaintext string. Here is a sample of how this function would be used: if __name__ == '__main__': # this file path works on my computer. # you will need to set this path to work on your computer! f = open('/Users/azs/code/bu/cs108/assignments/romeo.txt') text = f.read() f.close() d = letter_frequencies(text) s1 = 'h svun aptl h nv pu h...
Write a function count_div5(nested_list) that takes in a list of lists of integers, and returns a...
Write a function count_div5(nested_list) that takes in a list of lists of integers, and returns a list of integers representing how many integers in each sublist of the original were divisible by 5. Examples: >>> count_div5([[5, 3, 25, 4], [46, 7], [5, 10, 15]]) [2, 0, 3] >>> count_div5([]) [] >>> count_div5([[-20, 10, 2, 4, 5], [], [5], [8, 25, 10], [6]]) [3, 0, 1, 2, 0]
In Python: Sublist of list A is defined as a list whose elements are all from...
In Python: Sublist of list A is defined as a list whose elements are all from list A. For example, suppose list A = [0, 1, 2, 3, 4, 5, 6], its has many sublists and one of them is [0, 1, 3] because elements 0, 1 and 3 are all contained in list A. Define a function named returnComplement that accepts two integer lists as the parameter (one of the list is the sublist of the other). Suppose names...
Write a PYTHON function called myMax which accepts a LIST of numbers and returns the maximum...
Write a PYTHON function called myMax which accepts a LIST of numbers and returns the maximum number in the list. Do NOT use Python’s built-in function max. Example: result = myMax([-999000, -1000000, -2000000]); print(result) #output is -999000 Example: result = myMax([1000000, 1000001, 1000002]); print(result) #output is 1000002
using dr.racket programing language If we write a function that tests whether a list contains only...
using dr.racket programing language If we write a function that tests whether a list contains only strings, odd numbers, or even numbers, you will notice that the code that iterates through the list stays the same, with the only change being the predicate function that checks for the desired list element. If we were to write a new function for each of the tests listed above, it would be more error-prone and an example of bad abstraction. We could write...
convert code from python to cpp L = [2,7,4,19,45,16,9,13,8,69,55,11,23,98,14,5,1,3]   #Merging function def merge(M,N): merging_list = []...
convert code from python to cpp L = [2,7,4,19,45,16,9,13,8,69,55,11,23,98,14,5,1,3]   #Merging function def merge(M,N): merging_list = []        //create empty list to merge the lists M and N if not M:                //if M is empty list, m = 0                //set m = 0 else: m = len(M)             //otherwise, set m = len(M) if not N:                //if N is empty list, n = 0       ...
Write the function has duplicate() which takes as input a vector v and outputs true if...
Write the function has duplicate() which takes as input a vector v and outputs true if there is a re- peated element in v and false if there is no repeated elements. Below is the algorithm which you should implement. default output is 0 for elt1 in v: for elt2 later in v than elt1: if elt1==elt2, make output 1, break, end end end Checking if elt1==elt2 counts as one step. Setting the output value also counts as one step....
Write a function, grade(score), which is given score, and it returns a letter grade — the...
Write a function, grade(score), which is given score, and it returns a letter grade — the grade for that mark — according to this scheme: A: >= 90 B: [80, 90) C: [70, 80) D: [60, 70) F: < 60 The square and round brackets denote closed and open intervals. A closed interval includes the number, and an open interval excludes it. So 79.99999 gets grade C, but 80 gets grade B. Define the main function as follows: In main,...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort...
in C++ Please and thanks Here is a list of 6 numbers. Use the selection sort algorithm to sort this list. Fill in this table with each iteration of the loop in the selection sort algorithm. Mark the place from which you are looking for the 'next smallest element'. In this display, the upper numbers are the indices, the lower numbers are in the corresponding positions. Use the several rows provided to show the sequence of steps. 0 1 2...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT