Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

partitions(0) does not return empty set #79

Open
jessebett opened this issue Jul 11, 2019 · 3 comments · May be fixed by #82
Open

partitions(0) does not return empty set #79

jessebett opened this issue Jul 11, 2019 · 3 comments · May be fixed by #82

Comments

@jessebett
Copy link

Currently

julia> partitions(0)|>collect
1-element Array{Any,1}:
 #undef

This is unexpected because the partition of the empty set is the empty set so I would expect

patitions(0)|>collect == [[]]

This results in unexpected behviour here:

julia> [length(b) for b in partitions(1)]
1-element Array{Int64,1}:
 1

julia> [length(b) for b in partitions(0)]
1-element Array{Int64,1}:
 4492284592

# I expect this
julia> [length(b) for b in [[]]]
1-element Array{Int64,1}:
 0
@jessebett
Copy link
Author

jessebett commented Jul 11, 2019

I don’t know enough about defining Iterators to solve this 😞

I believe that the bug is

function Base.iterate(p::IntegerPartitions, xs = Int[])
    length(xs) == p.n && return
    xs = nextpartition(p.n,xs)
    (xs, xs)
end

since length(xs)==0 this returns nothing. I don't know how to have iterate here return [[]] only once.

@mschauer
Copy link
Member

mschauer commented Jul 11, 2019

This does the trick, needs some test etc.

function Base.iterate(p::IntegerPartitions, xs = Int[])
    length(xs) >= p.n + (p.n == 0) && return nothing
    xs = nextpartition(p.n,xs)
    (xs, xs)
end

Ah, no, this returns [0] which is not wrong but not "normalized"

@simonschoelly
Copy link

Then maybe add this as a second function:

function Base.iterate(p::IntegerPartitions)
   p.n == 0 && return (Int[], Int[])
  return iterate(p, Int[])
end

and change the signature of the other function from function Base.iterate(p::IntegerPartitions, xs = Int[]) to function Base.iterate(p::IntegerPartitions, xs).

@jessebett jessebett linked a pull request Jul 27, 2019 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants