D,g,@@#,BFB
D,k,@,¦+AbL/
D,f,@,dbLR$€g€k0b]$+ABcB]£>ª!
Çevrimiçi deneyin!
Orijinal olmayan sürüm, 30 bayt
D,f,@,¬+AbLRBcB/@0@B]ABcB]£>ª!
Çevrimiçi deneyin!
Her ikisi de golf dizileri için çıkış 1 , aksi takdirde 0
Onlar nasıl çalışır
İlk sürüm, başka çözümleri kontrol etmeden benim tarafımdan oluşturuldu. İkincisi Dennis'in yorumundan ilham aldı , bu yüzden bundan daha az memnunum.
İlk sürüm
Burada ana fonksiyonumuzu tanımlıyoruz fgirdi dizimizin golfitesini hesaplayan ,bir. İlk olarak, son eklerini vermemiz gerekir.bir, ilk önce aralık üretilerek yapılır B : = [ 1 , . . . | A | ], nerede | A | uzunluğunu gösterir bir. Bu kod ile yapılır dbLR$
hangi yaprakları,[ B , A ]yığını olarak. Daha sonra ikili fonksiyonu tekrarlıyoruzg over these two lists. Being dyadic, the function binds its left argument as A for each iterated element. It then iterates over the range B, which each element from B being the right argument provided to g. g is defined as
D,g,@@#,BFB
which is a dyadic function (i.e. takes 2 arguments), and pushes its arguments to the stack in reversed order (#
) before execution. BF
then flattens the two arguments. We'll assume that the arguments are A and e∈x. This leaves the stack as [...A,e], where ... represents an array splat. Finally, B
takes the first e elements of A and returns a list containing those elements.
Note : The function names g and k aren't chosen randomly. If the commands given to an operator (such as €
) doesn't currently have a function (which g
and k
don't), then the named functions are searched for a matching function. This saves 2 bytes, as normally the function would have to wrapped in {...}
to be recognised as a user-defined function. At the time of writing, the currently unused single byte commands are I
, K
, U
, Y
, Z
, g
, k
, l
, u
and w
.
When g is applied over the elements of a range x, this returns a list of prefixes for A. We then map our second helper function k over each of these prefixes. k is defined as
D,k,@,¦+AbL/
which is the standard implementation of the arithmetic mean. ¦+
calculates the sum of the argument, AbL
calculates its length, then /
divides the sum by the length. This calculates the arithmetic mean of each prefix, yielding a new array, C.
Unfortunately, C contains the mean of A as its final element, and does not include the mean of the empty list, 0. Therefore, we would have to remove the final element, and prepend a 0, but popping can be skipped, saving two bytes, for reasons explained in a second. Instead, we push [0] underneath C with 0b]$
, then concatenate the two arrays forming a new array, C+.
Now, we need to check each element as being less than its corresponding element in A. We push A once again and zip the two arrays together with ABcB]
. This is the reason we don't need to pop the final element: Bc
is implemented with Python's zip
function, which truncates the longer arrays to fit the length of the shortest array. Here, this removes the final element of C+ when creating the pairs.
Finally, we starmap p∈A,q∈C+;p<q≡¬(p≥q) over each pair p,q to obtain an array of all 0s if the array is golfy, and array containing at least a single 1 if otherwise. We then check that all elements are falsey i.e. are equal to 0 with ª!
and return that value.
The second version
This takes advantage of Dennis' approach to remove 24 bytes, by eliminating the helper functions. Given our input array of A, we first compute the cumulative sums with ¬+
, i.e the array created from [A0,A0+A1,A0+A1+A2,...,A0+...+Ai]. We then generate Jelly's equivalent of J
(indicies), by calculating the range B:=[1...|A|] where |A| once again means the length of the array.
Next, we divide each element in A by the corresponding index in B with BcB/
and prepend 0 with @0@B]
. This results in a new array, C+, defined as
C+:=[0,A0,A0+A12,A0+A1+A23,...,A0+...+Aii+1]
The final part is identical to the first version: we push and zip A with C+, then starmap inequality over each pair before asserting that all elements in the resulting array were falsy.