;Ė{~b.hℕ₁≜∧.¬{ġh₃hᵐs₂ᶠ-ᵐ=}∧}ⁱ⁽↔
オンラインでお試しください!
重要な場合:このチャレンジに取り組んだ後に私が要求した機能のおかげで、2バイトのゴルフが可能になりました。
説明
シーケンスをリストとして逆順に繰り返し生成します。例えば [2,2,1,1,2,1,1]
、最後に逆順にします。
ここにはいくつかのネストされた述語があります。それらを内側から見てみましょう。最初のものġh₃hᵐs₂ᶠ-ᵐ=
は、候補サブシーケンスを取り、someの算術シーケンスであるa(n),a(n-1),...,a(0)
かどうかを判別します。a(n),a(n-k),a(n-2k)
k
ġ Group the list into equal-length sublists (with the possible exception of
the last sublist, which might be shorter)
h₃ Get the first 3 sublists from that list
hᵐ and get the head of each of those 3 sublists
We now have a list containing a(n),a(n-k),a(n-2k) for some k
s₂ᶠ Find all 2-element sublists of that list: [a(n),a(n-k)] and [a(n-k),a(n-2k)]
-ᵐ Find the difference of each pair
= Assert that the two pairwise differences are equal
たとえば、次の入力がある場合[1,2,1,1,2,1,1]
:
ġ has possible outputs of
[[1],[2],[1],[1],[2],[1],[1]]
[[1,2],[1,1],[2,1],[1]]
[[1,2,1],[1,2,1],[1]]
[[1,2,1,1],[2,1,1]]
[[1,2,1,1,2],[1,1]]
[[1,2,1,1,2,1],[1]]
[[1,2,1,1,2,1,1]]
h₃ has possible outputs of
[[1],[2],[1]]
[[1,2],[1,1],[2,1]]
[[1,2,1],[1,2,1],[1]]
hᵐ has possible outputs of
[1,2,1]
[1,1,2]
[1,1,1]
s₂ᶠ has possible outputs of
[[1,2],[2,1]]
[[1,1],[1,2]]
[[1,1],[1,1]]
-ᵐ has possible outputs of
[-1,1]
[0,-1]
[0,0]
= is satisfied by the last of these, so the predicate succeeds.
次の述語~b.hℕ₁≜∧.¬{...}∧
は、サブシーケンスa(n-1),a(n-2),...,a(0)
を入力し、次に大きなサブシーケンスを出力しますa(n),a(n-1),a(n-2),...,a(0)
。
~b.hℕ₁≜∧.¬{...}∧
~b. The input is the result of beheading the output; i.e., the output is
the input with some value prepended
.h The head of the output
ℕ₁ is a natural number >= 1
≜ Force a choice as to which number (I'm not sure why this is necessary,
but the code doesn't work without it)
∧ Also,
. the output
¬{...} does not satisfy the nested predicate (see above)
I.e. there is no k such that a(n),a(n-k),a(n-2k) is an arithmetic sequence
∧ Break unification with the output
最後に、メインの述語;Ė{...}ⁱ⁽↔
は入力番号を受け取り、シーケンスの多くの項を出力します。
;Ė{...}ⁱ⁽↔
; Pair the input number with
Ė the empty list
{...}ⁱ⁽ Using the first element of the pair as the iteration count and the second
element as the initial value, iterate the nested predicate (see above)
↔ Reverse, putting the sequence in the proper order