# Path: dividebatur/results.py
# class ElectionDistributionPerformed:
#     """
#     Information on any election distribution which is performed during a
#     counting round.
#     """
# 
#     def __init__(self, candidate_id, transfer_value):
#         """
#         transfer_value is a Fraction instance
#         """
#         self.candidate_id = candidate_id
#         self.transfer_value = transfer_value
# 
# class ExclusionDistributionPerformed:
#     """
#     Information on any exclusion distribution which is performed during a
#     counting round.
#     """
# 
#     def __init__(self, candidates, transfer_value):
#         """
#         candidates: a List of candidate_ids
#         transfer_value: transfer value, as a Fraction
#         """
#         self.candidates = candidates
#         self.transfer_value = transfer_value
# 
# class ActProvision:
#     """
#     Note that a provision of the Act has been used.
#     """
# 
#     def __init__(self, text):
#         """
#         text: textual description of the provision used.
#         """
# 
#         self.text = text
# 
# class CandidateElected:
#     """
#     Information on the election of a candidate.
#     """
# 
#     def __init__(self, candidate_id, order, excess_votes, paper_count, transfer_value):
#         """
#         candidate_id: the candidate elected
#         order: the number of the spot the candidate was elected to [1..N_vacancies]
#         excess_votes: the number of excess votes the candidate received
#         paper_count: the number of papers the candidate held at time of elect
#         transfer_value: the transfer value for the excess papers (0 if no excess papers)
#         """
# 
#         self.candidate_id = candidate_id
#         self.order = order
#         self.excess_votes = excess_votes
#         self.paper_count = paper_count
#         self.transfer_value = transfer_value
# 
# class CandidatesExcluded:
#     """
#     Information on the exclusion of one or more candidates.
#     """
# 
#     def __init__(self, candidates, transfer_values, reason):
#         """
#         candidates: list of candidate_ids of those candidates elected
#         transfer_values: the transfer values of papers to be distributed (a list of Fraction instances)
#         reason: an instance of ExclusionReason
#         """
#         self.candidates = candidates
#         self.transfer_values = transfer_values
#         self.reason = reason
# 
# class ExclusionReason:
#     """
#     information of interest about an exclusion of one or more
#     candidates.
# 
#     simply to make this information available to users of this
#     counter, this data is not used to make any decisions which
#     affect count results.
#     """
#     def __init__(self, reason, info):
#         """
#         reason: a string identifying the reason
#         info: additional information
#         """
#         self.reason, self.info = reason, info


from collections import defaultdict, namedtuple
from .results import ElectionDistributionPerformed, \
    ExclusionDistributionPerformed, ActProvision, \
    CandidateElected, CandidatesExcluded, \
    ExclusionReason
import itertools
import fractions

        def determine_candidate_C():
            eligible = [candidate_id for candidate_id in continuing if candidate_notional_votes[candidate_id] < leading_shortfall]
            # the candidate of those eligible which stands highest in the poll
            eligible.sort(key=lambda candidate_id: candidate_votes[candidate_id])
            return highest(eligible)

        def candidates_lte(candidate_id):
            votes = candidate_votes[candidate_id]
            lower_votes = [t for t in by_votes.keys() if t < votes]
            to_exclude = [candidate_id]
            for vote in lower_votes:
                to_exclude += [candidate_id for candidate_id in by_votes[vote] if candidate_id in continuing]
            return to_exclude

        # candidate A, B, C as under (13A)(a)
        to_exclude = None
        candidate_A = determine_candidate_A()
        candidate_B = determine_candidate_B(candidate_A)
        candidate_C = None
        if candidate_B:
            candidate_B_votes = candidate_votes[candidate_B]
            if candidate_B_votes < leading_shortfall:
                to_exclude = candidates_lte(candidate_B)
            else:
                candidate_C = determine_candidate_C()
                if candidate_C:
                    to_exclude = candidates_lte(candidate_C)
        if to_exclude and len(to_exclude) == 1:
            to_exclude = None

# Path: dividebatur/results.py
# class ElectionDistributionPerformed:
#     """
#     Information on any election distribution which is performed during a
#     counting round.
#     """
# 
#     def __init__(self, candidate_id, transfer_value):
#         """
#         transfer_value is a Fraction instance
#         """
#         self.candidate_id = candidate_id
#         self.transfer_value = transfer_value
# 
# class ExclusionDistributionPerformed:
#     """
#     Information on any exclusion distribution which is performed during a
#     counting round.
#     """
# 
#     def __init__(self, candidates, transfer_value):
#         """
#         candidates: a List of candidate_ids
#         transfer_value: transfer value, as a Fraction
#         """
#         self.candidates = candidates
#         self.transfer_value = transfer_value
# 
# class ActProvision:
#     """
#     Note that a provision of the Act has been used.
#     """
# 
#     def __init__(self, text):
#         """
#         text: textual description of the provision used.
#         """
# 
#         self.text = text
# 
# class CandidateElected:
#     """
#     Information on the election of a candidate.
#     """
# 
#     def __init__(self, candidate_id, order, excess_votes, paper_count, transfer_value):
#         """
#         candidate_id: the candidate elected
#         order: the number of the spot the candidate was elected to [1..N_vacancies]
#         excess_votes: the number of excess votes the candidate received
#         paper_count: the number of papers the candidate held at time of elect
#         transfer_value: the transfer value for the excess papers (0 if no excess papers)
#         """
# 
#         self.candidate_id = candidate_id
#         self.order = order
#         self.excess_votes = excess_votes
#         self.paper_count = paper_count
#         self.transfer_value = transfer_value
# 
# class CandidatesExcluded:
#     """
#     Information on the exclusion of one or more candidates.
#     """
# 
#     def __init__(self, candidates, transfer_values, reason):
#         """
#         candidates: list of candidate_ids of those candidates elected
#         transfer_values: the transfer values of papers to be distributed (a list of Fraction instances)
#         reason: an instance of ExclusionReason
#         """
#         self.candidates = candidates
#         self.transfer_values = transfer_values
#         self.reason = reason
# 
# class ExclusionReason:
#     """
#     information of interest about an exclusion of one or more
#     candidates.
# 
#     simply to make this information available to users of this
#     counter, this data is not used to make any decisions which
#     affect count results.
#     """
#     def __init__(self, reason, info):
#         """
#         reason: a string identifying the reason
#         info: additional information
#         """
#         self.reason, self.info = reason, info


# Path: dividebatur/counter.py
from collections import defaultdict, namedtuple
from .results import ElectionDistributionPerformed, \
    ExclusionDistributionPerformed, ActProvision, \
    CandidateElected, CandidatesExcluded, \
    ExclusionReason
import itertools
import fractions


        def determine_candidate_C():
            eligible = [candidate_id for candidate_id in continuing if candidate_notional_votes[candidate_id] < leading_shortfall]
            # the candidate of those eligible which stands highest in the poll
            eligible.sort(key=lambda candidate_id: candidate_votes[candidate_id])
            return highest(eligible)

        def candidates_lte(candidate_id):
            votes = candidate_votes[candidate_id]
            lower_votes = [t for t in by_votes.keys() if t < votes]
            to_exclude = [candidate_id]
            for vote in lower_votes:
                to_exclude += [candidate_id for candidate_id in by_votes[vote] if candidate_id in continuing]
            return to_exclude

        # candidate A, B, C as under (13A)(a)
        to_exclude = None
        candidate_A = determine_candidate_A()
        candidate_B = determine_candidate_B(candidate_A)
        candidate_C = None
        if candidate_B:
            candidate_B_votes = candidate_votes[candidate_B]
            if candidate_B_votes < leading_shortfall:
                to_exclude = candidates_lte(candidate_B)
            else:
                candidate_C = determine_candidate_C()
                if candidate_C:
                    to_exclude = candidates_lte(candidate_C)
        if to_exclude and len(to_exclude) == 1:
            to_exclude = None

        return to_exclude, ExclusionReason("bulk", {