<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def remove_replica(self, partition_name, osr_broker_ids, count=1):
 """Remove one replica of a partition from the cluster. The replication-group to remove from is determined as follows: 1. Find all replication-groups that contain at least one out-of-sync replica for this partition. 2. Of these, find replication-groups with more than the average number of replicas of this partition. 3. Choose the replication-group with the most overall partitions. 4. Repeat steps 1-3 with in-sync replicas After this operation, the preferred leader for this partition will be set to the broker that leads the fewest other partitions, even if the current preferred leader is not removed. This is done to keep the number of preferred replicas balanced across brokers in the cluster. :param partition_name: (topic_id, partition_id) of the partition to remove replicas of. :param osr_broker_ids: A list of the partition's out-of-sync broker ids. :param count: The number of replicas to remove. :raises: InvalidReplicationFactorError when count is greater than the replication factor of the partition. """

        try:
            partition = self.cluster_topology.partitions[partition_name]
        except KeyError:
            raise InvalidPartitionError(
                "Partition name {name} not found".format(name=partition_name),
            )
        if partition.replication_factor <= count:
            raise InvalidReplicationFactorError(
                "Cannot remove {0} replicas. Replication factor is only {1}."
                .format(count, partition.replication_factor)
            )

        osr = []
        for broker_id in osr_broker_ids:
            try:
                osr.append(self.cluster_topology.brokers[broker_id])
            except KeyError:
                raise InvalidBrokerIdError(
                    "No broker found with id {bid}".format(bid=broker_id),
                )

        non_empty_rgs = [
            rg
            for rg in self.cluster_topology.rgs.values()
            if rg.count_replica(partition) > 0
        ]
        rgs_with_osr = [
            rg
            for rg in non_empty_rgs
            if any(b in osr for b in rg.brokers)
        ]

        for _ in range(count):
            candidate_rgs = rgs_with_osr or non_empty_rgs
            total_replicas = sum(
                rg.count_replica(partition)
                for rg in candidate_rgs
            )
            opt_replica_cnt, _ = compute_optimum(
                len(candidate_rgs),
                total_replicas,
            )
            over_replicated_rgs = [
                rg
                for rg in candidate_rgs
                if rg.count_replica(partition) > opt_replica_cnt
            ]
            candidate_rgs = over_replicated_rgs or candidate_rgs
            rg = max(candidate_rgs, key=lambda rg: len(rg.partitions))

            osr_in_rg = [b for b in rg.brokers if b in osr]
            rg.remove_replica(partition, osr_in_rg)

            osr = [b for b in osr if b in partition.replicas]
            if rg in rgs_with_osr and len(osr_in_rg) == 1:
                rgs_with_osr.remove(rg)
            if rg.count_replica(partition) == 0:
                non_empty_rgs.remove(rg)

        new_leader = min(
            partition.replicas,
            key=lambda broker: broker.count_preferred_replica(),
        )
        partition.swap_leader(new_leader)