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. /Derangements and Catalan numbers
MathematicsMediumHigh School (11–12)Pigeonhole Principle, Recursion and Graph Theory

Derangements and Catalan numbers

You will compute derangement numbers and Catalan numbers from their recurrences, and count the orders with exactly kkk items in place.

The idea. Dn=(n−1)(Dn−1+Dn−2)D_n = (n - 1)(D_{n-1} + D_{n-2})Dn​=(n−1)(Dn−1​+Dn−2​) with D0=1D_0 = 1D0​=1 and D1=0D_1 = 0D1​=0. The Catalan numbers follow Cn+1=C0Cn+C1Cn−1+⋯+CnC0C_{n+1} = C_0C_n + C_1C_{n-1} + \dots + C_nC_0Cn+1​=C0​Cn​+C1​Cn−1​+⋯+Cn​C0​ with C0=1C_0 = 1C0​=1. Exactly kkk items in place: (nk)Dn−k\binom{n}{k}D_{n-k}(kn​)Dn−k​.

Your tasks.

  1. Finish derangements(n). Build the list d = [1, 0] and extend it with the recurrence.
  2. Finish catalan(n). Build the list c = [1] and extend it with the sum.
  3. Finish exactly_fixed(n, k). Use comb(n, k) and derangements(n - k).

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, add up exactly_fixed(6, k) for k from 0 to 6. Which number do you get, and why?

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
← Counting by recursionDegrees and components →
More onPigeonhole Principle, Recursion and Graph Theory

Discussion (0)

Log in to join the discussion.

Loading…

Learn it in

  1. Derangements and Catalan numbersRecurrences and Counting by Recursion

More labs on this topic

  1. Euler and two coloursMedium
  2. Degrees and componentsMedium
  3. Counting by recursionMedium
  4. Blocks and odd partsMedium
  5. Boxes for remainders
Medium
All labs on this topic →