A simple, repeatable split keeps every k-th example aside for testing. Split a list that way.
What to build
A program that splits a list into a training set and a test set.
Requirements
- Items at positions k, 2k, 3k, ... (counting from 1) go to the test set; all others to the training set.
- Keep the original order in both sets.
- Print
trainfollowed by its items, thentestfollowed by its items.
Input
Line 1: k. Line 2: the items, separated by spaces.
Output
Two lines.
Constraints
2 ≤ k ≤ number of items ≤ 1000; items are single words or numbers.
Example
Input:
5
1 2 3 4 5 6 7 8 9 10
Output:
train 1 2 3 4 6 7 8 9
test 5 10
Why it works
Taking every k-th item spreads the test set evenly across the data, and doing it the same way every time makes the split repeatable.
Examples
Example 1 · Input
5 1 2 3 4 5 6 7 8 9 10
Expected output
train 1 2 3 4 6 7 8 9 test 5 10
Example 2 · Input
2 a b c
Expected output
train a c test b
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Positions start at 1: the k-th, 2k-th, 3k-th items go to the test set.
Hint 2
Use `enumerate(values, 1)` and test `i % k == 0`.
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("train", *train)
print("test", *test)