Counting in bijective base 62

The task is to generate all the strings from 'a' to '999' including upper case characters like so:

'a', 'b', 'c' ... 'y', 'z', 'A', 'B', 'C' ... 'Y', 'Z', '0', '1', 2' ...
'8', '9', 'aa', 'ab', 'ac' ... 'az', 'aA', 'aB' ... 'aZ', 'a0' ... 'a9', 'ba'

and so on (filling in the gaps), optionally starting with the empty string.

Input:

• The amount of consecutive characters the program has to print up to.

Output:

• An array containing each string OR one string per line

Clarifications:

• The order doesn't matter, you can print uppercase or lowercase letters first if you want.

• The output can return any type of enumerable, doesn't have to be an array specifically, although I doubt printing all the combinations won't be the easiest way to go.

• An input of 3 would print all the string from 'a' (or '') to '999'‚ an input of 5 up to '99999' and so on.

• What do you mean by outputting an array?
– fvla
May 3 '16 at 22:41
• So letters and numbers only? What order do you use? In ASCII numbers come first, then uppercase letters, the lowercase May 3 '16 at 22:43
• An enumerable containing all the values i.e. ['a', 'b', 'c' ..]. You should either see the output on each line via STDOUT or be able to assign it via a = (function return). May 3 '16 at 22:45
• @edc65 As I understand it, the input is the maximum number of characters to combine. So for input 4, you go from a to 9999, for 5 it's a to 99999, and so on. May 3 '16 at 23:02
• OK, thanks for clearing that up. That saved a lot of bytes. :) I think the current title is a bit confusing since you seem to require bijective base 62. May 4 '16 at 3:44

Jelly, 7 bytes

ØWṖṗR;/

This is a monadic link that accepts an integer as input and returns an array of strings.

Try it online!

How it works

ØW       Yield 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_'.
Ṗ      Remove the last element (underscore).
R    Range; yield [1, ..., n].
ṗ     Cartesian product. For each k in the range, this yields the arrays of all
strings of alphanumeric characters.
;/  Concatenate the arrays of strings of each length.
• When writing your own language for codegolf, couldn't you just fork it, modify it and use a 1 byte solution? May 4 '16 at 5:44
• No. We have strict rules for admissible programming languages, and one is that a working interpreter has to have existed before the challenge was posted. I could add a built-in for this task now, but I could use it only in future challenges. May 4 '16 at 5:46
• How is that ridiculous? If it was allowed, each challenge would be solved with 1 byte May 4 '16 at 8:19
• @UncleZeiv the Jelly code page is linked in the post title May 4 '16 at 8:50
• @UncleZeiv There's really only one character set that does it, which is the Jelly code page. May 4 '16 at 18:17

