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 function named "replacement" that takes a string as a parameter and returns an identical...
Write a function named "replacement" that takes a string as a parameter and returns an identical string except with every instance of the character "w" replaced with the character "v" My code: function replacement(word){ var str=word; var n=str.replace("w","v"); return n; } Syntax Error: function replacement incorrect on input Not sure how to fix? Can't use a loop for answer
} 1. write a function that takes a string as parameter, return true if it’s a...
} 1. write a function that takes a string as parameter, return true if it’s a valid variable name, false otherwise. You can use keyword module’s  iskeyword() to determine is a string is keyword. import keyword keyword.iskeyword(var)   #returns true or false }2. write a function that returns the length of a string (without using len() function) }3. write a function that counts number of vowels in a string }4. write a function that checks if a word is palindrome PYTHON PROGRAMMING
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT