Background
J has trains similar to APL's. Given a sequence of verbs (functions), three rightmost verbs are grouped to form a derived verb (a fork) recursively, until one or two verbs remain. If the sequence has odd length, the entire train is a chain of forks. Otherwise, two verbs remain at the end, forming a hook.
(F G H J K) x 5-train called monadically
-> (F G (H J K)) x
-> (F x) G ((H x) J (K x)) Function arities: 1, 2, 1, 2, 1
x (F G H J K) y 5-train called dyadically
-> x (F G (H J K)) y
-> (x F y) G ((x H y) J (x K y)) Function arities: 2, 2, 2, 2, 2
(F G H J K L) x 6-train called monadically
-> (F (G H (J K L))) x
-> x F ((G x) H ((J x) K (L x))) Function arities: 2, 1, 2, 1, 2, 1
x (F G H J K L) y 6-train called dyadically
-> x (F (G H (J K L))) y
-> x F ((G y) H ((J y) K (L y))) Function arities: 2, 1, 2, 1, 2, 1
You don't need to fully understand J trains to solve this challenge. The pattern is pretty simple:
- If the train length is even, the pattern is
[2, 1, 2, 1, ..., 2, 1]
regardless of the train's arity. - Otherwise (length is odd), if the train is called monadically, the pattern is
[1, 2, 1, 2, ..., 2, 1]
; if called dyadically, the pattern is all 2's.
Challenge
Given a train's length and arity (monadic is 1, dyadic is 2), output the arities of each function in the train.
Standard code-golf rules apply. The shortest code in bytes wins.
Test cases
length, arity -> answer
1, 1 -> [1]
1, 2 -> [2]
2, 1 -> [2, 1]
2, 2 -> [2, 1]
3, 1 -> [1, 2, 1]
3, 2 -> [2, 2, 2]
4, 1 -> [2, 1, 2, 1]
4, 2 -> [2, 1, 2, 1]
9, 1 -> [1, 2, 1, 2, 1, 2, 1, 2, 1]
9, 2 -> [2, 2, 2, 2, 2, 2, 2, 2, 2]
10, 1 -> [2, 1, 2, 1, 2, 1, 2, 1, 2, 1]
10, 2 -> [2, 1, 2, 1, 2, 1, 2, 1, 2, 1]
"2121"
is fine though. \$\endgroup\$