a#b=[a..b]
k n=mapM id.('a'#'z'++'A'#'Z'++'0'#'9'<$)=<<(1#)<$>1#n

Usage example: k 3 -> ["a","b","c",....,"997","998","999"].

How it works

a#b = [a..b]        -- helper function that builds a list from a to b

(1#n)<$> -- map the function (1#), i.e. "build the list from 1 up to" 1#n -- on the list from 1 to n -- now we have [,[1,2],[1,2,3]] =<< -- map over this list (and combine results in a single list) ( <$)      -- a function that makes length of input copies of
'a'#'z'++ ... '9'  -- all characters we need

-- now we have [["a..9"],["a..9","a..9"],["a..9","a..9","a..9"]]

mapM id.            -- and make the cartesian product of each sublist

Python, 86 bytes

f=lambda n:n*and[x+chr(y)for x in['']+f(n-1)for y in range(128)if chr(y).isalnum()]

Outputs a list of non-empty strings. Recursively prepends each alphanumeric character to each output for n-1 and empty string.

05AB1E, 9 8 bytes

Code:

ƒžj¨Nã€,

Explanation:

ƒ          # For N in range(0, input + 1), do:
žj        #   Push predefined literal [a-zA-Z0-9_]
¨       #   Remove the last character (the underscore)
N      #   Push N
ã     #   Take the Cartesian product, with N repetitions.
€,   #   For each element in the array, print with a newline

Uses CP-1252 encoding. Try it online!.

JavaScript (Firefox 30-57), 108 bytes

f=n=>n?[for(s of['',...f(n-1)])for(c of(t='abcdefghijklmnopqrstuvwxyz')+t.toUpperCase()+'0123456789')s+c]:[]

Saved 3 bytes by using toUpperCase. Computing the 62 characters takes me an extra 10 bytes.

• I can't get your code to work, says function f is undefined. May 4 '16 at 1:10
• @SimonLandry Whoops, I forgot the f= at the start. (I always forget to do that for recursive answers.)
– Neil
May 4 '16 at 7:38
• Doesn't work for reasons above. May 4 '16 at 15:49
• @CatsAreFluffy I put the f= in, any further problems are due to the way you're trying to call it.
– Neil
May 4 '16 at 16:36

Cinnamon Gum, 15 bytes

0000000: 689b b718 05be a345 9c4b c283 d077 de    h......E.K...w.

Not short enough, despite this being the exact kind of challenge Cinnamon Gum was made for :(

Compressed by converting from bijective base 96 to base 256. Try it online. Inputs greater than 2 will cause problems on TIO.

Explanation

This decompresses to the regex [a-zA-Z0-9]{1,%s}. The h mode then substitutes the input in to %s and outputs all strings matching the regex.

Ruby, 82 bytes

Constructs cartesian products of the character set up to the given length. The character set is generated by grabbing all characters between 0 and z and filtering out non-word characters and also _.

->n{a=(?0..?z).grep(/\w/)-[?_];r=[]
n.times{|i|r+=a.product(*[a]*i).map &:join};r}

Python 2.7, 136 134 bytes

Thanks to Maltysen and NonlinearFruit for saving 2 bytes

from itertools import*;from string import*;f=lambda n:[''.join(a) for i in range(1,n+1) for a in product(ascii_letters+digits,repeat=i)]

Takes ascii_letters and digits from the string module and uses the Cartesian Product as product from itertools to compute all the combinations.

Output

out = f(3)

print out[:10]
['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']

print out[100:110]
['aM', 'aN', 'aO', 'aP', 'aQ', 'aR', 'aS', 'aT', 'aU', 'aV']

print out[-10:]
['990', '991', '992', '993', '994', '995', '996', '997', '998', '999']
• you can remove the spaces between parenthesis and letters. May 3 '16 at 23:35
• Try i in range(n) with repeat=i+1 May 4 '16 at 1:12
• +1 for the negative input. Is that built in into the range function? Jun 23 '16 at 11:10

Pyth - 13 12 bytes

1 bytes saved thanks to @Jakube.

sm^s+rBG1UTh
s                    Add up the lists of different lengths
m          (Q)      Map implicitly over input
^     h(d)         Cartesian product of string to implicit lambda var + 1
++               Concat up three things
G               Alphabet
rG1             Uppercase alphabet
UT              All digits

• Nice one! Care to provide an explanation? May 3 '16 at 22:51
• I thought there's a command to iterate through the strings in lexicographical order? May 3 '16 at 23:20
• @KennyLau nvm, doesn't do numbers. May 3 '16 at 23:26
• rBG1 save one byte over +GrG1 May 4 '16 at 0:00
• @Jakube oh, Bifurcate works with arguments? thanks. May 4 '16 at 0:01

Python 2, 106 97 bytes

from string import*
f=lambda n,r=['']:n and r+f(n-1,[x+y for x in r for y in letters+digits])or r

Try it on Ideone.

• Had almost the same idea, but a few bytes longer... May 4 '16 at 9:31
• Wow 2 answers from you @Dennis, you're killing it! :) May 5 '16 at 6:18

MATL, 12 bytes

:"3Y24Y2h@Z^

This takes a number as input.

Try it online!

Explanation

:       % Implicitly take input, say N. Generate range [1 2... N]
"       % For each number in that range
3Y2   %   Predefined literal: string with all letters, uppercase and lowercase
4Y2   %   Predefined literal: string with all digits
h     %   Concatenate horizontally
@     %   Push number of characters corresponding to current iteration
Z^    %   Cartesian power. Each result is a row
% End for each. Implicitly display

𝔼𝕊𝕄𝕚𝕟, 21 chars / 27 bytes

ⒶïⓜᵖɱĬ⟦ᶛ+ᶐ+⩤9⨝],⧺_)ė)

Try it here (Firefox only).

Nope. Nope. Nope.

Explanation

ⒶïⓜᵖɱĬ⟦ᶛ+ᶐ+⩤9⨝],⧺_)ė) // implicit:
Ⓐïⓜ                    // [...Array(input)].map(($,_)=>...) ᵖ // push to stack: ɱĬ⟦ᶛ+ᶐ+⩤9⨝],⧺_) // list of n-digit numbers in [a-zA-Z0-9]-ary ė) // formatted into a matrix (no spaces) // implicit stack output, newline-separated • First time I see this language and can't find to find it using Google, care to add a link to its documentation and (or) source code? :) May 5 '16 at 6:14 • github.com/molarmanful/ESMin May 5 '16 at 12:41 • Is the name of the language seriously 4 spaces? May 6 '16 at 7:08 • No, but your browser might not render the doublestruck characters correctly. In ASCII, it's called ESMin. May 6 '16 at 12:36 Perl, 113 bytes + whitespace @r=""; for (1..shift) { @r = sub { map {$c=$_; map$c.$_, @{$_} } @{$_} }->(\@r, [0..9, "a".."z", "A".."Z"]) } map say($_), @r

Use "perl -E" on the above, with an argument that's a number. I could probably decently have not counted the last "map say" in the chars count.

J, 50 bytes

62&(('0123456789',~(,toupper)u:97+i.26){~#~#:i.@^)

Half of the bytes, 25 to be exact, are spent generating the letters and digits needed.

APL, 38 37 bytes

{⊃{⍵,,⍺∘.,⍵}/⍵⍴⊂,¨⎕a,⎕d,⍨⎕ucs 96+⍳26}
• I have to ask, how does one get around if they can't COMMUTE? (⎕ucs 96+⍳26),⎕d => ⎕d,⍨⎕ucs 96+⍳26 Jul 31 '17 at 22:54
• I can assure you I can commute (not talking about "making the same journey regularly between work and home", because that's boring). You seem to have found that it can be easy improve on other people's solutions. Especially if you don't have a full time job. Then there's real life that makes everything even harder... Aug 2 '17 at 9:14

Bash + GNU utilities, 90

printf -vs %$1s eval printf '%s\\n'${s// /{=,{a..z\},{A..Z\},{0..9\}\}}|sed s/^=*//\;/=/d

Input as a command-line parameter. Output is a whitespace-separated list.

Works for inputs upt and including 3. Run out of memory with 4 - the eval printf takes an the entire set of 63n elements of the bash expansion.

Bash + GNU utils, 66

Different (and I think slightly novel) approach to my other answer:

dc -e"64 $1^[d2 48^r-P1-d0<m]dsmx"|base64 -w8|sed s_^/*__\;/[+/]/d • dc counts down from 248-1 to 248-64n and Prints each resulting number as a bytestream (i.e. base 256). If the input is between 1 and 4 inclusive, this is guaranteed to be exactly 6 bytes per number. • base64 converts this to base64 output and thus 8 bytes per base64 digit, one per line. • sed strips off leading / (base64 digit 63), and then removes any lines containing + or / (base64 digits 62 and 63). This leaves the required sequence. R, 73 bytes y='';x=c(letters,LETTERS,0:9);for(i in 1:scan())cat(y<-outer(y,x,paste0)) y starts off as empty string, x as the base case 'a','b','c',...,'8','9'. outer takes each of its input arguments, and applies the function paste0 to each combination of elements in y and x which concatenates the strings. y saves the result, cat prints it, and it iterates through STDIN number of times of doing this. Try it online! Jq 1.5, 97 bytes range(.)as$n|[[range(97;123),range(65;91),range(48;58)]|implode/""|combinations($n+1)]|map(add)[] Expanded range(.) as$n           # for each n-digit sequence
| [
[                    # build array of ordinals for
range(97;123),     #   a-z
range(65;91),      #   A-Z
range(48;58)       #   0-9
]
| implode/""           # make into array of strings
| combinations(\$n+1)   # generate array of n-element combinations
]
| map(add)[]               # convert to sequence of strings

Try it online!