{}
CODE VISUALIZER
Master DSA, Python and C with step-by-step code visualization.
See it in action
CODE VISUALIZER
Master DSA, Python and C with step-by-step code visualization.
See it in action
run-icon
main.py
# Online Python compiler (interpreter) to run Python online. # Write Python 3 code in this online editor and run it. print("Try programiz.pro") def split_into_buckets(words,nVar): separateWords = words.split(" ") finalAnswer = [] while separateWords: if len(separateWords[0]) <= nVar: temporaryAnswer = [] temporaryAnswer.append(separateWords[0]) separateWords.pop(0) joined = " ".join(temporaryAnswer) while separateWords and (len(joined) + len(separateWords[0]) + 1 <= nVar): temporaryAnswer.append(separateWords[0]) separateWords.pop(0) joined = " ".join(temporaryAnswer) finalAnswer.append(joined) else: return [] return finalAnswer print(split_into_buckets("she sells sea shells by the sea", 10)) # output = ["she sells", "sea shells", "by the sea"] print(split_into_buckets("the mouse jumped over the cheese", 7)) # output = ["the", "mouse", "jumped", "over", "the", "cheese"] print(split_into_buckets("fairy dust coated the air", 20)) # output = ["fairy dust coated", "the air"] print(split_into_buckets("a b c d e", 2)) # output = ["a", "b", "c", "d", "e"] print(split_into_buckets("aaaaaa",1)) # output = []
Output