Interview Query
Back to Data Science Interview
Data Science Interview

Data Science Interview

92 of 257 Completed

Question
Question
Question
Question
Question
Question

Max Width
Go to question details page

Given an array of words and a max_width parameter, write a function justify to format the text such that each line has exactly max_width characters. Pad extra spaces ’ ‘ when necessary so that each line has exactly max_width characters.

Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line does not divide evenly between words, place excess spaces on the right-hand side of each line.

Note: You may assume that there is no word in words that is longer than max_width.

Example:

Input:

words = ["This", "is", "an", "example", "of", "text", "justification."]
max_width = 16

def justify(words, max_width): ->
[
   "This    is    an",
   "example  of text",
   "justification.  "
]
Good job, keep it up!

35%

Completed

You have 165 sections remaining on this learning path.

Loading pricing options.