Skip to content

Instantly share code, notes, and snippets.

@ahmed4end
Last active January 31, 2021 13:19
Show Gist options
  • Save ahmed4end/a4f5e72de1274a971e11a56060ad4906 to your computer and use it in GitHub Desktop.
Save ahmed4end/a4f5e72de1274a971e11a56060ad4906 to your computer and use it in GitHub Desktop.
#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