Permutations by swapping: Difference between revisions
Content added Content deleted
(Added Wren) |
Alextretyak (talk | contribs) (Added 11l) |
||
Line 24: | Line 24: | ||
* [[Gray code]] |
* [[Gray code]] |
||
<br><br> |
<br><br> |
||
=={{header|11l}}== |
|||
{{trans|Python: Iterative version of the recursive}} |
|||
<lang 11l>F s_permutations(seq) |
|||
V items = [[Int]()] |
|||
L(j) seq |
|||
[[Int]] new_items |
|||
L(item) items |
|||
V i = L.index |
|||
I i % 2 |
|||
new_items [+]= (0..item.len).map(i -> @item[0 .< i] [+] [@j] [+] @item[i ..]) |
|||
E |
|||
new_items [+]= (item.len..0).step(-1).map(i -> @item[0 .< i] [+] [@j] [+] @item[i ..]) |
|||
items = new_items |
|||
R enumerate(items).map((i, item) -> (item, I i % 2 {-1} E 1)) |
|||
L(n) (3, 4) |
|||
print(‘Permutations and sign of #. items’.format(n)) |
|||
L(perm, sgn) s_permutations(Array(0 .< n)) |
|||
print(‘Perm: #. Sign: #2’.format(perm, sgn)) |
|||
print()</lang> |
|||
{{out}} |
|||
<pre> |
|||
Permutations and sign of 3 items |
|||
Perm: [0, 1, 2] Sign: 1 |
|||
Perm: [0, 2, 1] Sign: -1 |
|||
Perm: [2, 0, 1] Sign: 1 |
|||
Perm: [2, 1, 0] Sign: -1 |
|||
Perm: [1, 2, 0] Sign: 1 |
|||
Perm: [1, 0, 2] Sign: -1 |
|||
Permutations and sign of 4 items |
|||
Perm: [0, 1, 2, 3] Sign: 1 |
|||
Perm: [0, 1, 3, 2] Sign: -1 |
|||
Perm: [0, 3, 1, 2] Sign: 1 |
|||
Perm: [3, 0, 1, 2] Sign: -1 |
|||
Perm: [3, 0, 2, 1] Sign: 1 |
|||
Perm: [0, 3, 2, 1] Sign: -1 |
|||
Perm: [0, 2, 3, 1] Sign: 1 |
|||
Perm: [0, 2, 1, 3] Sign: -1 |
|||
Perm: [2, 0, 1, 3] Sign: 1 |
|||
Perm: [2, 0, 3, 1] Sign: -1 |
|||
Perm: [2, 3, 0, 1] Sign: 1 |
|||
Perm: [3, 2, 0, 1] Sign: -1 |
|||
Perm: [3, 2, 1, 0] Sign: 1 |
|||
Perm: [2, 3, 1, 0] Sign: -1 |
|||
Perm: [2, 1, 3, 0] Sign: 1 |
|||
Perm: [2, 1, 0, 3] Sign: -1 |
|||
Perm: [1, 2, 0, 3] Sign: 1 |
|||
Perm: [1, 2, 3, 0] Sign: -1 |
|||
Perm: [1, 3, 2, 0] Sign: 1 |
|||
Perm: [3, 1, 2, 0] Sign: -1 |
|||
Perm: [3, 1, 0, 2] Sign: 1 |
|||
Perm: [1, 3, 0, 2] Sign: -1 |
|||
Perm: [1, 0, 3, 2] Sign: 1 |
|||
Perm: [1, 0, 2, 3] Sign: -1 |
|||
</pre> |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |