Skip to content
This repository was archived by the owner on Sep 17, 2022. It is now read-only.
This repository was archived by the owner on Sep 17, 2022. It is now read-only.

Add functionality for getPollResults endpoint #23

@binamkayastha

Description

@binamkayastha

For now do the simplest possible thing and just calculate the poll results whenever the endpoint is hit.
AC

  • When the endpoint is hit, get all the votes for this poll stored in the db, calculate the PollResults object, and return it

Input Example (13 different votes):
@CoryTapply to add

Output Example:
https://github.com/SocietyClub/RCV/pull/42/files#diff-c15db25bce7ec1971f2405350034b8189e679a48e0253f62d33297a05b51f974R101

Pseudo Code made with @calper-ql :

from collections import defaultdict
import json


def main():
    votes = {
        "uuid1": {
            0: {
                "name": "batman"
            },
            1: {
                "name": "superman"
            },
        },
        "uuid2": {
            0: {
                "name": "batman"
            },
            1: {
                "name": "superman"
            },
        }
    }

    steps: list = []
    step_id = 0
    surviving_candidates: list[str] = []
    while True:
        candidates_to_vote_counts: dict = defaultdict(lambda: 0)

        for uuid, vote in votes.items():
            adjusted_preference = get_adjusted_preference(
                vote, surviving_candidates
            )
            if adjusted_preference:
                candidates_to_vote_counts[adjusted_preference] += 1
        surviving_candidates = elimiate_candidates(
            candidates_to_vote_counts, surviving_candidates
        )
        steps.append({
            "stepId": step_id,
            "candidateList": [
                {
                    "name": candidate_name,
                    "eliminated": candidate_name not in surviving_candidates,
                    "voteCount": vote_count
                }
                for candidate_name, vote_count in candidates_to_vote_counts.items()
            ]
        })
        step_id += 1
        if len(surviving_candidates) == 1:
            break
    return json.dumps(steps)


def get_adjusted_preference(
    vote: dict[int, dict[str, str]], surviving_candidates: list[str]
) -> str:
    """Optional candidate as string"""
    pass


def elimiate_candidates(
    candidate_to_vote_counts: dict, surviving_candidates: list[str]
) -> list[str]:
    """Returns list of surviving_candidates by eliminating candidate with least
    number of vote counts"""
    miminum_candidate = ""
    return []


if __name__ == "__main__":
    main()

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions