-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpermutations.sc
137 lines (118 loc) · 3.9 KB
/
permutations.sc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import com.sageserpent.americium.Trials.api
import com.sageserpent.americium.{RangeOfSlots, Trials}
import scala.collection.immutable.{SortedMap, SortedSet}
def permutationIndices3(size: Int): Trials[Vector[Int]] = {
require(0 <= size)
def permutationIndices(
exclusiveLimitOnVacantSlotIndex: Int,
previouslyChosenItemsAsBinaryTree: RangeOfSlots
): Trials[Vector[Int]] = if (0 == exclusiveLimitOnVacantSlotIndex)
api.only(Vector.empty)
else
for {
vacantSlotIndex <- api.integers(0, exclusiveLimitOnVacantSlotIndex - 1)
(filledSlot, chosenItemsAsBinaryTree) = previouslyChosenItemsAsBinaryTree
.fillVacantSlotAtIndex(vacantSlotIndex)
permutationTail <- permutationIndices(
exclusiveLimitOnVacantSlotIndex - 1,
chosenItemsAsBinaryTree
)
} yield filledSlot +: permutationTail
permutationIndices(size, RangeOfSlots.allSlotsAreVacant(size))
}
for {
numberOfIndices <- 0 to 4
combinationSize <- 0 to numberOfIndices
} {
api
.indexCombinations(numberOfIndices, combinationSize)
.withLimit(30)
.supplyTo(println)
println("+++++++++++++")
}
for {
numberOfIndices <- 0 to 4
permutationSize <- 0 to numberOfIndices
} {
api
.indexPermutations(numberOfIndices, permutationSize)
.withLimit(30)
.supplyTo(println)
println("------------")
}
def permutationIndices2(numberOfElements: Int): Trials[Seq[Int]] = {
def permutationIndices(
lowerBoundInclusive: Int,
upperBoundExclusive: Int
): Trials[Seq[Int]] = {
require(
lowerBoundInclusive <= upperBoundExclusive
) // Allow an empty range.
if (lowerBoundInclusive == upperBoundExclusive) api.only(Seq.empty)
else {
val partitions =
api.integers(lowerBoundInclusive, upperBoundExclusive - 1)
partitions.flatMap { partition =>
permutationIndices(lowerBoundInclusive, partition)
.flatMap(lowerSection =>
permutationIndices(1 + partition, upperBoundExclusive).flatMap(
upperSection =>
api.integers(1, 6).map {
case 1 => lowerSection ++ (partition +: upperSection)
case 2 => upperSection ++ (partition +: lowerSection)
case 3 => partition +: (lowerSection ++ upperSection)
case 4 => partition +: (upperSection ++ lowerSection)
case 5 => (lowerSection ++ upperSection) :+ partition
case 6 => (upperSection ++ lowerSection) :+ partition
}
)
)
}
}
}
permutationIndices(0, numberOfElements)
}
def permutationIndices(numberOfElements: Int): Trials[Seq[Int]] = {
require(0 <= numberOfElements)
numberOfElements match {
case 0 => api.only(Seq.empty)
case 1 => api.only(Seq(0))
case _ =>
api.integers(0, numberOfElements - 1).flatMap { partitionIndex =>
permutationIndices(numberOfElements - 1).map { indices =>
partitionIndex +: indices
.map(index => if (partitionIndex <= index) 1 + index else index)
}
}
}
}
val permutations: Trials[SortedMap[Int, Int]] =
api.only(1000).flatMap { size =>
val sourceCollection = 0 until size
api
.indexPermutations(size)
.map(indices => {
val permutation = SortedMap.from(indices.zip(sourceCollection))
assume(permutation.size == size)
assume(SortedSet.from(permutation.values).toSeq == sourceCollection)
permutation
})
}
try {
permutations
.withLimit(100)
.withComplexityLimit(1000)
.supplyTo { permuted =>
Trials.whenever(4 < permuted.size) {
permuted.values zip permuted.values.tail foreach { case (left, right) =>
if (left > right) {
println((left, right, permuted.values))
throw new RuntimeException
}
}
}
}
} catch {
case exception: permutations.TrialException =>
println(exception)
}