HelpContactFor teachers and institutes
>_StemPy Lab
Mock TestsClasses

>_StemPy Lab

Learn. Solve. Code the solutions. Test yourself.

Live examsFree mock exams, timed and rankedEveryone starts together, and your score and rank come the moment it ends.See the exams →
Live examsInternational live exams are comingTell us which exam you want first. Timed practice papers are open now.See what is coming →

Learn

  • Physics
  • Chemistry
  • Mathematics
  • Biology
  • Python Programming
  • Statistics & Data Science
  • Computer Science
  • Space Science and Astronomy
  • Earth and Environmental Science
  • Classes
  • Groups

Practice

  • Problems
  • Quizzes
  • Mock Tests
  • Prepare for an Exam
  • Question Sets
  • Exams
  • Certificates
  • Python Labs
  • AI Practice Generator
  • Student-Created Tests

Community

  • Discussions
  • Resources
  • Notes
  • Glossary
  • Blog

Account

  • Log in
© 2026 senLearn. All rights reserved.HelpFor SchoolsAboutContactPrivacy PolicyTerms of UseRefund Policy
  1. Python Labs
  2. /Boxes for remainders
MathematicsMediumHigh School (11–12)Pigeonhole Principle, Recursion and Graph Theory

Boxes for remainders

You will write three small functions that choose the boxes of a pigeonhole argument and test it.

The idea. For a difference divisible by mmm, the box of a number is its remainder x % m. For a sum or a difference divisible by mmm, the remainders rrr and m−rm - rm−r share a box. We can label that box by the smaller of the two.

Your tasks.

  1. Finish fewest_for_pair(colours). Return the fewest socks that make sure of two of one colour.
  2. Finish same_remainder(numbers, m). Return a pair (a, b) of numbers from the list with the same remainder on division by m, or None if there is no such pair.
  3. Finish pair_box(x, m). Return the box of x when rrr and m−rm - rm−r share a box: the smaller of x % m and (m - x % m) % m.

Run it first, just as it is. The checker at the end prints FAIL for each task. Then do the tasks one at a time, and run the program again after each one. A Hint appears under every check that still fails.

When every line says PASS, use pair_box on the numbers 0 to 9 with m = 10. How many different boxes do you see?

StemPy LabContributed by StemPy Lab
python
Loading Python runtime…

A worked solution

Run your code once to unlock the worked solution.

Have you tried it?

Do the tasks in the editor first, and run the checker after each one. Its hints help when a check fails. When you are ready, compare your code with this solution.

Log in to save
← All Labs
← The point that minimises total distanceBlocks and odd parts →
More onPigeonhole Principle, Recursion and Graph Theory

Discussion (0)

Log in to join the discussion.

Loading…

Learn it in

  1. The pigeonhole principleThe Pigeonhole Principle

More labs on this topic

  1. Euler and two coloursMedium
  2. Degrees and componentsMedium
  3. Derangements and Catalan numbersMedium
  4. Counting by recursionMedium
  5. Blocks and odd parts
Medium
All labs on this topic →