Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Line truncating solution in Clear category for Cut Sentence by Splitter
def cut_sentence(line, length):
if len(line) <= length:
return line
if (line[length] == ' '):
return line[:length] + '...'
res = line[:length]
while res and res[-1].isalpha():
res = res[:-1]
return res.strip() + '...'
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!')
April 14, 2020
Comments: