Ask Your Question
1

Strings with given frequency

asked 2014-02-04 17:52:06 +0100

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 19:53:54 +0100

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 20:10:13 +0100 )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 20:49:44 +0100 )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 17:52:06 +0100

Seen: 676 times

Last updated: Feb 04 '14