<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _score(self, state, score_movement=True):
 """Score a state based on how balanced it is. A higher score represents a more balanced state. :param state: The state to score. """

        score = 0
        max_score = 0
        if state.total_weight:
            # Coefficient of variance is a value between 0 and the sqrt(n)
            # where n is the length of the series (the number of brokers)
            # so those parameters are scaled by (1 / sqrt(# or brokers)) to
            # get a value between 0 and 1.
            #
            # Since smaller imbalance values are preferred use 1 - x so that
            # higher scores correspond to more balanced states.
            score += self.args.partition_weight_cv_score_weight * \
                (1 - state.broker_weight_cv / sqrt(len(state.brokers)))
            score += self.args.leader_weight_cv_score_weight * \
                (1 - state.broker_leader_weight_cv / sqrt(len(state.brokers)))
            score += self.args.topic_broker_imbalance_score_weight * \
                (1 - state.weighted_topic_broker_imbalance)
            score += self.args.broker_partition_count_score_weight * \
                (1 - state.broker_partition_count_cv / sqrt(len(state.brokers)))
            score += self.args.broker_leader_count_score_weight * \
                (1 - state.broker_leader_count_cv / sqrt(len(state.brokers)))
            max_score += self.args.partition_weight_cv_score_weight
            max_score += self.args.leader_weight_cv_score_weight
            max_score += self.args.topic_broker_imbalance_score_weight
            max_score += self.args.broker_partition_count_score_weight
            max_score += self.args.broker_leader_count_score_weight

        if self.args.max_movement_size is not None and score_movement:
            # Avoid potential divide-by-zero error
            max_movement = max(self.args.max_movement_size, 1)
            score += self.args.movement_size_score_weight * \
                (1 - state.movement_size / max_movement)
            max_score += self.args.movement_size_score_weight

        if self.args.max_leader_changes is not None and score_movement:
            # Avoid potential divide-by-zero error
            max_leader = max(self.args.max_leader_changes, 1)
            score += self.args.leader_change_score_weight * \
                (1 - state.leader_movement_count / max_leader)
            max_score += self.args.leader_change_score_weight

        return score / max_score