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
Mkey [24]
3 years ago
15

Any suggestions on how to write the following Python code?

Computers and Technology
1 answer:
Ksju [112]3 years ago
6 0

Answer:

Hi

Explanation:

You might be interested in
Write a program that displays the values in the list numbers in ascendingorder sorted by the sum of their digits.
melisa1 [442]

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]                                                                                                          

4 0
3 years ago
What type of coverage pays for damage incurred as a result of theft, vandalism, fire or natural disaster?
Sloan [31]

Comprehensive or hazard insurance

5 0
4 years ago
Plz help code practice for python
laila [671]

Answer:umm

Explanation:

6 0
3 years ago
Read 2 more answers
How do you activate the Slicer Tools contextual tab?
Strike441 [17]

Answer:

i need this one too

Explanation:

4 0
3 years ago
One disadvantage of online information sharing is that:
zubka84 [21]

Answer:

its b i just took the test

Explanation:

3 0
3 years ago
Read 2 more answers
Other questions:
  • What is a possible style of formatting your company could prefer?
    14·2 answers
  • A device that produces a permanent human-readable text of graphic document.
    15·1 answer
  • HELP ASAP ILL DO ANYTHING
    6·1 answer
  • Which AWS service is a managed service that makes it easy to set up, operate, and scale a relational database in the cloud?​
    9·1 answer
  • A classmate is texting a false rumor around school that another student was arrested for shoplifting. This behavior can be descr
    9·2 answers
  • When you close a file, what happens to the undo history list for that file?
    5·1 answer
  • The physical parts of the computer that you can see and touch are called______.
    5·1 answer
  • Which option is considered a part of the document that is used to collect specific and predefined information?
    7·1 answer
  • Please help me!!!
    8·2 answers
  • I have no idea what I’m doing and this is due in 45 minutes
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!