Skip to content

Instantly share code, notes, and snippets.

@tuannvm
Last active July 5, 2023 11:01
Show Gist options
  • Save tuannvm/e3568aaea460bad8f18fc7f3f13ce1ab to your computer and use it in GitHub Desktop.
Save tuannvm/e3568aaea460bad8f18fc7f3f13ce1ab to your computer and use it in GitHub Desktop.

You've launched a revolutionary service not long ago, and were busy improving it for the last couple of months. When you finally decided that the service is perfect, you remembered that you created a feedbacks page long time ago, which you never checked out since then. Now that you have nothing left to do, you would like to have a look at what the community thinks of your service.

Unfortunately it looks like the feedbacks page is far from perfect: each feedback is displayed as a one-line string, and if it's too long there's no way to see what it is about. Naturally, this horrible bug should be fixed. Implement a function that, given a feedback and the size of the screen, splits the feedback into lines so that:

each token (i.e. sequence of non-whitespace characters) belongs to one of the lines entirely; each line is at most size characters long; no line has trailing or leading spaces; each line should have the maximum possible length, assuming that all lines before it were also the longest possible. Example

For feedback = "This is an example feedback", and size = 8, the output should be

feedbackReview(feedback, size) = ["This is", 
                                  "an", 
                                  "example", 
                                  "feedback"]

Input/Output

[time limit] 4000ms (py)
[input] string feedback

A string containing a feedback. Each feedback is guaranteed to contain only letters, punctuation marks and whitespace characters (' ').

Constraints:

0 ≤ feedback.length ≤ 100.

[input] integer size

The size of the screen. It is guaranteed that it is not smaller than the longest token in the feedback.

Constraints:

1 ≤ size ≤ 100.

[output] array.string

Lines from the feedback, split as described above.

@jaypatel-13
Copy link

jaypatel-13 commented Dec 21, 2021

import textwrap

def solution(feedback, size):
    return textwrap.wrap(feedback,size)

@kunzhangweb
Copy link

kunzhangweb commented Mar 31, 2023

The following code snippet doesn't yield the identical result, but I hope it can provide another way to solve the problem.
def solution(feedback, size):
return '\n'.join( [feedback[i : i+size].strip() for i in range(0, len(feedback), size)] )

@sebinsua
Copy link

sebinsua commented Jul 5, 2023

In the CodeSignal question you're only meant to use a single import, so I presume that @jaypatel-13's answer is correct, however, if this wasn't the case you could write code like this:

from functools import reduce

def solution(feedback: str, size: int):
    return reduce(
        lambda acc, word:
            acc[0:-1] + [f"{acc[-1]} {word}" if acc[-1] else word]
            if len(acc) > 0 and len(f"{acc[-1]} {word}") <= size
            else acc + ([word] if word else []),
        feedback.split(' '),
        []
    )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment