Dataset Splitting & Sampling
Beginner
K-Fold Size Allocation
Beginner
~10 min
code completion
K-Fold Size Allocation
K-fold cross-validation partitions n samples into k non-overlapping folds. Each fold serves as the validation set once while the other folds form the training set.
When n is not evenly divisible by k, the sizes can not all be equal. The standard convention:
n % k folds get size n // k + 1n // kThis ensures the sizes sum to exactly n.
Example: n = 10, k = 3
base = 10 // 3 = 3, remainder = 10 % 3 = 1[4, 3, 3] — verify: 4 + 3 + 3 = 10 ✓Your task:
Implement kfold_sizes(n, k) that returns a Python list of k integers summing to n.
Example Tests
10 samples, 3 folds: remainder 1 goes to first fold
Input: {"k":3,"n":10}
Expected: [4,3,3]
Evenly divisible: all folds have equal size
Input: {"k":4,"n":12}
Expected: [3,3,3,3]
7 samples, 3 folds: remainder 1 to first fold
Input: {"k":3,"n":7}
Expected: [3,2,2]