Ask Question
3 April, 01:14

The function below takes a single parameters: a list of numbers called num_list. Return a new list containing just the three largest numbers (in any order). Your function shouldn't modify the original list. You can assume that the list always contains at least 3 numbers. The easiest way to accomplish this is to sort the provided data and then slice, but you'll need to make a copy of the data (so that you don't modify the original list).

print. py 1. hef three_largest numbers (num_list) :

+3
Answers (1)
  1. 3 April, 05:06
    0
    def three_largest_numbers (num_list) : sort_list = num_list sort_list. sort (reverse=True) return sort_list[0:3] print (three_largest_numbers ([5, 7, 1, 8, 9, 3, 6]))

    Explanation:

    Firstly, we declare a function that will take one input list, num_list (Line 1). To avoid modifying the original num_list, we create a copy of num_list and assign it to a new list, sort_list (Line 2) in the function. Next, we use Python list sort function to sort the list in descending order (Line 3). At last, return the first three elements of the sort_list as output (Line 4).

    We test the function using a sample list (Line 6) and we shall get [9, 8, 7] printed in terminal.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “The function below takes a single parameters: a list of numbers called num_list. Return a new list containing just the three largest ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers