ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Tue, 04 Feb 2014 20:49:44 +0100Strings with given frequencyhttps://ask.sagemath.org/question/10989/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"]Tue, 04 Feb 2014 17:52:06 +0100https://ask.sagemath.org/question/10989/strings-with-given-frequency/Answer by ppurka for <p>Is there a sage function that gives me all vectors/strings/lists/tuples of integers with given "absolute frequency"? i.e.</p>
<p>dwim([2, 1]) == ["001", "010", "100"]</p>
https://ask.sagemath.org/question/10989/strings-with-given-frequency/?answer=16007#post-id-16007What you want is probably `Permutations`
sage: P = Permutations([0]*2+[1])
sage: P.list()
[[0, 0, 1], [0, 1, 0], [1, 0, 0]]Tue, 04 Feb 2014 19:53:54 +0100https://ask.sagemath.org/question/10989/strings-with-given-frequency/?answer=16007#post-id-16007Comment by ppurka for <p>What you want is probably <code>Permutations</code></p>
<pre><code>sage: P = Permutations([0]*2+[1])
sage: P.list()
[[0, 0, 1], [0, 1, 0], [1, 0, 0]]
</code></pre>
https://ask.sagemath.org/question/10989/strings-with-given-frequency/?comment=16333#post-id-16333You 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']
Tue, 04 Feb 2014 20:49:44 +0100https://ask.sagemath.org/question/10989/strings-with-given-frequency/?comment=16333#post-id-16333Comment by tmonteil for <p>What you want is probably <code>Permutations</code></p>
<pre><code>sage: P = Permutations([0]*2+[1])
sage: P.list()
[[0, 0, 1], [0, 1, 0], [1, 0, 0]]
</code></pre>
https://ask.sagemath.org/question/10989/strings-with-given-frequency/?comment=16334#post-id-16334Not 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']
Tue, 04 Feb 2014 20:10:13 +0100https://ask.sagemath.org/question/10989/strings-with-given-frequency/?comment=16334#post-id-16334