Definisi dan Aturan
Sebuah Array Golfy adalah sebuah array bilangan bulat, di mana setiap elemen adalah lebih besar atau sama dengan mean aritmetik dari semua elemen sebelumnya. Tugas Anda adalah menentukan apakah array bilangan bulat positif yang diberikan sebagai input golf atau tidak.
Anda tidak perlu menangani daftar kosong.
Anda dapat memilih dua nilai non-kosong yang berbeda. Mereka harus konsisten, dan harus mematuhi semua aturan masalah keputusan lainnya . Ini adalah kode-golf , kode terpendek dalam setiap bahasa yang menang!
Uji Kasus & Contoh
Misalnya array berikut:
[1, 4, 3, 8, 6]
Adalah array golf, karena setiap istilah lebih tinggi dari rata-rata aritmatika dari mereka yang mendahuluinya. Mari kita selesaikan langkah demi langkah:
Nomor -> Elemen sebelumnya -> Rata-rata -> Mengikuti aturan? 1 -> [] -> 0,0 -> 1 ≥ 0,0 (Benar) 4 -> [1] -> 1.0 -> 4 ≥ 1.0 (Benar) 3 -> [1, 4] -> 2.5 -> 3 ≥ 2.5 (Benar) 8 -> [1, 4, 3] -> 2. (6) -> 8 ≥ 2. (6) (Benar) 6 -> [1, 4, 3, 8] -> 4.0 -> 6 ≥ 4.0 (Benar)
Semua elemen menghormati kondisi ini, jadi ini adalah array golf. Perhatikan bahwa untuk tujuan tantangan ini, kami akan menganggap bahwa rata-rata daftar kosong ( []
) adalah 0
.
Lebih banyak kasus uji:
Input -> Output [3] -> Benar [2, 12] -> Benar [1, 4, 3, 8, 6] -> Benar [1, 2, 3, 4, 5] -> Benar [6, 6, 6, 6, 6] -> Benar [3, 2] -> Salah [4, 5, 6, 4] -> Salah [4, 2, 1, 5, 7] -> Salah [45, 45, 46, 43] -> Salah [32, 9, 15, 19, 10] -> Salah
Perhatikan bahwa ini adalah Puzzle 1 dari CodeGolf-Hackathon dan juga diposting di Anarchy Golf (yang rusak) - Diposting ulang oleh histokrat , tetapi saya adalah penulis asli di kedua situs, dan dengan demikian diizinkan untuk memposting ulang di sini.
sumber
Jawaban:
Python 2 , 37 byte
Cobalah online!
Output melalui kode keluar: crash (kode keluar 1) untuk array golf, hanya keluar dengan kode keluar 0 untuk array non-golf. ovs dan Jonathan Frech menyimpan 3 byte.
Python 2 , 44 byte
Cobalah online!
Varian yang lebih tradisional, yang kembali
True
untuk array golf, selain ituFalse
. Jonathan Frech menyimpan 2 byte.sumber
a==[]or
bisaa and
.sum(a)<=len(a)*a.pop()*[]
untuk kasus dasar, yang selalu benarint < list
!Jelly ,
65 byteCobalah online!
Bagaimana itu bekerja
sumber
ÆmƤµ⁼Ṣ
JavaScript (ES6),
3332 byteKode juga berfungsi pada nilai negatif seperti
[-3, -2]
. Pengembalianfalse
untuk array golf,true
untuk array lainnya. Sunting: Disimpan 1 byte berkat @JustinMariner.sumber
!
karena spec hanya meminta dua nilai yang berbeda, jadi kembalifalse
ketika array golf itu baik-baik saja.Bahasa Wolfram (Mathematica) , 35 byte
Cobalah online!
Output
False
untuk array golf danTrue
sebaliknya.sumber
MATL ,
98 byteOutput
0
untuk array golf,1
jika tidak.Cobalah online!
Penjelasan
Pertimbangkan input
[1, 4, 3, 8, 6]
.sumber
Haskell ,
535048 byteCobalah online!
Edit: -3 byte terima kasih kepada Zgarb!
Penjelasan
Versi bebas poin di atas setara dengan program berikut:
Diberi masukan
s=[1,4,3,8,6]
,scanl1(+)s
menghitung jumlah awalan[1,5,8,16,22]
danzipWith(*)[1..](tail s)
tetes elemen pertama dan mengalikan semua elemen lain dengan indeks mereka:[4,6,24,24]
. Daftar sekarang Golfy jika berpasangan dengan jumlah awalan lebih kecil atau sama dengan indeks elemen kali, yang dapat diperiksa dengan zipping kedua daftar dengan(<=)
dan memeriksa bahwa semua hasil adalahTrue
denganand
.sumber
C # (Visual C # Compiler) , 71 + 18 = 89 byte
tambahan 18 byte untuk
using System.Linq;
Cobalah online!
sumber
using System.Linq;
itu sebenarnya akan menjadi 89 byte, kadang-kadang dinyatakan sebagai "71 + 18 = 89" untuk menunjukkan bahwa 18 byte diperlukan tetapi bukan bagian dari solusi sementara masih memiliki hitungan akhir menjadi angka terakhir di baris judul ( yang bermanfaat untuk beberapa parser otomatis).APL (Dyalog) , 10 byte
Ini adalah fungsi awalan diam-diam anonim (disebut kereta monadik dalam istilah APL).
Coba semua test case di TIO!
Apakah itu
∧/
itu benar⊢
elemen-elemen≥
lebih besar atau sama dengan+\
jumlah kumulatif÷
dibagi dengan⍳
bilangan bulat 1 sampai∘
itu≢
sejumlah elemen?
sumber
∘
mengikat bersama hal-hal dengan cara yang sama "the" mengikat bersama dalam "menghitung kucing". Ini benar-benar disebut Tulis .C (gcc) ,
626062 byteb=
).Cobalah online!
sumber
05AB1E , 5 byte
Cobalah online!
Bantuan luas dari Dennis dan Adnan sampai ke versi yang dikurangi ini. Juga bug telah diperbaiki untuk memungkinkan ini, terima kasih sekali lagi kawan. Saya menerima sedikit pujian untuk jawaban ini.
05AB1E , 10 byte
Cobalah online!
Lama karenaDgsO/
setara dengan "rata-rata" di 05AB1E.Rupanya
ÅA
berarti aritmatika.sumber
+\÷J
(bagi jumlah kumulatif dengan indeks) di Jelly. Bukankah itu mudah di 05AB1E? Sunting: Nevermind.ü+
then there's really no divie by indices other thang
to get array length,L
to push1,2,...,n
and divide to get the mean, which is still essentially 5 bytes..S_
is a LONG way to go for<=
, if anyone has any ideas lmk.÷W
work instead ofü.S_P
?ÅA
, soηÅA÷W
works now.APL (Dyalog), 15 bytes
Try it online!
How?
sumber
PowerShell, 60 bytes
Try it online!
Takes input as a literal array (e.g.,
@(1, 4, 3, 8, 6)
) into$a
. Sets our$o
utput variable to be1
. Then loops through$a
. Each iteration, we're (ab)using PowerShell's implicit casting to*=
the result of a Boolean comparison against our$o
utput. The Boolean is whether the current value$_
is-g
reater-than-or-e
qual to the previous terms$a[0..$i++]
added together (-join'+'|iex
) divided by how many terms we've already seen$i
. So, if any step along the way is false, then$o
will get multiplied by0
. Otherwise, it will remain1
throughout.We then simply place
$o
onto the pipeline and output is implicit.1
for truthy and0
for falsey.sumber
Perl 5, 27 +2 (-ap) bytes
Try It Online
sumber
C# (.NET Core), 74 bytes
Try it online!
Returns 0 for false and 1 for true.
3 Bytes longer than core of chryslovelaces answer. But in total several Bytes shorter because my variant doesn't need any
using
statements.sumber
Cubix, 35 bytes
Try it online!
Not the most efficient use of space (6 no-ops in the code) Produces no output for a golfy array,
1
for a not-golfy array.Expands to the following cube:
Explanation forthcoming, but it basically ports something like Luis Mendo's MATL answer or Dennis' Julia answer.
Watch it run!
sumber
Matlab and Octave,
4136 bytes5 bytes saved thx to Luis Mendo
Try it online!
sumber
a
is zero. But that's a handy trick nonetheless in similar situations, have to keep that one in mind.SQL (MySQL), 68 bytes
Try it online!
Returns 1 for golfy arrays, and 0 otherwise. Takes input from a named table,
t
. In order to createt
, run:and to load the values:
sumber
Ruby, 30 bytes
Try it online!
Inspired by Lynn's answer. Throws
NoMethodError
for golfy, returnsnil
otherwise.sumber
Python 2, 52 bytes
Try it online!
Python 2,
50484442 bytesand
.S=k=0
.or
and the comparison's boolean value ask
's increment value.NameError
by using an undefined variable instead of aZeroDivisionError
.Try it online!
sumber
q/kdb+, 14 bytes
Solution:
Examples:
Explanation:
Fairly simple with the
avgs
built-in:sumber
Julia 0.6, 29 bytes
Returns false or true.
Try it online!
sumber
R,
3834 bytesTry it online!
sumber
y
in the function arguments, usingcumsum(x)
directly is 4 bytes shorter. It's a shamecummean
doesn't exist in base R.Add++, 54 bytes
Try it online!
Unoriginal version, 30 bytes
Try it online!
Both output 1 for golfy arrays and 0 otherwise
How they work
The first version was created by me, without checking any other solutions. The second was inspired by Dennis' comment, so I'm less happy with it.
The first version
Here, we define our main functionf that computes the golfiness of our input array, A . First, we need to yield the suffixes of A , which is done by first generating the range B:=[1,...|A|] , where |A| denotes the length of A . This is done with the code [B,A] as the stack. We then iterate the dyadic function g 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
dbLR$
, which leaveswhich is a dyadic function (i.e. takes2 arguments), and pushes its arguments to the stack in reversed order (A and e∈x . This leaves the stack as [...A,e] , where ... represents an array splat. Finally, e elements of A and returns a list containing those elements.
#
) before execution.BF
then flattens the two arguments. We'll assume that the arguments areB
takes the firstNote : The function namesg and k aren't chosen randomly. If the commands given to an operator (such as 2 bytes, as normally the function would have to wrapped in
€
) doesn't currently have a function (whichg
andk
don't), then the named functions are searched for a matching function. This saves{...}
to be recognised as a user-defined function. At the time of writing, the currently unused single byte commands areI
,K
,U
,Y
,Z
,g
,k
,l
,u
andw
.Wheng 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
which is the standard implementation of the arithmetic mean.C .
¦+
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,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 C+ .
0b]$
, then concatenate the two arrays forming a new array,Now, we need to check each element as being less than its corresponding element inA . We push A once again and zip the two arrays together with C+ when creating the pairs.
ABcB]
. This is the reason we don't need to pop the final element:Bc
is implemented with Python'szip
function, which truncates the longer arrays to fit the length of the shortest array. Here, this removes the final element ofFinally, we starmapp∈A,q∈C+;p<q≡¬(p≥q) over each pair p,q to obtain an array of all 0 s 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 remove24 bytes, by eliminating the helper functions. Given our input array of A , we first compute the cumulative sums with [A0,A0+A1,A0+A1+A2,...,A0+...+Ai] . We then generate Jelly's equivalent of B:=[1...|A|] where |A| once again means the length of the array.
¬+
, i.e the array created fromJ
(indicies), by calculating the rangeNext, we divide each element inA by the corresponding index in B with 0 with C+ , defined as
BcB/
and prepend@0@B]
. This results in a new array,The final part is identical to the first version: we push and zipA with C+ , then starmap inequality over each pair before asserting that all elements in the resulting array were falsy.
sumber
Pyth,
1110 bytes-1 byte thanks to Mr. Xcoder
Try it online!
sumber
SI.OM._
(port of cairdcoinheringaahing's solution from Jelly, by Erik the Outgolfer), or 10 bytes using your approach:.A.egb.O<Q
Java (OpenJDK 8), 96 bytes
I know it's not a good golfing language, but I still gave it a go!
Input array as first argument of comma separated ints to test.
Returns 1 for true, 0 for false.
Try it online!
sumber
Java 7, 100 bytes
Golfed:
Ungolfed:
Try it online
Returns 0 for ungolfy and 1 for golfy arrays. Slightly longer than java 8 answer.
sumber
PHP, 44 bytes
takes input from command line arguments, exits with
0
(ok) for a golfy array, with1
else.Run with
-nr
or try it online.sumber
J, 19 bytes
+/\ % #\
averages of the prefixes:#\
produces 1..n}:0,
add 0 to the beginning and remove the last[>:
is the original list element by element >= to the shifted list of averages?*/
are all the elements greater, ie, the previous list is all1
s?Try it online!
sumber
AWK, 39 bytes
Try it online!
Note that the TIO link has 5 extra bytes
i=s=0
to allow for multi-line input.sumber
Japt, 10 bytes
Came up with two 10 byte solutions, can't seem to improve on that.
Try it
Explanation
Alternative
Try it
sumber