Strings with given frequency
Is there a sage function that gives me all vectors/strings/lists/tuples of integers with given "absolute frequency"? i.e.
dwim([2, 1]) == ["001", "010", "100"]
Is there a sage function that gives me all vectors/strings/lists/tuples of integers with given "absolute frequency"? i.e.
dwim([2, 1]) == ["001", "010", "100"]
What you want is probably Permutations
sage: P = Permutations([0]*2+[1])
sage: P.list()
[[0, 0, 1], [0, 1, 0], [1, 0, 0]]
Not bad ! So, you can combine all this to get the dwim function: sage: dwim = lambda L : [ str(Word(w)) for w in Permutations(flatten([[i]*j for (i,j) in enumerate(L)]))] sage: dwim([2, 1]) ['001', '010', '100']
You can input strings directly :) sage: P = Permutations(['0']*2+['1']) sage: map(join, P) ['0 0 1', '0 1 0', '1 0 0'] sage: map(lambda x: ''.join(x), P) ['001', '010', '100']
Asked: 11 years ago
Seen: 840 times
Last updated: Feb 04 '14