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
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...
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]
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...
Java please. Given a sequence of unsorted numbers, determine how badly out of order they are....
Java please. Given a sequence of unsorted numbers, determine how badly out of order they are. Write a program that, for any given sequence of unique natural numbers, will compute the 'distance' between that original ordering and the same set of numbers sorted in ascending order. The distance should be computed by calculating how far displaced each number is in the original ordering from its correct location in the sorted list and summing those results. For instance, given the list...
8.8 LAB: Warm up: People's weights (Lists) (1) Prompt the user to enter four numbers, each...
8.8 LAB: Warm up: People's weights (Lists) (1) Prompt the user to enter four numbers, each corresponding to a person's weight in pounds. Store all weights in a list. Output the list. (2 pts) Ex: Enter weight 1: 236.0 Enter weight 2: 89.5 Enter weight 3: 176.0 Enter weight 4: 166.3 Weights: [236.0, 89.5, 176.0, 166.3] (2) Output the average of the list's elements with two digits after the decimal point. Hint: Use a conversion specifier to output with a...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
Project 1 - NodeList write in c++ with 5 files: main.cpp List.h List.cpp ListNode.h ListNode.cpp Building...
Project 1 - NodeList write in c++ with 5 files: main.cpp List.h List.cpp ListNode.h ListNode.cpp Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3,...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT