| Bytes | Lang | Time | Link |
|---|---|---|---|
| 018 | APLNARS | 250912T050040Z | Rosario |
| 013 | Pip | 210920T024807Z | DLosc |
| 023 | Uiua | 240713T232605Z | jan |
| 147 | Pascal FPC | 210919T195348Z | jdt |
| 014 | Pyt | 240714T010710Z | Kip the |
| 030 | Wolfram Language Mathematica | 210916T032325Z | att |
| 042 | JavaScript Node.js | 240711T234631Z | l4m2 |
| 040 | Octave | 240711T163451Z | Glory2Uk |
| 034 | Julia | 220220T163026Z | David Sc |
| 131 | Lean | 211005T220522Z | Wheat Wi |
| 068 | Python 3.8 prerelease | 211006T000714Z | Stephen |
| 020 | Charcoal | 211001T191452Z | Neil |
| 066 | C clang | 210915T225152Z | jdt |
| 051 | Ruby | 210917T075814Z | G B |
| 020 | Pari/GP | 210917T031828Z | alephalp |
| 006 | 05AB1E | 210917T080012Z | Kevin Cr |
| 013 | BQN | 210916T054042Z | frasiyav |
| 009 | APL Dyalog Unicode | 210915T214154Z | user |
| 026 | Haskell | 210916T122617Z | Wheat Wi |
| 044 | Perl 5 | 210915T222854Z | Kjetil S |
| 046 | JavaScript ES6 | 210915T210220Z | Arnauld |
| 066 | Python 3 | 210916T100136Z | Jitse |
| 069 | Python 3.8 prerelease | 210916T063945Z | wasif |
| 043 | R | 210916T043339Z | pajonk |
| 048 | Desmos | 210915T235642Z | Aiden Ch |
| 006 | Vyxal r | 210916T022151Z | okie |
| 047 | Factor + math.combinatorics | 210915T231621Z | chunes |
| 006 | Jelly | 210915T200501Z | caird co |
APL(NARS), 18 chars
{⍵≡{⍵!⍨0..⍵}¯1+≢⍵}
{⍵!⍨0..⍵}n return the n+1, Pascal triangle line (a!b is binomial function).
{⍵!⍨0..⍵}3
1 3 3 1
that has n+1 elements.
test:
f←{⍵≡{⍵!⍨0..⍵}¯1+≢⍵}
f 1 2 1
1
f ,1
1
f 1 2 3
0
f 1 3 3 1
1
f 1 1
1
f 2 1
0
Pip, 20 14 13 bytes
Lgl+:lPE!ll=g
Takes the list of numbers as separate command-line arguments. Attempt This Online!
Explanation
We generate the nth row of Pascal's triangle, where n is the length of the input list; then we output truthy if the row and the input are equal, falsey otherwise. Heavily based on my answer to Generate Pascal's triangle, which I recommend you read for a better explanation of the core algorithm.
g is list of cmdline args; l is empty list
L Loop
g len(g) times:
!l 1 if l is empty, 0 otherwise
lPE l with that value prepended
l+: Add to l itemwise and assign back to l
l=g 1 if l equals g, 0 otherwise
Uiua, 23 characters
/×=≡(÷∩/×⊃(+1)-⇡):⊢⇌.°⊏
It's just the binomial coefficient again, though I didn't realize at first.
Uiua, 14 characters
≡°0°1°⊂?⍥\--1⧻.
My attempt at the scan approach inspired by @Wheat Wizard's Haskell Answer. (error for no, nothing for yes)
I don't know whether this is sufficient, but propably. Even though this abuses the given first value in the input as a 1 from the row above, this is fine because the value in the first position never changes during the process and must be 1 in the end.
Pascal (FPC), 165 147 bytes
function f(a:array of word):word;var n,i,c:word;begin
n:=high(a)+1;c:=1;f:=1;for i:=1 to n do begin
if a[i-1]<>c then f:=0;c:=c*(n-i)div i;end;end;
Pyt, 14 bytes
ĐŁř⁻ĐŁĐ⑴*⁻⇹ć=Π
implicit input (of length n)
ĐŁř⁻ push [0,1,...,n-1]
ĐŁĐ⑴*⁻ push [n-1,n-1,...,n-1] (of length n)
⇹ć swap and calculate nCr element-wise
= check for equality element-wise
Π take product (1 if all true, 0 otherwise); implicit print
Wolfram Language (Mathematica), 30 bytes
(i=-1;r=++i&/@#)!#==i!/(i-r)!&
Wolfram Language (Mathematica), 38 36 bytes
FromDigits[#,i=-1;r=++i&/@#]==++r^i&
Checks that the polynomial of degree \$n\$ in \$x\$ with these coefficients is equal to \$(x+1)^n\$ by testing it at \$0,...,n\$.
JavaScript (Node.js), 42 bytes
a=>a+-1==a.map((v,i)=>n=i?n*a[1]--/i:1)+-n
From Arnauld's. Removing - Fails 1,11,55,165,330,462,462,330,165,55,111
JavaScript (Node.js), 44 bytes
f=a=>(t=a.map(n=>g=n-g,g=0)).pop()?a==1:f(t)
Mine.
Octave, 40 bytes
@(x)all(bincoeff((n=sum(x|1)-1),0:n)==x)
Port of @cairdCoinheringaahing's Jelly answer.
Based on the bincoeff function that outputs \$n^{th}-1\$ line of the triangle.
Julia, 47 38 34 bytes
!l=binomial.((n=[l;0][2];),0:n)==l
Explanation
We just generate the n-th row of the Pascal triangle and test equality.
Thanks to amelies, we can get it down to 38 bytes!
Thanks to MarkMush, we can get it further down to 34 bytes!
Lean, 187 176 143 131 bytes
def q:list ℕ→bool:=λx,by{induction x,exact[],apply(::)1,induction x_ih with h t i,exact[],cases t,exact[h],exact(h+t_hd)::i}=x
A simpler version of this can be found below:
Lean, 190 185 169 bytes
def f:ℕ→list ℕ:=λn,by{induction n,exact[],apply(::)1,induction n_ih with h t i,exact[],cases t,exact[h],exact(h+t_hd)::i}
def g:list ℕ→bool:=λx,f(x.length)=x
This version defines a helper function f which gives the \$n\$th row of pascal's triangle. From here we check if the input is equal to the \$m\$th row where \$m\$ is the length of the input.
This is the straightforward way to do things.
The shorter version rolls these two into one. Instead of doing induction on the length of the list, it just does induction on the list itself. This turns the already hairy induction into a real mess.
Python 3.8 (pre-release), 71 68 bytes
lambda l:[*map(math.comb,(a:=len(l))*[a-1],range(a))]==l
import math
Using some list expansion shenanigans, it can be slightly shorter.
Old answer:
lambda l:list(map(math.comb,(a:=len(l))*[a-1],range(a)))==l
import math
Happily, Python has a built-in combination function. This just uses a simple mapping of that to build the row of Pascal's triangle corresponding to the length of the input list and compares that to the input.
Charcoal, 20 bytes
⬤θ⎇κ⁼×⁻Lθκ§θ⊖κ×κι⁼¹ι
Try it online! Link is to verbose version of code. Outputs a Charcoal boolean, i.e. - if it's a row, nothing if not. Explanation: The first element must always be 1. Subsequent elements obey the recurrence relation that the previous element multiplied by the inclusive number of elements remaining equals the current element multiplied by its index.
θ Input array
⬤ Do all elements satisfy
⎇κ If not first element then
§θ⊖κ Previous element
× Multiplied by
⁻Lθκ Number of elements remaining
⁼ Equals
×κι Current element multiplied by its index
⁼¹ι If first element then it is `1`
Implicitly print
C (clang), 66 bytes
i,c,t;f(*p,n){for(t=c=i=1;i<=n;c=c*(n-i)/i++)t=t&&p[i-1]==c;*p=t;}
C (clang), 55 bytes
i,c;f(*p,n){for(c=i=1;i<=n;c=c*(n-i)/i++)c/=p[i-1]==c;}
Inspired by Wheat Wizards' post. Triggers a floating point exception for falsey.
Pari/GP, 20 bytes
a->a==binomial(#a-1)
When binomial takes only one argument, it returns the n'th row of the Pascal's triangle.
05AB1E, 6 bytes
g<DÝcQ
Port of @cairdCoinheringaahing's Jelly answer.
Try it online or verify all test cases.
Explanation:
g # Get the length of the (implicit) input-list
< # Decrease it by 1
D # Duplicate it
Ý # Pop the copy, and push a list in the range [0,length-1]
c # Get the binomial coefficient of length-1 with each integer in this list
Q # Check if this list is now equal to the (implicit) input-list
# (after which the result is output implicitly)
BQN, 15 13 bytesSBCS
-2 bytes thanks to Marshall Lochbaum
-˜`⍟≠≡1‿¯1⥊˜≠
Unlike many array languages, BQN doesn't have a binomial primitive, so a more creative solution is required.
-˜`⍟≠≡1‿¯1⥊˜≠ #
-˜` # swapped minus scan
⍟≠ # repeated input length number of times
≡ # matches
1‿¯1⥊˜≠ # alternating 1,-1 .. extended to the input length
APL (Dyalog Unicode), 11 9 bytes
Saved 2 bytes thanks to Bubbler
⊢≡⍳∘≢!≢-≡
Requires 0-indexing.
⊢≡⍳∘≢!≢-≡
⊢ ⍝ Does the input
≡ ⍝ equal
⍳∘≢!≢-≡ ⍝ the row of Pascal's triangle with the same length?
≢-≡ ⍝ The number r of the real row that has the same length
≢ ⍝ The length of the input
-≡ ⍝ Decremented/Minus the depth (which is always one)
⍳∘≢ ⍝ The column numbers for that row (a range [0..r])
! ⍝ Get the number at row r and column c for each element in the previous range
Haskell, 36 31 26 bytes
f[1]=1
f(1:b)=f$scanr1(-)b
This version errors as a false indicator and returns 1 as the true indicator. It's short but I generally find these sorts of answers a little cheaty so below I have a version with a more traditional output method:
42 37 32 bytes
f[1]=1
f(1:b)=f$scanr1(-)b
f _=0
Outputs 1 for yes and 0 for no.
This is I believe the only answer here using this method. We use scanr1(-) to calculate what the layer above would have to be in order to produce the input, and check if that new smaller layer holds. If we encounter [1] we halt with yes, because that is the first layer of Pascal's triangle. And if we encounter something starting with something other than 1 we halt with no.
Perl 5, 45 44 bytes
sub{$n=$q=1;!grep$_-$n|($n*=@_/$q++-1)*0,@_}
A translation of the JavaScript answer from @Arnauld.
JavaScript (ES6), 46 bytes
Returns a Boolean value.
a=>!a.some(v=>v-n|(n*=a.length/q++-1)-n,q=n=1)
Python 3, 66 bytes
f=lambda a,*r:r<a[1:]and f(a,*map(sum,zip(r,(1,*r))),1)or(1,*r)==a
Constructs the rows of Pascal's triangle recursively until a match is found or the generated row is larger than the input.
Python 3.8 (pre-release), 69 bytes
lambda l:[math.comb(~-len(l),x)for x in range(len(l))]==l
import math
Luckily python 3.8 has a built-in for n choose k
R, 43 bytes
Or R>=4.1, 36 bytes by replacing the word function by \.
function(r)any(r-choose(x<-sum(r|1)-1,0:x))
Taking also length of input as second argument, we can shave 6 bytes off: Try it online!.
Desmos, 62 48 bytes
Thanks Bubbler for -14 bytes.
The function \$f(l)\$ takes in a list of numbers, and returns 0 for falsey, 1 for truthy.
a=length(l)-1
f(l)=\min(\{l=\nCr(a,[0...a]),0\})
Vyxal r, 6 bytes
₌ẏL‹ƈ⁼
vyxal (and lyxal) is the best?
₌ẏL‹ƈ⁼
(implicit input)
₌ parallel stuffs, do the next two command both on the stack top
ẏ range of len(top)
L len(top) currently stack [range(0,len),len]
‹ -1 len-1
ƈ do combination caculation C(i,len-1) i= range(0,len)
⁼ check equal stack are not enough so input is used
Factor + math.combinatorics, 47 bytes
[ dup length 1 - dup [0,b] [ nCk ] with map = ]
Explanation:
It's a quotation (anonymous function) that takes a sequence of numbers from the data stack as input and leaves a boolean on the data stack as output. Assuming { 1 3 3 1 } is on the data stack when this quotation is called...
dupDuplicate the top object on the data stack.Stack:
{ 1 3 3 1 } { 1 3 3 1 }lengthGet the length of a sequence.Stack:
{ 1 3 3 1 } 41 -Subtract one.Stack:
{ 1 3 3 1 } 3dupDuplicate the top object on the data stack.Stack:
{ 1 3 3 1 } 3 3[0,b]Create a range from 0 to whatever is on top of the data stack, inclusive.Stack:
{ 1 3 3 1 } 3 T{ range f 0 4 1 }[ nCk ] with mapTake \$\binom{3}{0}\$, \$\binom{3}{1}\$, ... \$\binom{3}{3}\$Stack:
{ 1 3 3 1 } { 1 3 3 1 }=Are the top two objects on the data stack equal?Stack:
t
Jelly, 6 bytes
L’cŻ$⁼
As each row of Pascal's triangle has a unique length \$n\$, all we have to do is reconstruct the row, given its length, and check if it equals the original input. As each row is given by \$\binom{n-1}{i}, 0 \le i < n\$, we just calculate that (as Jelly has a 1 byte for binomial coefficient)
In fact, if we can take the length of the input as a second input, we can get 5 bytes
How it works
L’cŻ$⁼ - Main link. Takes a list L on the left
L - Length of L
’ - Decrement
$ - Last 2 links as a monad f(len-1):
Ż - Range from 0 to len-1
c - Binomial coefficient of len-1 and each element in the range
⁼ - Does that equal the input?