Ask Your Question

# 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"]

edit retag close merge delete

## 1 answer

Sort by ยป oldest newest most voted

What you want is probably Permutations

sage: P = Permutations([0]*2+[1])
sage: P.list()
[[0, 0, 1], [0, 1, 0], [1, 0, 0]]

more

## Comments

1

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']

( 2014-02-04 13:10:13 -0600 )edit

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']

( 2014-02-04 13:49:44 -0600 )edit

## Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

## Stats

Asked: 2014-02-04 10:52:06 -0600

Seen: 103 times

Last updated: Feb 04 '14