Skip to content

Instantly share code, notes, and snippets.

@mcall52
mcall52 / coding_exercise.py
Created October 25, 2022 13:23
A short coding exercise. The objective of this code is to allocate N objects into M buckets and print out all possible ways to allocate the objects in the buckets.
def allocate_objects(bucket_count: int, object_count: int, allocated_objects: str = "") -> None:
"""Prints all possible combinations for allocating objects.
This function finds all possible combinations of ways to allocate objects
by putting all objects in the first bucket, then iteratively taking away one
object from the first bucket and placing it in the next bucket. The solution
uses recursion to divide the larger problem (allocate N objects into M buckets)
into smaller problems (allocate k objects into M - 1 buckets, then M - 2 buckets ... then into 1 bucket).
For example, given `bucket_count = 3` and `object_count = 4`:

Keybase proof

I hereby claim:

  • I am mcall52 on github.
  • I am mcall52 (https://keybase.io/mcall52) on keybase.
  • I have a public key ASBlabP5K0YgvqHkvuj68rn6KIsicUHS6BofSxTmZoTH1wo

To claim this, I am signing this object: