Skip to content

Commit da404fa

Browse files
committed
Rename and add tests for the partitions function
As there is already an integer_partitions function in Base which does basically the same thing, it is better to have this as an extra method of it.
1 parent 65bbe37 commit da404fa

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

src/Catalan.jl

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ module Catalan
22

33
using Polynomial
44

5+
import Base: integer_partitions
6+
57
export bell,
68
catalan,
79
derangement,
@@ -16,8 +18,7 @@ export bell,
1618
multinomial,
1719
primorial,
1820
stirlings1,
19-
subfactorial,
20-
partitions
21+
subfactorial
2122

2223
include("youngdiagrams.jl")
2324

@@ -152,14 +153,18 @@ function stirlings1(n::Integer, k::Integer)
152153
end
153154

154155
# Lists the partitions of the number n, the order is consistent with GAP
155-
function partitions(n)
156-
if n == 1
156+
function integer_partitions(n::Integer)
157+
if n < 0
158+
throw(DomainError())
159+
elseif n == 0
160+
return Vector{Int}[]
161+
elseif n == 1
157162
return Vector{Int}[[1]]
158163
end
159164

160165
list = Vector{Int}[]
161166

162-
for p in partitions(n-1)
167+
for p in integer_partitions(n-1)
163168
push!(list, [p, 1])
164169
if length(p) == 1 || p[end] < p[end-1]
165170
push!(list, [p[1:end-1], p[end]+1])
@@ -169,4 +174,4 @@ function partitions(n)
169174
list
170175
end
171176

172-
end
177+
end # module

test/Catalan_test.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,7 @@ using Base.Test
4545
@test bell(7) == 877
4646
@test bell(42) == BigInt("35742549198872617291353508656626642567")
4747
@test_fails bell(-1)
48+
49+
# integer_partitions
50+
@test integer_partitions(5) == {[1, 1, 1, 1, 1], [2, 1, 1, 1], [2, 2, 1], [3, 1, 1], [3, 2], [4, 1], [5]}
51+
@test_fails integer_partitions(-1)

0 commit comments

Comments
 (0)