Pregunta de entrevista de Google

Given a long string, and a width, parse the string return an array list containing string that shorter or equal to the given width; if multiple spaces left, split them equally

Respuesta de la entrevista

Anónimo

10 de jun de 2015

What does "if multiple spaces left, split them equally" mean? if I ignore this part of the question, the solution will be: #Python import math def splitStr(s, width): len_lst = math.ceil(len(s)/width) lst = [0]*len_lst idx = 0 for i in range(len_lst-1): lst[i] = s[idx:idx+width] #store idx to idx+width-1 idx += width lst[-1] = s[idx:] # just use whatever is left for the last one return lst ###*********************** s = 'hey how are you' print(splitStr(s,2))