Ask Your Question
1

Strings with given frequency

asked 2014-02-04 10:52:06 -0500

anonymous user

Anonymous

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 flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2014-02-04 12:53:54 -0500

ppurka gravatar image

What you want is probably Permutations

sage: P = Permutations([0]*2+[1])
sage: P.list()
[[0, 0, 1], [0, 1, 0], [1, 0, 0]]
edit flag offensive delete link 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']

tmonteil gravatar imagetmonteil ( 2014-02-04 13:10:13 -0500 )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']

ppurka gravatar imageppurka ( 2014-02-04 13:49:44 -0500 )edit

Your Answer

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

Add Answer

Question Tools

Stats

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

Seen: 72 times

Last updated: Feb 04 '14