# Is it a row of Pascal's triangle?

Pascal's triangle is a triangular diagram where the values of two numbers added together produce the one below them.

This is the start of it:

    1
1 1
1 2 1
1 3 3 1
1 4 6 4 1


You can see that the outside is all 1s, and each number is the sum of the two above it. This continues forever.

Your challenge is to check whether a non-empty array of positive integers is a row of Pascal's triangle, somewhere down the line.

You should output two distinct values for truthy and falsy.

## Scoring

This is , shortest wins.

## Testcases

Truthy:

[1]
[1,2,1]
[1,3,3,1]
[1,5,10,10,5,1]
[1,6,15,20,15,6,1]
[1,7,21,35,35,21,7,1]


Falsy:

[2]
[1,2]
[1,1,2]
[2,2,2,2]
[1,2,10,2,1]
[1,2,3,4,5,6]
[1,3,5,10,5,3,1]

• Can we take the length of the input as input as well? Sep 15, 2021 at 20:09
• Darn you for preventing sum of n = 2^n. 😂 Sep 15, 2021 at 21:06
• @DLosc No, you don't. Sep 20, 2021 at 3:59

f[1]=1
f(1:b)=f$scanr1(-)b  Try it online! 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: # 4237 32 bytes f[1]=1 f(1:b)=f$scanr1(-)b
f _=0


Try it online!

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.

# Jelly, 6 bytes

L’cŻ$⁼  Try it online! 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

# 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.

Try it online!

# Ruby, 51 bytes

->l{l.all?{r=0;*l,r=l.map{|x|r=x-r};r==(l[0]?0:1)}}


Try it online!

# Pip-x, 20 14 bytes

L#al+:lPE!ll=a


Try it here! TIO doesn't support the -x flag, but you can simulate it using the header: Try it 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.

                a is 1st cmdline arg, evaluated (-x flag); l is empty list
L               Loop
#a              len(a) 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=a  1 if l equals a, 0 otherwise


# Python 3.8 (pre-release), 71 68 bytes

lambda l:[*map(math.comb,(a:=len(l))*[a-1],range(a))]==l
import math


Try it online!

Using some list expansion shenanigans, it can be slightly shorter.

lambda l:list(map(math.comb,(a:=len(l))*[a-1],range(a)))==l
import math


Try it online!

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.

# 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\})


Try It On Desmos!

Try It On Desmos! - Prettified

• You could use "minimum" on the second one if Desmos has such a built-in. Sep 16, 2021 at 0:32
• @Bubbler Why didn't think about that... Sep 16, 2021 at 3:04

# Charcoal, 20 bytes

⬤θ⎇κ⁼×⁻Ｌθκ§θ⊖κ×κι⁼¹ι


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
⁻Ｌθκ              Number of elements remaining
⁼                   Equals
×κι       Current element multiplied by its index
⁼¹ι    If first element then it is 1
Implicitly print
`