D,g,@@#,BFB
D,k,@,¦+AbL/
D,f,@,dbLR$€g€k0b]$+ABcB]£>ª!
Essayez-le en ligne!
Version non originale, 30 octets
D,f,@,¬+AbLRBcB/@0@B]ABcB]£>ª!
Essayez-le en ligne!
Les deux sorties 1 pour les tableaux de golf et 0 sinon
Comment ils travaillent
La première version a été créée par mes soins, sans vérifier aucune autre solution. Le second a été inspiré par le commentaire de Dennis , donc je suis moins content.
La première version
Ici, nous définissons notre fonction principale Fqui calcule le golfe de notre tableau d'entrée,UNE. Tout d'abord, nous devons fournir les suffixes deUNE, ce qui se fait en générant d'abord la plage B : = [ 1 , . . . | A | ], où | A | dénote la longueur de UNE. Cela se fait avec le code dbLR$
, qui laisse[ B , A ]comme la pile. On itère ensuite la fonction dyadiquegsur ces deux listes. Étant dyadique, la fonction lie son argument de gauche commeUNEpour chaque élément itéré. Il itère ensuite sur la plageB, dont chaque élément de B étant le bon argument fourni à g. g est défini comme
D,g,@@#,BFB
qui est une fonction dyadique (ie prend 2arguments), et pousse ses arguments dans la pile dans l'ordre inverse ( #
) avant l'exécution. BF
aplatit ensuite les deux arguments. Nous supposerons que les arguments sontUNE et 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.