Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Cut Sentence solution in Uncategorized category for Cut Sentence by Aleksandra_Niewiadomska
def cut_sentence(line: str, length: int) -> str:
words = line.split()
while words:
res = ' '.join(words)
if len(res) <= length:
return res + '...' if res != line else res
else:
words.pop()
return '...'
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!")
Feb. 1, 2023