Skip to content

Instantly share code, notes, and snippets.

@renzon
Last active July 14, 2017 18:38
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 renzon/c581d498ab4ccea192f36429d4836cde to your computer and use it in GitHub Desktop.
Save renzon/c581d498ab4ccea192f36429d4836cde to your computer and use it in GitHub Desktop.
from collections import Counter, OrderedDict
def all_perms_r(curr, freqs):
if all(value==0 for value in freqs.values()):
yield curr
for ch, freq in freqs.items():
if freq == 0:
continue
freqs[ch] -= 1
yield from all_perms_r(curr + ch, freqs)
freqs[ch] += 1
def all_perms_rep(a):
freqs = OrderedDict(sorted(Counter(a)))
yield from all_perms_r("", freqs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment