Introduction

In a private chat, a friend of mine apparently recently stumbled across a security system which has the following two restrictions on its valid pins:

  • Each digit must be unique (that is "1" may only appear once)
  • The order of the digits doesn't matter ("1234"="4321")

So to illustrate how bad this padlock system is, let's actually enumerate all valid PINs!

Input

Your input will consist of a single, positive integer, which denotes the length of the PIN.

Output

Your output consists of a list of non-negative integers or strings*, which enumerate all valid PINs of the given length.

*More precisely something that a human can use to try all combinations if you would print it for them. This means set of sets of digits and arrays of arrays of digits are fine.

Who wins?

This is so the shortest answer in bytes wins! Standard rules and loopholes apply.

Corner Cases

  • The output behaviour is undefined if an integer greater than 10 is input.
  • The ordering of the digits within each output entry is undefined, as entries with a zero actually contain said zero, that is you may not strip "0123" to "123" but "1230", "1203" and "1023" are all valid as is "0123".

Test Cases

1
[0,1,2,3,4,5,6,7,8,9]

2
[10,20,30,40,50,60,70,80,90,21,31,41,51,61,71,81,91,32,42,52,62,72,82,92,43,53,63,73,83,93,54,64,74,84,94,65,75,85,95,76,86,96,87,97,98]

3
[210,310,410,510,610,710,810,910,320,420,520,620,720,820,920,430,530,630,730,830,930,540,640,740,840,940,650,750,850,950,760,860,960,870,970,980,321,421,521,621,721,821,921,431,531,631,731,831,931,541,641,741,841,941,651,751,851,951,761,861,961,871,971,981,432,532,632,732,832,932,542,642,742,842,942,652,752,852,952,762,862,962,872,972,982,543,643,743,843,943,653,753,853,953,763,863,963,873,973,983,654,754,854,954,764,864,964,874,974,984,765,865,965,875,975,985,876,976,986,987]

4
[3210,4210,5210,6210,7210,8210,9210,4310,5310,6310,7310,8310,9310,5410,6410,7410,8410,9410,6510,7510,8510,9510,7610,8610,9610,8710,9710,9810,4320,5320,6320,7320,8320,9320,5420,6420,7420,8420,9420,6520,7520,8520,9520,7620,8620,9620,8720,9720,9820,5430,6430,7430,8430,9430,6530,7530,8530,9530,7630,8630,9630,8730,9730,9830,6540,7540,8540,9540,7640,8640,9640,8740,9740,9840,7650,8650,9650,8750,9750,9850,8760,9760,9860,9870,4321,5321,6321,7321,8321,9321,5421,6421,7421,8421,9421,6521,7521,8521,9521,7621,8621,9621,8721,9721,9821,5431,6431,7431,8431,9431,6531,7531,8531,9531,7631,8631,9631,8731,9731,9831,6541,7541,8541,9541,7641,8641,9641,8741,9741,9841,7651,8651,9651,8751,9751,9851,8761,9761,9861,9871,5432,6432,7432,8432,9432,6532,7532,8532,9532,7632,8632,9632,8732,9732,9832,6542,7542,8542,9542,7642,8642,9642,8742,9742,9842,7652,8652,9652,8752,9752,9852,8762,9762,9862,9872,6543,7543,8543,9543,7643,8643,9643,8743,9743,9843,7653,8653,9653,8753,9753,9853,8763,9763,9863,9873,7654,8654,9654,8754,9754,9854,8764,9764,9864,9874,8765,9765,9865,9875,9876]
  • 1
    The input is non-negative - so on input zero, the program should be a list containing one value, the empty string? – aschepler Jun 13 at 1:25
  • @aschepler indeed, the zero case is about as sensical as the 11 case, so I changed that to "positive integer". – SEJPM Jun 13 at 7:37

27 Answers 27

Jelly, 4 bytes

ØDœc

Try it online!

Explanation

ØDœc  Double-builtin; main link
  œc  Number of combinations of          of length
ØD                              [digits]
                                                   <right argument>

Behavior for n > 10 is empty list.

  • how does it not work with input greater than 10 (tried it, and seemed weird to me)? – Windmill Cookies Jun 12 at 14:11
  • 2
    @gnu-nobody it gives the empty list because you can't have a combination of 11 elements with 10 choices. – HyperNeutrino Jun 12 at 14:12
  • oh, the ØD is the "0123456789". thanks. – Windmill Cookies Jun 12 at 14:13

05AB1E, 5 bytes

žhæsù

Try it online!

Explanation

   sù  # keep elements the length of the input
  æ    # from the powerset
žh     # of 0123456789
  • Didn't know ù was a thing. Pretty sure I've used filter before. How new is that one? – Magic Octopus Urn Jun 12 at 17:57
  • 1
    @MagicOctopusUrn: Pretty old. Been there for a long time :P – Emigna Jun 12 at 19:44
  • Me brain no do see the command before the now. Me brain no am smart. – Magic Octopus Urn Jun 12 at 19:47
  • Has happened to me a couple of times. More than once on some commands as well, which is... – Emigna Jun 12 at 19:49
  • ... acceptable of a god-tier 05AB1Er (05AB1E-ite? 05AB1E-an?) such as yourself. – Magic Octopus Urn Jun 12 at 19:57

