Question

In: Computer Science

python Write a function pack_to_5(words) that takes a list of string objects as a parameter and...

python

Write a function pack_to_5(words) that takes a list of string objects as a parameter and returns a new list containing each string in the title-case version. Any strings that have less than 5 characters needs to be expanded with the appropriate number of space characters to make them exactly 5 characters long. For example, consider the following list:

words = ['Right', 'SAID', 'jO']

The new list would be:

['Right', 'Said ', 'Jo   ']

Since the second element only contains 4 characters, an extra space must be added to the end. The third element only contains 2 characters, so 3 more spaces must be added.

Note:

  • Title case means that the first letter of each word is uppercase and the rest lower-case. You may want to use the .title() string function. (e.g. 'SMITH'.title() returns 'Smith')
  • You should not modify the original list of string objects.
  • You may assume that each word in the list does not have more than 5 characters.
  • You may want to use the format string: '{:<5}' in this question.

For example:

Test Result
words = ['Right', 'SAID', 'jO']
result = pack_to_5(words)
print(words)
print(result)
['Right', 'SAID', 'jO']
['Right', 'Said ', 'Jo   ']
words = ['help', 'SAID', 'jO', 'FUNNY']
result = pack_to_5(words)
print(words)
print(result)
['help', 'SAID', 'jO', 'FUNNY']
['Help ', 'Said ', 'Jo   ', 'Funny']
result = pack_to_5([])
print(result)
[]

Solutions

Expert Solution

Code:

def pack_to_5 (words): #pack_to_5() function
final_result=[] #final_result[] list
for word in [word.title() for word in words]: #for each word in words convert it to title case and take every word in that
if (len(word)!=5): #check if the word lenght is 5 or not if not 5
diff=5-len(word) #take the difference of 5-length(word)
spaces=diff * ' ' #take string and store the number of spaces
word=word+(spaces) #finally concatenate the spaces
final_result.append(word) #finally append the word to final_result list
return final_result #return final_result

print("Test Case-1")
words = ['Right', 'SAID', 'jO'] #test case 1 input list
result = pack_to_5 (words) #call pack_to_5(words) and store the returned result
print(words)
print(result) #print result


print("\nTest Case-2")
words=['help','SAID','jO','FUNNY'] #test case 2 input list
result = pack_to_5 (words) #call pack_to_5(words1) and store the returned result
print(words)
print(result) #print result


print("\nTest Case-3")
words=[] #test case 3 input list
result = pack_to_5 (words) #call pack_to_5(words2) and store the returned result
print(words)
print(result) #print result

Code and Output Screenshots:

Note : For better understanding i have added a print statement for displaying each test case . we can remove that print statement if we want only input and output list.

Note : if you have any queries please post a comment thanks a lot..always available to help you...


Related Solutions

Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of string as arguments. When the function is...
Write a Python function that takes a list of string as arguments. When the function is called it should ask the user to make a selection from the options listed in the given list. The it should get input from the user. Place " >" in front of user input. if the user doesn't input one of the given choices, then the program should repeatedly ask the user to pick from the list. Finally, the function should return the word...
Write a function that takes a C string as an input parameter and reverses the string.
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
in c++ Write a function that takes a C string as an input parameter and reverses...
in c++ Write a function that takes a C string as an input parameter and reverses the string. The function should use two pointers, front and rear. The front pointer should initially reference the first character in the string, and the rear pointer should initially reference the last character in the string. Reverse the string by swapping the characters referenced by front and rear, then increment front to point to the next character and decrement rear to point to the...
Write a function named "characters" that takes a string as a parameter and returns the number...
Write a function named "characters" that takes a string as a parameter and returns the number of characters in the input string
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and...
Write a recursive function in python called make_palindrome that takes a sequence as a parameter and returns a new sequence that is twice the length of the parameter sequence but that contains the contents of the original in palindrome form. For example, if the sequence "super" is passed into the function, the function will return "superrepus".
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of...
Write a function called fnReadInParameters() that takes as parameter(s): • a string indicating the name of the parameter le This function should return a dictionary that stores all of the parameter le data of the SIR model in a format that we will describe further below.
Python Programcomplete theprocessString(string)function. This function takesin a string as a parameter and prints the...
Python Program complete theprocessString(string)function. This function takes in a string as a parameter and prints the average number of characters per word in each sentence in the string. Print the average character count per word for each sentence with 1 decimal precision(see test cases below).-Assume a sentence always ends with a period (.)or when the string ends. -Assume there is always a blank space character(" ")between each word. -Do not count the blank spaces between words or the periods as...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns...
Python program. Write a function called cleanLowerWord that receives a string as a parameter and returns a new string that is a copy of the parameter where all the lowercase letters are kept as such, uppercase letters are converted to lowercase, and everything else is deleted. For example, the function call cleanLowerWord("Hello, User 15!") should return the string "hellouser". For this, you can start by copying the following functions discussed in class into your file: # Checks if ch is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT