Self-Consistency: Majority Vote over Samples

easy
Unit 1 · LLMs0/3 to milestone

Self-consistency boosts LLM accuracy on reasoning tasks: sample several answers at a non-zero temperature, then take the most common final answer.

Model outputs are messy, so normalise each answer before voting:

  • strip surrounding whitespace and a trailing full stop,
  • lowercase,
  • remove thousands separators from numbers ("1,000" → "1000"),
  • treat empty answers (after normalising) as abstentions that don't vote.

Implement majority_vote(answers) returning (answer, agreement), where answer is the winning normalised answer and agreement is its share of the non-empty votes. Break ties by the answer that appeared first. If nothing votes, return (None, 0.0).

Example

majority_vote(["42", " 42.", "41", "1,000", "42"])   # ("42", 0.6)
#llm#reasoning#prompting3 visible + 3 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
Self-Consistency: Majority Vote over Samples · TechGradify