I have a python method that takes a list of tuples of the form (string, float) and returns a list of strings that, if combined, would not exceed a certain limit. I am not splitting sentences to preserve the output length, but making sure to stay within a sentence length from the desired output length.
For example:
s: [('Where are you',1),('What about the next day',2),('When is the next event',3)]
max_length : 5
output : 'Where are you What about the next day'
max_length : 3
output: 'Where are you'
This is what I am doing:
l=0
output = []
for s in s_tuples:
if l <= max_length:
output.append(s[0])
l+=len(get_words_from(s[0]))
return ''.join(output)
Is there a smarter way to make sure the output word length does not exceed max_length other than stopping when the length is reached?