Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
One more "first" solution solution in Clear category for Cut Sentence by Kolia951
import re
def cut_sentence(line: str, length: int) -> str:
if length >= len(line):
return line
elif length < len(line.split()[0]):
return "..."
else:
res = re.search(rf".{{0,{length}}}[^a-zA-Z]", line)[0]
final_symb = line[length]
ellipsis = "..." if length < len(line) else ""
return res.strip() + ellipsis
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
Comments: