rougier/numpy-100

A doubt about question No.74

madeirak opened this issue · 6 comments

  1. Given an array C that is a bincount, how to produce an array A such that np.bincount(A) == C? (★★★)

The given answer is shown as below.
C = np.bincount([1,1,2,3,4,4,6]) A = np.repeat(np.arange(len(C)), C) print(A)

There maybe a problem with the description of question No.74,the answer given can only solve the problem of Non-strictly increasing 1-d array not for all kind of 1-d array.

Do you have an example where this fails?

Do you have an example where this fails?

  1. problem
    The example in given answer is created by direct assignment,not by common means like np.random.randint(0,10,7)
    The correct example must be a Non-strictly increasing 1-d array like the answer given [1,1,2,3,4,4,6],whose the next adjacent element is larger than the previous one.

A counterexample is like [2,1,1,1,1,1,1]

C = np.bincount([2,1,1,1,1,1,1,1])
A = np.repeat(np.arange(len(C)), C)
#print C and A
#C= [0 7 1]
#A= [1 1 1 1 1 1 1 2]

We can see [2,1,1,1,1,1,1,1] is not equal to [1,1,1,1,1,1,1,2],we get the wrong answer.
The key point is np.repeat() is sequential.

  1. Possible solutions
    Adding the notion of non-strictly increasing 1-d array in the title description of No.74 is the most convenient way.

Thanks for the ifnromation. We could simply add sorted in C description: "Given a sorted array C that corresponds to a bincount". Can you make a PR?

Thanks for the ifnromation. We could simply add sorted in C description: "Given a sorted array C that corresponds to a bincount". Can you make a PR?

It`s my pleasure :)

Thanks, merged.