Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Cut Sentence by Pavellver
def cut_sentence(line: str, length: int) -> str:
array = line.split()
if length < len(line):
uncut_line = f'{line[:length].rstrip()}'
if uncut_line.split()[-1] in array:
return f'{line[:length].rstrip()}...'
else:
return f'{line[:length - len(uncut_line.split()[-1])].rstrip()}...'
else:
return line
print("Example:")
print(cut_sentence("Hi my name is Alex", 4))
# These "asserts" are used for self-checking
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!")
Jan. 20, 2023
Comments: