37
\$\begingroup\$

Input a non-empty array with \$n\$ positive integers. Test if the input array contains every integer in \$1\cdots n\$.

In case you prefer 0-indexed numbers, you may choose to input an array of non-negative integers, and test if the input array contains every integer in \$0\cdots (n-1)\$ instead. All testcases and formula listed below use 1-index. You may need to adjust them if you choose this option.

Input / Output

Input is an array \$A\$ with \$n\$ positive integers:

$$ A = \left[A_1,\dots,A_n\right] $$ $$ \forall i \in \left[1,\dots,n\right]: A_i>0 $$

Output if input \$A\$ satisfies:

$$ \forall i \in \left[1,\dots,n\right]: i \in A $$

Output would be two distinct values, or truthy vs falsy values (swap meaning of truthy / falsy is allowed).

Rules

  • This is , shortest code wins. And since this is code-golf, don't worry about time / memory complexity of your code. You may even timeout on TIO as long as your program works when giving it more time to run.

Testcases

Truthy

1
1,2
2,1
1,3,2
3,1,2
1,2,3,4,5,6,7,8,9,10,11,12
12,11,10,9,8,7,6,5,4,3,2,1
6,3,8,12,1,10,4,2,7,9,5,11
16,37,14,15,23,8,29,35,21,6,5,34,38,9,36,26,24,32,28,7,20,33,39,12,30,27,40,22,11,41,42,1,10,19,2,25,17,13,3,18,31,4

Falsy

2
12
1,1
1,3
2,3
3,3
2,1,3,2
1,4,3,1
4,1,2,4
1,2,2,5,5
1,3,3,3,5
8,7,5,3,4,1,6
5,7,1,4,6,1,8,3
6,3,5,4,7,1,8,1,2
6,5,3,8,2,7,9,4
1,1,1,1,1,1,1,1
1,5,9,13,11,7,3
14,6,12,4,10,8,16,2
34,33,38,17,35,11,36,31,28,14,6,15,18,2,19,40,29,41,9,1,27,23,20,32,26,25,37,8,13,30,39,7,5,3,21,4,11,16,10,22,12,24
38,27,20,23,31,6,2,24,21,31,33,7,26,12,14,17,3,2,28,31,5,23,28,27,37,32,7,39,22,6,35,42,19,3,35,17,35,40,22,13,27,7
\$\endgroup\$
10
  • 1
    \$\begingroup\$ is empty list falsy? \$\endgroup\$
    – Razetime
    Jun 24, 2021 at 6:45
  • 8
    \$\begingroup\$ @Razetime IMO, empty list should be truthy. But anyway, empty list is excluded from testcases, and it is undefined behavior to your program. \$\endgroup\$
    – tsh
    Jun 24, 2021 at 6:48
  • \$\begingroup\$ How do you feel about restricting to n<10? It would make the elements to check finite and all single digit. And that would allow lots of wacky tarpits to solve it and not require as high a computational class! I think the spirit would still be preserved, and I can’t think of any way it could be used to hardcode or “cheat” that wouldn’t just be longer than doing it “right,” for the languages that can do it. \$\endgroup\$
    – AviFS
    Jun 24, 2021 at 8:20
  • 4
    \$\begingroup\$ @AviFS If your language doesn't support decimal number I/O, you can use character value I/O. If it doesn't support that either, you can take it in binary or unary. Computational class is not a problem; taking lots of time or memory is allowed by default (even if it can't be run to completion realistically on any machine). You're even allowed to handwave the inputs exceeding the limit of the built-in integer representation. So I don't see any extra benefit of restricting the input size to <10. \$\endgroup\$
    – Bubbler
    Jun 24, 2021 at 9:13
  • 1
    \$\begingroup\$ Would outputting 0 for one result or any other integer for the other be acceptable? \$\endgroup\$
    – Shaggy
    Jun 24, 2021 at 14:03

65 Answers 65

16
\$\begingroup\$

Risky, 7 bytes 7 bytes and a palindrome

1_??!0:0!??_1

Try it online!

Explanation

1   sort
_
?         input
?     filter by
!       factorial
0         0
: =
0   range
!       length
?         input
?     find first number such that
_
1         1
\$\endgroup\$
15
\$\begingroup\$

J, 7 bytes

-:/:@/:

Try it online!

Ninja'd by hyper-neutrino :(

Takes zero-based input. Applying Grade Up twice to a vector gives a "ranking" of each element, so that 0 is the smallest, 1 is the next smallest, ... up to n-1, breaking ties by giving a smaller number to the one appearing first. A vector is a permutation if and only if the ranking is identical to itself.

J, 7 bytes

2|C.!.2

Try it online!

Takes zero-based input. C.!.2 is a built-in for "cycle parity". It gives 1 if a permutation has even number of swaps, -1 if odd, and 0 if not a valid permutation. I take it modulo 2 to convert all nonzero results to 1 (the only truthy value in J).

Even if error/non-error output were allowed, it is too bad that permutation-related built-ins A. and C. don't error on all possible non-permutations.

J, 7 bytes

#\-:/:~

Try it online!

Takes one-based input. #\ is a golfing trick for getting 1..n, which is compared to the sorted array /:~ of the original.


If output by erroring/non-erroring is allowed:

J, 3 bytes

C.~

Try it online!

Takes zero-based input. Abuses dyadic form of C., whose left argument must strictly be a permutation. It throws an index error otherwise.

\$\endgroup\$
13
\$\begingroup\$

APL (Dyalog Unicode), 5 bytes

⊢≡⍋∘⍋

Try it online!

-4 bytes thanks to Bubbler by turning this into a train and via using ⍵≡ to match the whole array instead of ∧/⍵= to match element-wise and then reduce by AND.

⊢≡⍋∘⍋    Train
 ≡         Check equality between
⊢         (Right argument ⍵) and
  ⍋∘⍋    Composed function {⍋⍋⍵}; grade up twice

Grading up twice gives the minimum permutation of length X that has the same (non-strict) ordering as the original - so, if the permutation of the right order is the same as the original, then the original was that permutation and thus a permutation.

\$\endgroup\$
4
  • \$\begingroup\$ You can delete ∧/ and change = (element-wise equals) to (entire array match). The train version is ⊢≡⍋∘⍋. \$\endgroup\$
    – Bubbler
    Jun 24, 2021 at 6:39
  • \$\begingroup\$ @Bubbler Ah, I should've looked for that built-in instead of using ∧/= :P thanks! \$\endgroup\$
    – hyper-neutrino
    Jun 24, 2021 at 6:40
  • \$\begingroup\$ May I use this for APLcart? (This entails relinquishing all rights to the code.) \$\endgroup\$
    – Adám
    Jun 24, 2021 at 7:01
  • 2
    \$\begingroup\$ @Adám Sure, that's cool with me and I checked with Bubbler too :) \$\endgroup\$
    – hyper-neutrino
    Jun 24, 2021 at 14:54
12
\$\begingroup\$

Haskell (1 indexed), 29 bytes

Courtesy of AZTECCO

g l=all(`elem`l)[1..length l]

Try it online!

Haskell (0 indexed), 42 32 31 bytes

Lynn saved one byte with the pointfree solution.

all.(.fst).flip elem<*>zip[0..]

Try it online!

I tried a bunch of creative stuff with scans, folds and zips, but the boring solution ended up being the shortest.

Both of these check that every integer in the available range is in the list. The first is 1 to the length of the list, the second is 0 to 1 less than the length of the list.

\$\endgroup\$
8
  • \$\begingroup\$ There's a shorter solution without import :) \$\endgroup\$
    – xnor
    Jun 24, 2021 at 12:58
  • \$\begingroup\$ @xnor Ah indeed. And much shorter. \$\endgroup\$
    – Wheat Wizard
    Jun 24, 2021 at 13:04
  • \$\begingroup\$ With sorting, this pointfree saves a byte, using flipped output: Try it online! \$\endgroup\$
    – xnor
    Jun 24, 2021 at 13:07
  • 1
    \$\begingroup\$ Looks like all.(.fst).flip elem<*>zip[0..] is a hair shorter. \$\endgroup\$
    – Lynn
    Jun 24, 2021 at 16:37
  • 2
    \$\begingroup\$ If I'm not missing something..Try it online! \$\endgroup\$
    – AZTECCO
    Jun 24, 2021 at 17:55
11
\$\begingroup\$

Python 3, 31 bytes

Input is \$ 0 \$-indexed.

lambda a:{*a}=={*range(len(a))}

Try it online!

This is the most obvious implementation I could think of. It compares the set of \$ a \$ to the set of numbers from 0 to len(a)-1.

Python 3, 30 bytes

As @ovs suggested, we can save a byte by switching the definition of truthy and falsey. A valid permutation returns a falsey value, and truthy otherwise.

lambda a:{*a}^{*range(len(a))}

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ If you swap truthy and falsy outputs (as allowed in the post), the == can be a ^. \$\endgroup\$
    – ovs
    Jun 24, 2021 at 8:12
  • \$\begingroup\$ This answer is old, but I am pretty sure this would fail for many inputs with duplicated values, e.g [1,2,1] \$\endgroup\$
    – des54321
    Apr 16, 2022 at 4:17
  • \$\begingroup\$ @des54321 It does? I think it correctly identifies [1,2,1] as False. \$\endgroup\$ Apr 16, 2022 at 5:03
  • \$\begingroup\$ @dingledooper never mind actually, youre correct, my mental analysis missed that the len(a) bit handled checking for non-unique values in the input \$\endgroup\$
    – des54321
    Apr 16, 2022 at 5:08
10
\$\begingroup\$

JavaScript (ES6), 30 bytes

Expects 0-indexed values. Returns a Boolean value.

a=>a.every(x=>(a[~x]^=1)/a[x])

Try it online!

Commented

We test whether all values are less than the length \$N\$ of the input array and make sure that there is no duplicate. Both conditions are satisfied iff the array consists of all values from \$0\$ to \$N-1\$.

a =>             // a[] = input array
  a.every(x =>   // For each value x in a[]:
    (a[~x] ^= 1) //   ~x is -x - 1, which is guaranteed to be negative
                 //   Therefore, a[~x] is a property (such as '-1') of
                 //   the underlying object of a[], which can be safely
                 //   used to store values that were already encountered
                 //   If a[~x] XOR 1 is not equal to 1, we have a
                 //   duplicate value
    / a[x]       //   We also make sure that a[x] is defined, which
                 //   means that x < a.length
                 //   The possible cases are:
                 //     1 / n, n > 0 is a truthy number  \__ success
                 //     1 / 0 is +Infinity (also truthy) /
                 //     1 / undefined is NaN (falsy)     \
                 //     0 / n, n > 0 is 0 (falsy)         |_ failed
                 //     0 / 0 is NaN (falsy)              |
                 //     0 / undefined is NaN (falsy)     /
  )              // End of every()
\$\endgroup\$
10
\$\begingroup\$

Julia 1.5, 6 bytes

This is a built-in, so it's quite easy for Julia

isperm

Use it like this: isperm([6,3,8,12,1,10,4,2,7,9,5,11])

\$\endgroup\$
1
  • 3
    \$\begingroup\$ That's almost Mathematica-level surprising! \$\endgroup\$
    – ojdo
    Jun 25, 2021 at 8:22
9
\$\begingroup\$

Zsh, 18 12 11 bytes

>$@<{1..$#}

Attempt This Online!

Outputs via exit code: 0 for a permutation and 1 for not a permutation

Explanation:

  • >: create files called
    • $@: each command-line argument
  • {1..$#}: range from 1 to the number of command-line arguments
  • <: try to read from each of those numbers as a file

If any command fails, then that means one of the files in the range didn't exist, so it's not a valid permutation, and zsh will exit with status 1.

\$\endgroup\$
8
\$\begingroup\$

R, 26 24 bytes

-2 bytes thanks to pajonk.

all(seq(x<-scan())%in%x)

Try it online!

If input is of length at least 2, seq(x) outputs the integers from 1 to length(x); if input is of length 1, it outputs the integers from 1 to x. Return TRUE iff all those integers are elements of x.

\$\endgroup\$
3
  • \$\begingroup\$ I don't think the ! is necessary here. \$\endgroup\$
    – pajonk
    Jun 24, 2021 at 7:58
  • \$\begingroup\$ @pajonk Indeed, thanks! \$\endgroup\$ Jun 24, 2021 at 8:01
  • \$\begingroup\$ And now we can do all(seq(x<-scan())%in%x) \$\endgroup\$
    – pajonk
    Jun 24, 2021 at 8:02
7
\$\begingroup\$

Ruby, 21 bytes

->a{a|[*1..a.max]==a}

Try it online!

-1 byte thanks to ovs!

-3 bytes thanks to Razetime!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I'm not completely sure, but ->a{a|[*1..a.max]==a} might work. \$\endgroup\$
    – ovs
    Jun 24, 2021 at 8:19
  • \$\begingroup\$ @ovs seems to be working! \$\endgroup\$
    – user100752
    Jun 24, 2021 at 8:20
7
\$\begingroup\$

Factor + math.unicode, 24 23 bytes

[ dup length iota ⊃ ]

Try it online!

0-indexed to save a byte. Is the input a superset of \$[0..|input|)\$?

\$\endgroup\$
6
\$\begingroup\$

Python 3, 33 bytes

lambda x:max(*x,len(x))^len({*x})
  1. Check uniqueness: len(x) = len(set(x))
  2. For unique set: max(x) == len(x) iff x = 1...n

Try it online!

\$\endgroup\$
6
\$\begingroup\$

Zsh, 13 bytes

: >$@<{1..$#}

Try it online!

:              noop command to prevent hanging while waiting for input
  >$@          create a file for every argument
     <{1..$#}  read every file between 1 and the number of arguments

Since the redirects are handled left-to-right, we create the files before trying to read them. If we encounter a number for which no file exists (for example on zsh x.zsh 4 1 2), we get the following error:

no such file or directory: 3

and receive exit code 1. If it was indeed a permutation, we exit cleanly with code 0.

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to Code Golf, and nice first answer! I've edited your answer slightly to include a link to Try it online! so that others can test your solution. Don't worry about not being able to comment, we encourage new users to post solutions, even if they're golfs of current ones :) \$\endgroup\$ Jun 25, 2021 at 12:16
5
\$\begingroup\$

Jelly, 3 bytes

Ṣ⁼J

Try it online!

Ṣ⁼J   Monadic Link
Ṣ     1-chain: sort the input
 ⁼J   2,1-chain: is (the input sorted) equal to (range on the length of the input)?
\$\endgroup\$
5
\$\begingroup\$

Brachylog, 3 bytes

o~⟦

Try it online!

0-indexed. Succeeds or fails.

o      The input sorted
 ~⟦    is the range from 0 to something inclusive.

Note that this can not be golfed to ⟦p with reversed input, as it fails to terminate for cases it should reject:

⟦     Choose an integer. Is the range from 0 to it inclusive
 p    a permutation of the input?
\$\endgroup\$
5
\$\begingroup\$

Pyth, 3 bytes

UIS

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Wow... I had a 6 byter, had no idea U had that use. Nice! \$\endgroup\$
    – hakr14
    Jun 24, 2021 at 17:19
5
\$\begingroup\$

R, 27 bytes

any(rank(x<-scan(),,'f')-x)

Try it online!

Returns FALSE for Truthy, TRUE for FALSY (just to shave one byte)

Explanation:

If the array containing the rank of each value is equal to the input array, then it's a permutation of 1..n

Note:
5 bytes are wasted because in rank function the default handling strategy in case of ties is "average their ranks" instead of any of the other possibilities... argh!

\$\endgroup\$
4
  • \$\begingroup\$ -1: any(rank(x<-scan())-x) swapping thruthy and falsy. (TIO link too long for the comment) \$\endgroup\$
    – pajonk
    Jun 24, 2021 at 7:54
  • 1
    \$\begingroup\$ @pajonk: I was just changing it while you were posting your comment ! :D \$\endgroup\$
    – digEmAll
    Jun 24, 2021 at 7:55
  • 1
    \$\begingroup\$ I think this fails on input 1,3,3,3,5. \$\endgroup\$ Jun 24, 2021 at 7:57
  • \$\begingroup\$ @RobinRyder: you're right... damn, I always forget that rank default for ties is avg... \$\endgroup\$
    – digEmAll
    Jun 24, 2021 at 8:02
4
\$\begingroup\$

Perl 5, 38 bytes

sub{!grep!{map+($_,1),@_}->{$_},1..@_}

Try it online!

\$\endgroup\$
2
4
\$\begingroup\$

Red, 46 bytes

func[a][i: 0 until[alter a i: 1 + i]single? a]

Try it online!

increments a counter as long as the counter is found in the list, and then checks it against the length.

1 = length? is used as a polyfill for single? in the latest Red version.

-5 bytes from tsh.

-5 bytes from Galen Ivanov.

\$\endgroup\$
5
  • \$\begingroup\$ Maybe i >(length? a)? \$\endgroup\$
    – tsh
    Jun 24, 2021 at 7:00
  • \$\begingroup\$ yes, it works. Thanks. \$\endgroup\$
    – Razetime
    Jun 24, 2021 at 7:16
  • \$\begingroup\$ Once again, alter (plus reversal of logic) helps to get rid of 5 bytes: func[a][i: 0 until[alter a i: 1 + i]single? a]. (single? doesn't work in TIO, must be replaced by 1 = lenght? a) \$\endgroup\$ Jun 24, 2021 at 18:18
  • 1
    \$\begingroup\$ only one more answer for an alter hattrick :P \$\endgroup\$
    – Razetime
    Jun 25, 2021 at 3:46
  • \$\begingroup\$ @Razetime haha, yes! \$\endgroup\$ Jun 25, 2021 at 6:42
4
\$\begingroup\$

Java (JDK), 43 bytes

a->a.sorted().reduce(1,(x,y)->y==x?x+1:0)>0

Try it online!

Explanations

This answer is a Predicate<IntStream> and requires that the input contains no 0.

First this code sorts the input stream. The reduce method then makes sure that each consecutive number is encountered, and return the last encountered number. x always contains the next expected number, or 0 if we got an unexpected number. 1 is the first parameter of the reduce method as it's the first expected number. Given that 0, or any negative number, is not allowed in the IntStream, when 0 is returned for the first time, it will always be returned for the remaining of the process.

\$\endgroup\$
0
4
\$\begingroup\$

TypeScript's Type System, 450 442 bytes

type A<T extends any[]>=T extends{length:infer L}?(L extends number?L:never):never
type B<L extends number,T extends 0[]=[]>=T extends{length:L}?T:B<L,[...T,0]>
type C<N extends number,T extends number=1,R extends any[] = []> = R['length']extends N?R:C<N,A<[...B<T>,0]>,[...R,T]>
type D<T extends any[],U extends any[]>=T extends[]?true:T extends[infer H,...infer R]?H extends U[number]?D<R,U>:false:never
type F<T extends any[]>=D<T,C<A<T>>>

TypeScript Playground Link

Explanation

// This allows getting the Length of any tuple via inferring its L property
type A<T extends any[]>=T extends{length:infer L}?(L extends number?L:never):never

// This creates a tuple of arbitrary length L (we need to use this later for arithmetic)
type B<L extends number,T extends 0[]=[]>=T extends{length:L}?T:B<L,[...T,0]>

// We can use this to generate an incrementing tuple of length L (e.g. [1, 2, 3, 4]). We use this to compare against the final array.
type C<N extends number,T extends number=1,R extends any[] = []> = R['length']extends N?R:C<N,A<[...B<T>,0]>,[...R,T]>

// This checks if two arrays have the same elements inside them 
type D<T extends unknown[],U extends unknown[]>=T extends[]?true:T extends[infer H,...infer R]?H extends U[number]?D<R,U>:false:never

// Finally, we can use this to compare the chosen array T vs the increasing tuple N
type F<T extends any[]>=D<T,C<A<T>>>
\$\endgroup\$
1
  • \$\begingroup\$ A can probably just be T["length"], which can then be inlined. \$\endgroup\$
    – noodle man
    May 12 at 11:21
3
\$\begingroup\$

Wolfram Language (Mathematica), 22 20 bytes

Sort@#==Range@Max@#&

-2 bytes, thanks to @ZaMoC:

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ 20 bytes \$\endgroup\$
    – ZaMoC
    Jun 24, 2021 at 8:22
3
\$\begingroup\$

Stax, 5 bytes

c%R|}

Run and debug it

Explanation:

     ; Implicit input onto top of stack
c    ; Copy top of stack
%    ; Length of top of stack
R    ; Make range [1 .. n]
|}   ; Compare top two elements of stack. Arrays are different orderings of same elements?
     ; Implicit output of truthy/falsy

Could probably save a byte somewhere.

\$\endgroup\$
1
  • \$\begingroup\$ oc%R- exists, but i don't think there's away to get this to pack. \$\endgroup\$
    – Razetime
    Jun 29, 2021 at 5:12
3
\$\begingroup\$

Nibbles, 1.5 bytes (3 nibbles)

-,,

Returns an empty list (falsy) if input is a permutation of 1..n, or a non-empty list (truthy) if it is not.

-       # remove 
 ,      #   sequence of 1..
  ,     #   length of
        #   (implicitly) input
        # from
        #   (implicitly) input

enter image description here

\$\endgroup\$
3
\$\begingroup\$

Python, 43 39 Bytes

lambda x:sorted(x)==list(range(len(x)))

0-indexed. Uses sorted(), range() and list(). Surprisingly gives correct answer for empty list.

Test on TIO.

\$\endgroup\$
8
  • \$\begingroup\$ You can remove the space between len(x)) and for y \$\endgroup\$
    – mousetail
    May 13 at 12:57
  • \$\begingroup\$ You can also remove the square brackets inside all, a generator will work there, it doesn't need to be a list \$\endgroup\$
    – mousetail
    May 13 at 12:58
  • \$\begingroup\$ Also the answer is invalid: tio.run/##K6gsycjPM/… Returns true on many of the falsy test cases \$\endgroup\$
    – mousetail
    May 13 at 13:00
  • \$\begingroup\$ @mousetail fixed \$\endgroup\$ May 14 at 8:57
  • \$\begingroup\$ Smart solution, well done \$\endgroup\$
    – mousetail
    May 14 at 11:08
3
\$\begingroup\$

Python, 39 Bytes

lambda x:sorted(x)==list(range(len(x)))

I am an account owned by PlaceReporter99. I am trying to get 20 rep so that I can access the chat rooms.

\$\endgroup\$
1
2
\$\begingroup\$

Charcoal, 6 bytes

¬⁻Eθκθ

Try it online! Link is to verbose version of code. 0-indexed. Outputs a Charcoal boolean, i.e. - for permutation, nothing if not. Explanation:

   θ    Input array
  E     Map over elements
    κ   Current index
 ⁻      Remove values found in
     θ  Input array
¬       Check that nothing is left
        Implicitly print

(Mapping over the input indices is golfier than creating a range.)

\$\endgroup\$
2
\$\begingroup\$

JavaScript (Node.js), 40 38 bytes

s=>s.sort((a,b)=>a-b).some((x,i)=>x^i)

Try it online!

Returns true for invalid and false for valid. Put a ! before the s.sort for the otherway round. Takes input 0-indexed.

\$\endgroup\$
5
  • \$\begingroup\$ 0-indexed is allowed, so you can remove ++. \$\endgroup\$
    – user100690
    Jun 24, 2021 at 7:51
  • \$\begingroup\$ (a,b)=>a-b -> a=>b=>a-b from this tip \$\endgroup\$
    – Razetime
    Jun 24, 2021 at 8:15
  • 1
    \$\begingroup\$ @razetime that wont work for the sort function \$\endgroup\$
    – user100752
    Jun 24, 2021 at 8:18
  • \$\begingroup\$ oh, too bad..... \$\endgroup\$
    – Razetime
    Jun 24, 2021 at 8:19
  • \$\begingroup\$ @RecursiveCo. thanks! \$\endgroup\$
    – emanresu A
    Jun 24, 2021 at 9:02
2
\$\begingroup\$

jq, 20 49 36 bytes

length==max and max==(unique|length)

Try it online!

Thanks to tsh and xigoi for catching mistakes in the previous attempts...

Third time is the charm perhaps?

Since the rules limit the numbers to "positive integers", there's no need to test the minimum. Making sure the largest number in the list, the size of the original list and the size of a de-dup'd list are all the same is sufficient. So that's what this version does.

\$\endgroup\$
4
  • 1
    \$\begingroup\$ Failed on [1,1,1] \$\endgroup\$
    – tsh
    Jun 24, 2021 at 9:25
  • \$\begingroup\$ Re-written to actually do what the challenge called for... \$\endgroup\$
    – cnamejj
    Jun 24, 2021 at 10:10
  • \$\begingroup\$ Maybe [1,3,3]? \$\endgroup\$
    – tsh
    Jun 24, 2021 at 10:39
  • 1
    \$\begingroup\$ I think it should be == instead of -. \$\endgroup\$
    – xigoi
    Jun 24, 2021 at 16:35
2
\$\begingroup\$

Raku, 14 bytes

{$_∖bag ^$_}

0-based.

Try it online!

  • $_ is the list argument to the function.
  • ^$_ is a list of numbers from 0 to one less than the size of the input list.
  • bag ^$_ is a bag (a set with multiplicity) of that list of those sequential numbers.
  • is the set difference operator. The left argument, the input list, is coerced to a bag because the right argument is a bag, from which one instance of each of the sequential numbers is removed. The result will be an empty bag only if the input list contained exactly one number from zero up to one less than its size. An empty bag is falsey in a boolean context, and a non-empty bag is truthy.
\$\endgroup\$
2
  • \$\begingroup\$ Dear lord I've got to learn Raku properly at some point. I'm a Perl 5 golfer, and Raku baffles me half the time. \$\endgroup\$ Jun 25, 2021 at 2:31
  • \$\begingroup\$ @SilvioMayolo I love golfing in Raku, since you rarely need weird tricks to keep the byte count down--the built-ins suffice for so many things. \$\endgroup\$
    – Sean
    Jun 25, 2021 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.