Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Uncategorized category for Cut Sentence by Kurush
def cut_sentence(line, length):
truncated = ""
for word in line.split():
if truncated == "":
if len(word) <= length: truncated += word
else:
truncated = "..."
break
else:
if len(truncated + " " + word) <= length: truncated += " " + word
else:
truncated += "..."
break
return truncated
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!')
May 6, 2021