Question
PYTHON3; DO NOT IMPORT ANY PACKAGES Write a function that takes a list of strings, and insert spaces to every string in the list according
PYTHON3; DO NOT IMPORT ANY PACKAGES
Write a function that takes a list of strings, and insert spaces to every string in the list according to the following rules:
-
If the string is empty, change the empty string to one space
-
Else if the string length is less than or equal to 4, insert a space every character
-
Else if the string length is less than or equal to 8, insert a space every two characters
-
Else insert a space every three characters
Return the new list of strings after inserting spaces.
Notes:
-
Don't insert a space at the end, except when the string is empty.
-
You may find the built-in function str.join() helpful.
Requirements: assert statements, one-line list comprehension. IMPORTANT, PLEASE FOLLOW THE REQUIREMENTS OR ELSE NO CREDIT WILL BE GIVEN
*** When a question requires assert statements: write assert statements to prevent any input that may corrupt your code, including arguments in invalid type, and arguments that do not fit the logic. *** When a question requires one-line list comprehension: your implementation should only return an expression that involves list comprehension. In other words, without the 79-character line length limit, your implementation should fit in exactly one line. Note that if this question also requires assert statements, or you need to declare magic numbers, they would not count as extra lines.
def insert_spaces(lst):
"""
>>> insert_spaces(["", "hola"])
[" ", "h o l a"]
>>> insert_spaces(["sean", "marina", "cumberbatch"])
["s e a n", "ma ri na", "cum ber bat ch"]
>>> insert_spaces(["I love Python!!"])
["I l ove Py tho n!!"]
"""
# YOUR CODE GOES HERE #
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started