The function receives a string containing a sentence. Its goal is to shorten every word in it to a length of 4 characters until the string is shorter than 38 characters and return.
The 38 characters max length is mandatory and must be reached with the least amount of deletion possible.
Here is what I've done:
# reduces each word to a lenght of 4
def shorten_words(string):
i = 0
j = 0
while(len(string) > 38 and i < len(string)):
array = list(string)
if(array[i] != ' ' and array[i] != '-'):
j += 1
else:
j = 0
if(j > 4):
array[i] = ''
i -= 1
i += 1
string = ''.join(array)
return(string)
I feel like the list/join method is inefficient and would like to know how I could improve this function