Skip to content

Instantly share code, notes, and snippets.

@cfjedimaster
Created November 16, 2022 14:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cfjedimaster/4b57bb47d523c2e52c5179e187a375cc to your computer and use it in GitHub Desktop.
Save cfjedimaster/4b57bb47d523c2e52c5179e187a375cc to your computer and use it in GitHub Desktop.
def combineString(input, size):
result = []
marker = 0
tempStr = ""
while(marker < len(input)):
if(tempStr == ""):
tempStr = tempStr + input[marker]
else:
tempStr = tempStr + " " + input[marker]
if marker < len(input)-1:
nextSize = 1 + len(input[marker+1])
else:
nextSize = 0
if (len(tempStr) + nextSize) > size:
result.append(tempStr)
tempStr = ""
elif marker == len(input)-1:
result.append(tempStr)
marker = marker + 1
return result
input1 = ["a","b","c","d","e","f","g"]
print(combineString(input1, 5))
print(combineString(input1, 12))
input2 = ["alpha", "beta", "gamma", "delta", "epsilon"]
print(combineString(input2, 20))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment