Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Cut Sentence by lisovsky
def cut_sentence(line: str, length: int) -> str:
if len(line) <= length:
return line
i = line.rfind(" ", 0, length + 1)
return "..." if i == -1 else line[0:i] + "..."
print("Example:")
print(cut_sentence("Hi my name is Alex", 4))
assert cut_sentence("Hi my name is Alex", 8) == "Hi my..."
assert cut_sentence("Hi my name is Alex", 4) == "Hi..."
assert cut_sentence("Hi my name is Alex", 20) == "Hi my name is Alex"
assert cut_sentence("Hi my name is Alex", 18) == "Hi my name is Alex"
print("The mission is done! Click 'Check Solution' to earn rewards!")
Oct. 1, 2022
Comments: