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

A doubt about question No.74 #136

Closed
madeirak opened this issue Dec 29, 2020 · 6 comments
Closed

A doubt about question No.74 #136

madeirak opened this issue Dec 29, 2020 · 6 comments

Comments

@madeirak
Copy link
Contributor

  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.

@rougier
Copy link
Owner

rougier commented Jan 4, 2021

Do you have an example where this fails?

@madeirak
Copy link
Contributor Author

madeirak commented Jan 4, 2021

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.

@rougier
Copy link
Owner

rougier commented Jan 4, 2021

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?

@madeirak
Copy link
Contributor Author

madeirak commented Jan 4, 2021

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 :)

@madeirak
Copy link
Contributor Author

madeirak commented Jan 4, 2021

#138

@rougier
Copy link
Owner

rougier commented Jan 5, 2021

Thanks, merged.

@rougier rougier closed this as completed Jan 5, 2021
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

No branches or pull requests

2 participants