Question

In: Computer Science

Transforming a List One typical kind of list processing is to transform the elements of a...

Transforming a List

One typical kind of list processing is to transform the elements of a list, producing a copy of the list but with the elements transformed.

Write a function definition of add_sizes using a for loop that takes a list of strings, say strings, and returns a list of pairs consisting of the elements of strings together with their lengths.

def add_sizes(strings):
"""
Return the list of pairs consisting of the elements of strings together
with their sizes.

Parameters:
list<str>: a list a strings

Return:
list<tuple<str, int>>: a list of strings and their corresponding
lengths
  
"""
# add your code here

Solutions

Expert Solution

Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

#code

def add_sizes(strings):
    """
    Return the list of pairs consisting of the elements of strings together
    with their sizes.

    Parameters:
    list<str>: a list a strings

    Return:
    list<tuple<str, int>>: a list of strings and their corresponding
    lengths

    """
    #creating an empty list
   
result=[]
    #looping through each string in strings
   
for s in strings:
        #appending to result list a tuple containing s and length of s
       
result.append((s,len(s)))
    #returning the result
   
return result


#testing the above method using a list of strings
strings=['hello','how','is','Python','?']
print(add_sizes(strings)) #[('hello', 5), ('how', 3), ('is', 2), ('Python', 6), ('?', 1)]

#output

[('hello', 5), ('how', 3), ('is', 2), ('Python', 6), ('?', 1)]


Related Solutions

Recall the four elements of processing that improve learning. Explain which elements of processing are incorporated...
Recall the four elements of processing that improve learning. Explain which elements of processing are incorporated in this​ teacher's use of the KWL​ chart, and describe how they apply.
One of the elements that is always found on a list of key leadership traits is...
One of the elements that is always found on a list of key leadership traits is trust.Subdivides trust into cognitive trust and affective trust. Which one do you prefer in your business relationships? Why? How have you built this trust?
Which of these items are elements of a typical scope statement?
 Which of these items are elements of a typical scope statement? (You may select more than one answer. Single click the box with the question mark to produce a check mark for a correct answer and double click the box with the question mark to empty the box for a wrong answer.) List of stakeholders Initiation date Limits and exclusions Reviews with customer Technical requirements Project Leadership
Briefly describe the elements of the transaction processing cycle.
Briefly describe the elements of the transaction processing cycle.
Discuss the key elements that are required to transform an organization, which is just beginning this...
Discuss the key elements that are required to transform an organization, which is just beginning this journey? What do you think is the critical success factor?
list of approved POCT locations in hospital and what kind of POC each one do?
list of approved POCT locations in hospital and what kind of POC each one do?
Consider a four-step serial process with processing times given in the following list. There is one...
Consider a four-step serial process with processing times given in the following list. There is one machine at each step of the process, and this is a machine-paced process. bullet Step 1: 25 minutes per unit bullet Step 2: 15 minutes per unit bullet Step 3: 30 minutes per unit bullet Step 4: 20 minutes per unit Assuming that the process starts out empty, how long will it take (in hours) to complete a batch of 105 units?
List and explain the steps in loan processing
List and explain the steps in loan processing
list the modifications involved in processing mRNA
list the modifications involved in processing mRNA
List out appropriate usage of Online processing and Batch processing with reasoning for each.
List out appropriate usage of Online processing and Batch processing with reasoning for each.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT