Pick a final answer by self-consistency majority vote

Why it matters Majority voting over sampled chains of thought is a cheap, strong way to raise accuracy on reasoning benchmarks.

Self-consistency samples several reasoning paths and keeps the answer that appears most often. Answers must be normalised first or "42" and " 42" split the vote.

Write majority_vote(answers). answers is a list of strings or None (None means the answer could not be parsed). Normalise each string with strip() and lower(); skip None and empty strings. Return (winner, share) where share = winner's count / number of valid answers. On a tie, pick the answer whose first valid occurrence comes earliest. If no valid answers, return (None, 0.0).

Signaturedef majority_vote(answers)

Example

majority_vote(["42", " 42", "41", None]) is ("42", 0.6667) (share 2/3).

Understand the idea

Ask a model the same maths question five times with sampling, and the reasoning paths differ. Wrong paths tend to scatter over many different answers, while correct paths tend to agree. So the most common final answer is usually right, like asking several friends and going with the crowd.

Example: answers "42", " 42", "41" and one unparsable output. After trimming and lowercasing: "42" twice, "41" once, one skipped. Winner "42" with share 2 / 3 = 0.667.

Rule: normalise; drop invalid; winner = argmax count (earliest first occurrence on ties); share = count / valid.

The share is a useful confidence signal: a 9 out of 10 vote is far more reliable than 3 out of 10. Self-consistency is reported on most reasoning benchmarks as maj@k, next to pass@k.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks all 4.

  • Sample 1: examplemajority_vote(['42', ' 42', '41', None])
  • Sample 2: tie goes to earliestmajority_vote(['B', 'a', 'b', 'A'])
  • Test 3: all unparsedmajority_vote([None, '', ' '])
  • Test 4: casemajority_vote(['Paris', 'paris', 'Lyon'])