JavaScript (ES7), 89 bytes

Returns a list of lists of digits (as characters), or an empty list if n > 10.

n=>[...2**29+'4'].reduce((a,x)=>[...a,...a.map(y=>[x,...y])],[[]]).filter(a=>a.length==n)

Try it online!

How?

We first generate a list of all decimal digits as characters by computing 229 = 536870912, adding the missing '4' and splitting:

[...2**29+'4'] → [ '5', '3', '6', '8', '7', '0', '9', '1', '2', '4' ]

We then compute the powerset:

.reduce(                                   ,[[]]) // starting with a[] holding an empty list
        (a,x)=>[                          ]       // for each decimal digit x:
                ...a,                             //   copy all previous entries in a[]
                     ...a.map(y=>        )        //   and duplicate each previous entry y
                                 [x,...y]         //   with x prepended at the beginning

Finally, we filter the results on their length:

.filter(a=>a.length==n)

Python 3, 57 bytes

lambda l:combinations(range(10),l)
from itertools import*

Try it online!

Finds all combinations of 0 .. 9 of length l.

Behavior for n > 10 is empty list.

Wolfram Language (Mathematica), 22 bytes

0~Range~9~Subsets~{#}&

Try it online!

Python 2, 62 bytes

f=lambda n:{d+s for d in`5**19`*n for s in f(n-1)if d>s}or{''}

Returns a set of strings.

Try it online!

Pyth, 4 bytes

.cUT

Try it here

Explanation

.cUT
  UT     [0, 1, ..., 9].
.c  Q    All (implicit input)-element subsets.

R, 17 bytes

combn(0:9,scan())

Try it online!

Errors for input greater than 10.

Returns a matrix where each column is a PIN.

Ruby, 30 bytes

->n{[*[*0..9].combination(n)]}

Try it online!

MATL, 6 bytes

4Y2wXN

Try it online!

Returns nothing (empty array) for k>10.

          % implicit input k
4Y2       % push '0':'9'
   w      % swap top two elements of stack
    XN    % nchoosek, select all k-combinations of '0':'9' as a char array
          % implicit output

Haskell, 56 50 bytes

-6 bytes thanks to Hat Wizard.

(10!)
_!0=[[]]
w!n=[0..w-1]>>=(map.(:)<*>(!(n-1)))

Try it online!

Java (JDK 10), 105 bytes

n->{var s="";for(int i=1024,j;i-->0;s+=" ")for(j=10;n.bitCount(i)==n&j-->0;)s+=(1<<j&i)>0?j:"";return s;}

Try it online!

Haskell, 47 bytes

f 0=[[]]
f n=[a:x|x<-f$n-1,a<-[0..9],all(/=a)x]

Try it online!

Explanation

When the number of digits is zero there is only one combination, that is the empty one:

f 0=[[]]

When the number of digits is n and n/=0 the combinations are all the ways to add digits to combinations from f$n-1 such that no digit is addded to a combination that already contains it.

f n=[a:x|x<-f$n-1,a<-[0..9],all(/=a)x]

Gaia,  4  3 bytes

₸…K

Try it online!

It's been a while since I posted an answer in Gaia! Thanks to Mr. Xcoder for saving a byte!

      % implicit input n
₸     % push 10
 …    % pop 10, push 0..9
  K   % all subsets of size n
      % print top of stack implicitly
  • You don't need @. – Mr. Xcoder Jun 12 at 18:15
  • @Mr.Xcoder thanks. Didn't realize that; it doesn't seem to be documented very clearly. – Giuseppe Jun 12 at 18:17

Retina, 51 36 bytes

.+
10*
"$+"{%`^.
*_$&
L$v`_+
$.%`$%'

Try it online! Outputs nothing for n>10. Explanation:

.+
10*

Replace the input with 10 _s.

"$+"{

Repeat the rest of the program n times.

%`^.
*_$&

Prefix each number with _ repeated according to its first digit.

L$v`_+

Match all _s, but include all of the following _s in the match too, for which we need to enable overlapping matches.

$.%`$%'

For each _ found, prefix the number of _s to its left to the number.

This is a bit tricky so perhaps an actual case would be better. Let's suppose that we've already run the loop twice, so that all 2-digit PINs have been generated, and we're currently working through them to create 3-digit PINs. We'll look at what happens to 36: The first digit is 3, so three _s are prefixed, to make ___36. This then creates the following matches, marked here with `'s:

Match   $%` $.%`
`___'36     0
_`__'36 _   1
__`_'36 __  2

$%' evalutes to 36 in all three cases, resulting in the 3-digit PINs 036, 136 and 236.

If we were then to go on to create 4-digit PINs, then 036 would not have any _s prefixed, and therefore would result in no matches at all in the final output.

  • The question says the input is non-negative. On input zero, this outputs ten underscore characters. – aschepler Jun 13 at 1:36
  • @aschepler Indeed; I see the OP has decided to exclude that case, but in case you're wondering, the fix would be to prepend ^0 and at a cost of 5 bytes. – Neil Jun 13 at 7:47

Proton, 43 bytes

(0..9)&__import__("itertools").combinations

Try it online!

Proton finally outgolfs Python :D I thought (import itertools) would return the value but apparently I failed at that. Also importing * afterwards doesn't work because it's not in a lambda, it's a top-level expression.

Japt, 5 bytes

Outputs an array of digit arrays. Outputs all combinations if input is 0 or an empty array if input is <0 or >10.

Ao àU

Try it


Explanation

          :Implicit input of integer U
A         :10
 o        :Range [0,10)
   àU     :Combinations of length U
  • I don't think the output for input 0 is correct. The output should be a list containing one element, which is empty. – aschepler Jun 13 at 1:34
  • @aschepler, where are you getting that from? – Shaggy Jun 13 at 7:54

Stax, 4 bytes

Vd,S

Try it online!

Vd is "0123456789". , pushes the input to the main stack. S gets combinations of the specified size.

In the tio link, m is used in the footer to print each output.

Standard ML, 124 122 121 bytes

open List;fun f(s:: &)m=(if exists(fn x=>x=m)s then[]else[m::s])@f&m|f&m= &
fun!0=[[]]| !n=concat(tabulate(10,f(!(n-1))))

Try it online! Example usage: !2 yields [[0,1],[0,2],[0,3],[0,4],[0,5],[0,6],[0,7],[0,8],[0,9],[1,0],[1,2],[1,3], ...].

Ungolfed:

open List;

(* int list list -> int -> int list list *)
fun f (s::r) m =
    if exists (fn x => x=m) s 
    then f r m
    else (m::s) :: f r m
  | f [] m = []

(* int -> int list list *)
fun g 0 = [[]]
  | g n = concat(tabulate(10, f(g(n-1))))

Some alternatives:

125 123 bytes

fun f(s:: &)m=(if List.exists(fn x=>x=m)s then[]else[m::s])@f&m|f&m= &
fun!m 0=[[]]| !10n=[]| !m n=f(!0(n-1))m@ !(m+1)n;!0;

Try it online! Defines an anonymous function which is bound to it.

127 124 bytes

fun!0=[[]]| !n=let fun f(s:: &)m=(if List.exists(fn x=>x=m)s then[]else[m::s])@f&m|f&9=[]|f&m=f(!(n-1))(m+1)in f(!(n-1))0end

Try it online!

JavaScript (Node.js), 53 bytes

f=(i,b='',k=10)=>k--?f(i-1,k+b,k)+f(i,b,k):i?'':b+`,`

Try it online!

Oracle 18 SQL, 169 bytes

Not a golfing language but:

WITH t(v)AS(SELECT*FROM SYS.ODCINUMBERLIST(0,1,2,3,4,5,6,7,8,9)),n(a,b,c)AS(SELECT a,-1,''FROM i UNION ALL SELECT a-1,v,v||c FROM n,t WHERE v>b)SELECT c FROM n WHERE a=0

Expected the input to be in a table i with column a:

CREATE TABLE i (a INT);
INSERT INTO i VALUES ( 3 );

Try it online at Oracle Live SQL (a free login is required then copy-paste the solution into a worksheet) or SQLFiddle (no login but requires +7 bytes to work on the lower Oracle version).

CJam, 13 11 bytes

{Ae!f<:$_|}

Try it online!

Technically doesn't run on tio.run, since heap space runs out. However, it works properly for up to 9 digit keypads, and should run just fine with more RAM.

Saved 2 bytes thanks to Dennis

Bash, 113 99 bytes

p()(r $1 0 $[10-$1])
r()for i in `seq $2 $3`;{ (($1>1))&&r $[$1-1] $[i+1] $[$3+1] $4$i||echo $4$i;}

Try it online!

  • r 0 doesn't work. – aschepler Jun 13 at 1:40
  • r is the recursive function: it's not meant to be launched with only 1 parameter. p 0 behaviour is unspecified in the question. – glenn jackman Jun 13 at 1:44

JavaScript (Firefox 30-57), 67 bytes

n=>n?[for(x of f(n-1))for(y of Array(x?+x[0]:10).keys())y+x]:['']

Port of my Retina answer, but works for n=0 too (returning a list of an empty string, as distinct from an empty list for n>10).

Charcoal, 21 bytes

⊞υωFχ≔⁺υEυ⁺κIιυΦυ⁼θLι

Try it online! Link is to verbose version of code. Explanation:

⊞υω

Push the empty string to the predefined list.

Fχ

Loop over each digit.

Eυ⁺κIι

Append the digit to every string in the list.

≔⁺υ...υ

Append the result to the original list.

Φυ⁼θLι

Print all strings with the correct number of digits.

Perl 6, 20 bytes

{combinations 10,$_}

Try it online!

This is exactly what combinations (as a subroutine, or .combinations on a list) is for.

https://docs.perl6.org/routine/combinations

J, 32 bytes

.. frustratingly longer than Mathematica and R f=:{[:(#@>"0]/.])[:<@I.@#:@i.2^] TIO

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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