Skip to content

Commit ddcd9c3

Browse files
committed
More tests
1 parent 7cad161 commit ddcd9c3

2 files changed

Lines changed: 76 additions & 6 deletions

File tree

mathics/packages/DiscreteMath/CombinatoricaLite.m

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,9 +333,35 @@
333333

334334

335335
(****************************************************************************
336-
*** Combinatorica 0.6 versions until we support more modern WL features *****
336+
*** Combinatorica 0.9 versions until we support more modern WL features *****
337337
*****************************************************************************)
338338

339+
Backtrack::usage = "Backtrack[s,partialQ,solutionQ] performs a backtrack search of the state space s, expanding a partial solution so long as partialQ is True and returning the first complete solution, as identified by solutionQ."
340+
341+
Backtrack[space_List,partialQ_,solutionQ_,flag_:One] :=
342+
Module[{n=Length[space],all={},done,index,v=2,solution},
343+
index=Prepend[ Table[0,{n-1}],1];
344+
While[v > 0,
345+
done = False;
346+
While[!done && (index[[v]] < Length[space[[v]]]),
347+
index[[v]]++;
348+
done = Apply[partialQ,{Solution[space,index,v]}];
349+
];
350+
If [done, v++, index[[v--]]=0 ];
351+
If [v > n,
352+
solution = Solution[space,index,n];
353+
If [Apply[solutionQ,{solution}],
354+
If [SameQ[flag,All],
355+
AppendTo[all,solution],
356+
all = solution; v=0
357+
]
358+
];
359+
v--
360+
]
361+
];
362+
all
363+
]
364+
339365
(* Note: Until we support With[], this is the Combinatorica 0.6 version of BinarySearch *)
340366
BinarySearch::usage = "BinarySearch[l,k,f] searches sorted list l for key k and returns the the position of l containing k, with f a function which extracts the key from an element of l."
341367
BinarySearch[l_List,k_Integer] := BinarySearch[l,k,1,Length[l],Identity]
@@ -351,6 +377,24 @@
351377
]
352378
]
353379

380+
DistinctPermutations::usage = "DistinctPermutations[l] returns all permutations of the multiset described by list l."
381+
382+
DistinctPermutations[s_List] :=
383+
Module[{freq,alph=Union[s],n=Length[s]},
384+
freq = Map[ (Count[s,#])&, alph];
385+
Map[
386+
(alph[[#]])&,
387+
Backtrack[
388+
Table[Range[Length[alph]],{n}],
389+
(Count[#,Last[#]] <= freq[[Last[#]]])&,
390+
(Count[#,Last[#]] <= freq[[Last[#]]])&,
391+
All
392+
]
393+
]
394+
]
395+
396+
397+
354398
KSubsets::usage = "KSubsets[l, k] gives all subsets of set l containing exactly k elements, ordered lexicographically."
355399
KSubsets[l_List,0] := { {} }
356400
KSubsets[l_List,1] := Partition[l,1]
@@ -397,6 +441,10 @@
397441
]
398442
RandomPermutation[n_Integer?Positive] := RandomPermutation1[n]
399443

444+
Solution[space_List,index_List,count_Integer] :=
445+
Module[{i}, Table[space[[ i,index[[i]] ]], {i,count}] ]
446+
447+
400448
(* Tableaux stuff not working. Hitting recursion limit....
401449
TransposeTableau::usage = "TransposeTableau[t] reflects a Young tableau t along the main diagonal, creating a different tableau."
402450
TransposeTableau[tb_List] :=

test/test_combinatorica.py

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@ def test_combinatorica():
1515
"""
1616
)
1717

18+
# A number of examples from Implementing Discrete Mathematics by
19+
# Steven Skiena and
1820
# A number of examples from Computation Discrete Mathematics by
19-
# Sriram Pemmaraju and Steven Skiena
21+
# Sriram Pemmaraju and Steven Skiena.
22+
23+
# Page numbers below come from the first book
24+
2025

2126
# Permutation[3] doesn't work
2227
permutations3 = (
@@ -48,7 +53,7 @@ def test_combinatorica():
4853
"{c, b, d, a}, {c, d, a, b}, {c, d, b, a}, "
4954
"{d, a, b, c}, {d, a, c, b}, {d, b, a, c}, "
5055
"{d, b, c, a}, {d, c, a, b}, {d, c, b, a}}",
51-
"LexicographicPermuations"
56+
"LexicographicPermuations, Page 4"
5257
),
5358

5459
("Map[RankPermutation, Permutations[Range[4]]]",
@@ -58,11 +63,11 @@ def test_combinatorica():
5863

5964
("RandomPermutation1[20] === RandomPermutation2[20]",
6065
"False",
61-
"Not likey two of 20! permutations will be the same (different routines)"
66+
"Not likey two of 20! permutations will be the same (different routines), Page 7"
6267
),
6368
("RandomPermutation1[20] === RandomPermutation1[20]",
6469
"False",
65-
"Not likey two of 20! permutations will be the same (same routine)"
70+
"Not likley two of 20! permutations will be the same (same routine)"
6671
),
6772
("RankPermutation[{8, 9, 7, 1, 6, 4, 5, 3, 2}]", "321953", "RankPermutation"),
6873
(
@@ -73,7 +78,24 @@ def test_combinatorica():
7378
(
7479
"MinimumChangePermutations[{a,b,c}]",
7580
"{{a, b, c}, {b, a, c}, {c, a, b}, {a, c, b}, {b, c, a}, {c, b, a}}",
76-
"MinimumChangePermuations",
81+
"MinimumChangePermuations, Page 11",
82+
),
83+
(
84+
"Union[Permutations[{a,a,a,a,a}]]",
85+
"{{a, a, a, a, a}}",
86+
"simple but wasteful Permutation duplication elimination, Page 12"
87+
),
88+
(
89+
"DistinctPermutations[{1,1,2,2}]",
90+
"{{1, 1, 2, 2}, {1, 2, 1, 2}, {1, 2, 2, 1}, "
91+
"{2, 1, 1, 2}, {2, 1, 2, 1}, {2, 2, 1, 1}}",
92+
"DisctinctPermutations of multiset Binomial[6,3] permutations, Page 14"
93+
),
94+
(
95+
"Multinomial[3,3]",
96+
"20",
97+
98+
"The built-in function Multinomial, Page 14"
7799
),
78100
(
79101
"Subsets[Range[3]]",

0 commit comments

Comments
 (0)