Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
lambda with re solution in Clear category for Cut Sentence by Max0526
cut_sentence = lambda string, length, s=__import__('re').search: s(r'.{,%s}\b' % length, string).group().rstrip()+'...'*(len(string) > length)
'''
A rewrite of the best creative solution by przemyslaw.daniel (below) as a lambda function one-liner.
def cut_sentence(string, length, s=__import__('re').search):
return s(r'.{,%s}\b' % length, string).group().rstrip()+'...'*(len(string) > length)
'''
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!")
May 1, 2023
Comments: