Skip to content

Commit 91ad2c7

Browse files
committed
Bang on Combinatorica
1 parent 114f0e6 commit 91ad2c7

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

mathics/packages/DiscreteMath/CombinatoricaLite.m

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
LexicographicPermutations[0] := {{}}
5959
LexicographicPermutations[1] := {{1}}
6060

61+
(*
6162
LexicographicPermutations[n_Integer?Positive] := LP[n]
6263
LexicographicPermutations[l_List] := Permute[l, LexicographicPermutations[Length[l]] ]
6364
LP[{{n, _Integer}}] :=
@@ -69,6 +70,7 @@
6970
l, n!-1
7071
]
7172
]
73+
*)
7274

7375
MinimumChangePermutations::usage = "MinimumChangePermutations[l] constructs all permutations of list l such that adjacent permutations differ by only one transposition."
7476
MinimumChangePermutations[l_List] := LexicographicPermutations[l] /; (Length[l] < 2)
@@ -361,6 +363,24 @@
361363
KSubsets[Rest[l],k]
362364
]
363365

366+
LexicographicPermutations[{a_,b_}] := {{a,b},{b,a}}
367+
368+
LexicographicPermutations[l_List] :=
369+
Block[{i,n=Length[l]},
370+
Apply[
371+
Join,
372+
Table[
373+
Map[
374+
(Prepend[#,l[[i]]])&,
375+
LexicographicPermutations[
376+
Complement[l,{l[[i]]}]
377+
]
378+
],
379+
{i,n}
380+
]
381+
]
382+
]
383+
364384

365385
(* Not working: always returns the same sorted value.
366386
Probably Sort[] below is buggy.

test/test_combinatorica.py

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,39 @@ def test_combinatorica():
2323
r"{{1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1}}"
2424
)
2525
for str_expr, str_expected, message in (
26+
(
27+
"Permute[{a, b, c, d}, Range[4]]",
28+
"{a, b, c, d}",
29+
"Permute list with simple list",
30+
),
31+
(
32+
"Permute[{a, b, c, d}, {1,2,2,4}]",
33+
"Permute[{a, b, c, d}, {1,2,2,4}]",
34+
"Incorrect permute: index 2 duplicated",
35+
),
2636
(
2737
"Permute[{A, B, C, D}, %s]" % permutations3,
2838
"{{A, B, C}, {A, C, B}, {B, A, C}, {B, C, A}, {C, A, B}, {C, B, A}}",
2939
"Permute",
3040
),
41+
(
42+
"LexicographicPermutations[{a,b,c,d}]",
43+
"{{a, b, c, d}, {a, b, d, c}, {a, c, b, d}, "
44+
"{a, c, d, b}, {a, d, b, c}, {a, d, c, b}, "
45+
"{b, a, c, d}, {b, a, d, c}, {b, c, a, d}, "
46+
"{b, c, d, a}, {b, d, a, c}, {b, d, c, a}, "
47+
"{c, a, b, d}, {c, a, d, b}, {c, b, a, d}, "
48+
"{c, b, d, a}, {c, d, a, b}, {c, d, b, a}, "
49+
"{d, a, b, c}, {d, a, c, b}, {d, b, a, c}, "
50+
"{d, b, c, a}, {d, c, a, b}, {d, c, b, a}}",
51+
"LexicographicPermuations"
52+
),
53+
54+
("Map[RankPermutation, Permutations[Range[4]]]",
55+
"Range[0, 23]",
56+
"Permutations uses lexographic order"
57+
),
58+
3159
("RankPermutation[{8, 9, 7, 1, 6, 4, 5, 3, 2}]", "321953", "RankPermutation"),
3260
(
3361
"Permute[{5,2,4,3,1}, InversePermutation[{5,2,4,3,1}]]",
@@ -40,7 +68,7 @@ def test_combinatorica():
4068
"MinimumChangePermuations",
4169
),
4270
(
43-
"Subsets[{1,2,3}]",
71+
"Subsets[Range[3]]",
4472
"{{}, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}}",
4573
"Subsets",
4674
),
@@ -71,7 +99,7 @@ def test_combinatorica():
7199
"BinarySearch - find where key is a list",
72100
),
73101
(
74-
"KSubsets[{1,2,3,4,5},3]",
102+
"KSubsets[Range[5], 3]",
75103
"{{1, 2, 3}, {1, 2, 4}, {1, 2, 5}, {1, 3, 4}, {1, 3, 5}, {1, 4, 5}, "
76104
"{2, 3, 4}, {2, 3, 5}, {2, 4, 5}, {3, 4, 5}}",
77105
"Ksubsets",

0 commit comments

Comments
 (0)