Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Speedy category for Cut Sentence by bespontoff
def cut_sentence(line, length):
'''
Cut a given sentence, so it becomes shorter than or equal to a given length.
'''
# your code here
words = line.split()
while words:
res = ' '.join(words)
if len(res) <= length:
return res + '...' if res != line else res
else:
words.pop()
return '...'
if __name__ == '__main__':
#These "asserts" using only for self-checking and not necessary for auto-testing
assert cut_sentence("Hi my name is Alex", 4) == "Hi...", "First"
assert cut_sentence("Hi my name is Alex", 8) == "Hi my...", "Second"
assert cut_sentence("Hi my name is Alex", 18) == "Hi my name is Alex", "Third"
assert cut_sentence("Hi my name is Alex", 20) == "Hi my name is Alex", "Fourth"
print('Done! Do you like it? Go Check it!')
June 11, 2019
Comments: