Back to Practice
#0059

Deterministic Train-Test Split Indices

MediumML20 min20 XP

Problem

Given n rows, a test fraction, and a random seed, return two lists: train indices and test indices. Shuffle indices using random.Random(seed), put the first test_size indices into test, and the rest into train. test_size is round(n * test_fraction).

Why This Matters

A model score is only meaningful if the split is reproducible and the test set stays separate. This problem focuses on split logic before adding any model.

Function Signature

def train_test_indices(n, test_fraction, seed):

Examples

Example 1
Inputn = 5, test_fraction = 0.4, seed = 7
Outputtwo non-overlapping lists covering [0,1,2,3,4]

The exact order is deterministic because the seed controls the shuffle.

Constraints

  • Use random.Random(seed), not global random.seed.
  • Return train first, test second.
  • Each index from 0 to n-1 should appear exactly once across both lists.
CodePython
Visible browser tests run here when available.
Testcases2 visible / 3 hidden categories
Split covers all rows
Input10, 0.3, 42
Expected7 train indices and 3 test indices

round(10 * 0.3) gives three test rows.

Reproducible split
Input8, 0.25, 99
Expectedsame output on repeated calls

The seed should make the split deterministic.

Hidden Test Categories
zero test fractionhalf split with odd ndifferent seeds produce different order