A documented split rule lets anyone rebuild exactly the same training and test sets. Send every k-th row to the test set.
What to build
A program that splits rows into training and test sets by position.
Requirements
- Counting positions from 1, a row goes to the test set when its position is divisible by k, and to the training set otherwise.
- Print
train N: ...andtest M: ..., each followed by its rows in their original order, separated by spaces.
Input
Line 1: k. Line 2: the rows, as words separated by spaces.
Output
Two lines.
Constraints
1 ≤ k ≤ 100; 1 to 1000 rows.
Example
Input:
5
r1 r2 r3 r4 r5 r6 r7 r8 r9 r10
Output:
train 8: r1 r2 r3 r4 r6 r7 r8 r9
test 2: r5 r10
Why it works
A rule based only on position gives the same split every time it runs, so results can be reproduced and compared.
Examples
Example 1 · Input
5 r1 r2 r3 r4 r5 r6 r7 r8 r9 r10
Expected output
train 8: r1 r2 r3 r4 r6 r7 r8 r9 test 2: r5 r10
Example 2 · Input
2 a b c
Expected output
train 2: a c test 1: b
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Use enumerate(items, 1) to count positions from 1.
Hint 2
print("test", f"{len(test)}:", *test) prints the count and the items.
Solution
Show one correct solution (try it yourself first)
k = int(input())
items = input().split()
train = [x for i, x in enumerate(items, 1) if i % k != 0]
test = [x for i, x in enumerate(items, 1) if i % k == 0]
print(f"train {len(train)}:", *train)
print(f"test {len(test)}:", *test)