Created
February 15, 2021 07:38
-
-
Save ashleighbasil/1edcd0e746c5a2df0d37c582ae7b4420 to your computer and use it in GitHub Desktop.
Solution to Cassidoo make sentence puzzle :D :D :D
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def make_sentence(string, words, current=[], final=[]): | |
# Base case | |
if string == '': | |
final.append(current) | |
for word in words: | |
if string[:len(word)] == word: | |
make_sentence( | |
string[len(word):], | |
words, | |
current + [word], | |
final | |
) | |
return [' '.join(s) for s in final] | |
string = "penpineapplepenapple" | |
words = ["apple", "pen", "applepen", "pine", "pineapple"] | |
print(make_sentence(string, words)) | |
# result: ['pen pine apple pen apple', 'pen pine applepen apple', 'pen pineapple pen apple'] | |
# :D :D :D |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Someone please talk to me <3