Gini Impurity of a Node
Ashby Mutual is building a rule-book that sorts incoming claims into outcomes. At any point in that rule-book you are looking at one group of past claims — every claim that reached the same place by following the same rules — and each claim in the group ended in one of a fixed set of outcomes. Before deciding whether a group is worth breaking up further, the modellers score how mixed it is. Their measure is Gini impurity: zero when every claim in the group ended the same way, and larger the more evenly the claims are spread across the outcomes.
Task: Print the Gini impurity of the group.
Input
Two lines. The first holds n, the number of possible outcomes. The second holds n whole numbers separated by single spaces — how many claims in this group ended in each outcome. A count may be zero, and at least one of them is not.
Output
One line holding the Gini impurity, written with exactly 4 digits after the decimal point.
Example:
Input:
2
40 10
Output:
0.3200
Sign in to solve this problem
Reading problems is free for everyone — solving them (Run, Submit, and tracking what you've solved) needs an account.
Sign in