Coding

Write code to implement a text wrapper that takes in a long string and a character limit as inputs and returns a list of strings, where each string is limited to the specified number of characters, split only at spaces.

Machine Learning Engineer

Flexport

Uber

Reddit

Bloomberg

Symantec

Lyft

Did you come across this question in an interview?

Answers

Anonymous

5 months ago
4.6Exceptional
def text_wrapper(text, char_limit):
    # Initialize a list to hold the wrapped text
    wrapped_text = []
    
    # Split the original text into words
    words = text.split()
    
    # Initialize a temporary string to build the current line
    current_line = ""
    
    for word in words:
        # Check if adding the next word exceeds the character limit
        if len(current_line) + len(word) + 1 > char_limit:
            # If it does, append the current line to the list and start a new line
            wrapped_text.append(current_line.strip())
            current_line = word  # Start a new line with the current word
        else:
            # Otherwise, add the word to the current line
            current_line += " " + word
    
    # Append any remaining text as the last line
    if current_line:
        wrapped_text.append(current_line.strip())
    
    return wrapped_text

# Example usage
long_string = "This is an example of a long string that needs to be wrapped according to a specified character limit without breaking any words."
char_limit = 30
wrapped_result = text_wrapper(long_string, char_limit)

# Print the wrapped result
for line in wrapped_result:
    print(f'"{line}"')

  • Write code to implement a text wrapper that takes in a long string and a character limit as inputs and returns a list of strings, where each string is limited to the specified number of characters, split only at spaces.
  • How would you code a text-wrapping function that respects space-delimited words within a character limit?
  • Can you develop a text wrapper function that segments a string based on a character count, without breaking words?
  • How would you write a function that wraps text into lines of a certain length, breaking only at spaces?
  • What would be your approach to implementing a function that wraps text to a specified character length, ensuring it splits at spaces?
  • Could you create a code for a text wrapper that adheres to a given character limit without cutting words in half?
  • How do you plan to code a text wrapping algorithm that breaks lines by space within a character limit?
  • Show how to write a function that neatly wraps text, considering space boundaries and a character threshold.
  • How would you program a text wrapper that divides a string into a list of space-separated strings, each within a character limit?
  • Can you construct a text-wrapping function that breaks a long string into space-separated lines under a character cap?
  • Write code to implement a text wrapper that takes in a long string and a character limit as inputs and returns a list of strings, where each string is limited to the specified number of characters, split only at spaces. For example, if the input string is "This is a long sentence that needs to be wrapped", and the character limit is 10, the output should be a list with three strings: ["This is a", "long", "sentence that", "needs to be", "wrapped"].

Interview question asked to Machine Learning Engineers interviewing at Mozilla, Bloomberg, Uber and others: Write code to implement a text wrapper that takes in a long string and a character limit as inputs and returns a list of strings, where each string is limited to the specified number of characters, split only at spaces..