Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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:

  1. Don't insert a space at the end, except when the string is empty.

  2. 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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

SQL Server Query Performance Tuning

Authors: Sajal Dam, Grant Fritchey

4th Edition

1430267429, 9781430267423

More Books

Students also viewed these Databases questions

Question

Prepare a short profile of Henry words worth Longfellow?

Answered: 1 week ago

Question

What is RAM as far as telecommunication is concerned?

Answered: 1 week ago

Question

Question 1: What is reproductive system? Question 2: What is Semen?

Answered: 1 week ago

Question

Describe the sources of long term financing.

Answered: 1 week ago

Question

What is the most important part of any HCM Project Map and why?

Answered: 1 week ago