1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
worty [1.4K]
3 years ago
8

Write a program that displays the values in the list numbers in ascendingorder sorted by the sum of their digits.

Computers and Technology
1 answer:
melisa1 [442]3 years ago
4 0

Answer:

Here is the Python program.

def DigitList(number):  

   return sum(map(int, str(number)))  

 

list = [18, 23, 35, 43, 51]  

ascendList = sorted(list, key = DigitList)  

print(ascendList)

Explanation:

The method DigitList() takes value of numbers of the list as parameter. So this parameter is basically the element of the list. return sum(map(int, str(number)))  statement in the DigitList() method consists of three methods i.e. str(), map() and sum(). First the str() method converts each element of the list to string. Then the map() function is used which converts every element of list to another list. That list will now contain digits as its elements. In short each number is converted to the string by str() and then the digit characters of each string number is mapped to integers. Now these digits are passed to sum() function which returns the sum. For example we have two numbers 12 and 31 in the list so each digit is 1 2 and 3 1 are added to get the sum 3 and 4. So now the list would have 3 4 as elements.

Now list = [18, 23, 35, 43, 51] is a list of 5 numbers. ascendList = sorted(list, key = DigitList)  statement has a sorted() method which takes two arguments i.e. the above list and a key which is equal to the DigitList which means that the list is sorted out using key=DigitList. DigitList simply converts each number of list to a another list with its digits as elements and then returns the sum of the digits. Now using DigitList method as key the element of the list = [18, 23, 35, 43, 51] are sorted using sorted() method. print(ascendList) statement prints the resultant list with values in the list in ascending order sorted by the sum of their digits.

So for the above list [18, 23, 35, 43, 51] the sum of each number is 9 ,5, 8, 7, 6 and then list is sorted according to the sum values in ascending order. So 5 is the smallest, then comes 6, 7, 8 and 9. So 5 is the sum of the number 23, 6 is the sum of 51, 7 is the sum of 43, 8 is the sum of 35 and 9 is the sum of 18. So now after sorting these numbers according to their sum the output list is:

[23, 51, 43, 35, 18]                                                                                                          

You might be interested in
Convert ⅖ pie radian to degree​
aniked [119]

Answer:

Convert to a decimal.

22.9183118°

Explanation:

7 0
3 years ago
A batholith is an example of a(n) _____ igneous rock (one of the two main igneous rock groups).
Naily [24]
A batholith is an intrusive igneous rock.
3 0
4 years ago
Read 2 more answers
When the computer is turned on, the operating system is loaded from the hard disk to the _________________. *​
Pavlova-9 [17]

Answer:

The main memory (ROM)

7 0
3 years ago
Tell me the defination of working principle of computer with all information​
Rudik [331]

Answer:

Computer do the work primarily in the machine and we can not see, a control centre that converts the information data input to output.

Explanation:

Please mark as brainlist please please!

7 0
4 years ago
Define watcher block
DochEvi [55]

Answer:

Happens when you are watching a series / film / drama / etc. but can not retain the attention span to watch for long periods of time. As well as the shortened viewing time, the ability to retain what has been viewed is impaired.

Explanation:

5 0
3 years ago
Read 2 more answers
Other questions:
  • Microsoft windows server and linux are examples? of:
    12·1 answer
  • Several steps are involved in creating a presentation. what is the last step in the process before preparing to presenting it?
    12·1 answer
  • When running JavaApplication8, the following output is seen on the display:
    15·1 answer
  • Jax needs to write a block of code that will organize a list of items alphabetically. Which function should he use? append() pri
    12·1 answer
  • Which of the following is an example of fine art? 1. fashion 2. interior design 3. painting 4. product design
    7·2 answers
  • Match the tool to the task it helps to accomplish. Thesaurus allows the user to find the synonyms and antonyms of a word Smart L
    8·1 answer
  • The following program segment is designed to compute the product of two nonnegative integers X and Y by accumulating the sum of
    9·1 answer
  • An empty frame used to reserve space for a picture to be inserted at a later time is known as a _____. shape image picture place
    7·1 answer
  • Which function in the plane manager allows you to create a new plane where both x and y axes are perpendicular to the line selec
    14·1 answer
  • 14. The term used to describe an attribute that can function like a primary key is a?​
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!