Last active
January 31, 2021 13:19
-
-
Save ahmed4end/a4f5e72de1274a971e11a56060ad4906 to your computer and use it in GitHub Desktop.
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
#Sol 1 - performance: Exp | |
vowels = { 'A': 1, 'E': 21, 'I': 7, 'O': 8, 'U': 5, 'T':4 } | |
result = [] | |
def backtrack(trac=[], totalWeight=10, vowelsRepeatLimit={'A':5}): | |
''' | |
:params: | |
:totalWeight: total weight target. | |
:vowelsRepeatLimit: letter count limit per single iteration. | |
''' | |
if totalWeight==0: | |
return True | |
elif totalWeight<0: | |
return False | |
for v in vowels: | |
callback = backtrack(trac=trac+[v], totalWeight=totalWeight-vowels[v]) | |
passRepeatLimit = all([(trac+[v]).count(v[0])<v[1] for v in vowelsRepeatLimit.items()]) | |
if callback and passRepeatLimit: | |
result.append(trac+[v]) | |
backtrack() | |
if __name__ == "__main__": | |
print(*result, sep='\n') | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment