20
\$\begingroup\$

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.

\$\endgroup\$
  • \$\begingroup\$ What do you mean by outputting an array? \$\endgroup\$ – frederick May 3 '16 at 22:41
  • \$\begingroup\$ So letters and numbers only? What order do you use? In ASCII numbers come first, then uppercase letters, the lowercase \$\endgroup\$ – Luis Mendo May 3 '16 at 22:43
  • \$\begingroup\$ 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). \$\endgroup\$ – Simon Landry May 3 '16 at 22:45
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – Alex A. May 3 '16 at 23:02
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ – Dennis May 4 '16 at 3:44

19 Answers 19

13
\$\begingroup\$

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ṖṗR;/  Main link. Argument: n

Ø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.
\$\endgroup\$
  • 1
    \$\begingroup\$ When writing your own language for codegolf, couldn't you just fork it, modify it and use a 1 byte solution? \$\endgroup\$ – Dodekeract May 4 '16 at 5:44
  • 9
    \$\begingroup\$ 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. \$\endgroup\$ – Dennis May 4 '16 at 5:46
  • 8
    \$\begingroup\$ How is that ridiculous? If it was allowed, each challenge would be solved with 1 byte \$\endgroup\$ – Zibelas May 4 '16 at 8:19
  • 7
    \$\begingroup\$ @UncleZeiv the Jelly code page is linked in the post title \$\endgroup\$ – edc65 May 4 '16 at 8:50
  • 7
    \$\begingroup\$ @UncleZeiv There's really only one character set that does it, which is the Jelly code page. \$\endgroup\$ – isaacg May 4 '16 at 18:17
8
\$\begingroup\$

Haskell, 65 bytes

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],[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 
\$\endgroup\$
5
\$\begingroup\$

Python, 86 bytes

f=lambda n:n*[1]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.

\$\endgroup\$
5
\$\begingroup\$

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.

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

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.

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

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}
\$\endgroup\$
4
\$\begingroup\$

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

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

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']
\$\endgroup\$
  • 1
    \$\begingroup\$ you can remove the spaces between parenthesis and letters. \$\endgroup\$ – Maltysen May 3 '16 at 23:35
  • \$\begingroup\$ Try i in range(n) with repeat=i+1 \$\endgroup\$ – NonlinearFruit May 4 '16 at 1:12
  • \$\begingroup\$ +1 for the negative input. Is that built in into the range function? \$\endgroup\$ – Kevin Cruijssen Jun 23 '16 at 11:10
3
\$\begingroup\$

Pyth - 13 12 bytes

1 bytes saved thanks to @Jakube.

sm^s+rBG1UTh

Try it online here.

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
   s                 Add up list
    ++               Concat up three things
     G               Alphabet
     rG1             Uppercase alphabet
     UT              All digits
\$\endgroup\$
  • \$\begingroup\$ Nice one! Care to provide an explanation? \$\endgroup\$ – Simon Landry May 3 '16 at 22:51
  • \$\begingroup\$ I thought there's a command to iterate through the strings in lexicographical order? \$\endgroup\$ – Leaky Nun May 3 '16 at 23:20
  • \$\begingroup\$ @KennyLau nvm, doesn't do numbers. \$\endgroup\$ – Maltysen May 3 '16 at 23:26
  • \$\begingroup\$ rBG1 save one byte over +GrG1 \$\endgroup\$ – Jakube May 4 '16 at 0:00
  • \$\begingroup\$ @Jakube oh, Bifurcate works with arguments? thanks. \$\endgroup\$ – Maltysen May 4 '16 at 0:01
3
\$\begingroup\$

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.

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

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
\$\endgroup\$
1
\$\begingroup\$

𝔼𝕊𝕄𝕚𝕟, 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
\$\endgroup\$
  • \$\begingroup\$ 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? :) \$\endgroup\$ – Simon Landry May 5 '16 at 6:14
  • 1
    \$\begingroup\$ github.com/molarmanful/ESMin \$\endgroup\$ – Mama Fun Roll May 5 '16 at 12:41
  • \$\begingroup\$ Is the name of the language seriously 4 spaces? \$\endgroup\$ – Bálint May 6 '16 at 7:08
  • \$\begingroup\$ No, but your browser might not render the doublestruck characters correctly. In ASCII, it's called ESMin. \$\endgroup\$ – Mama Fun Roll May 6 '16 at 12:36
1
\$\begingroup\$

Perl, 113 bytes + whitespace

@r="";
for (1..shift) {
  @r = sub {
    map { $c=$_; map $c.$_, @{$_[1]} } @{$_[0]}
  }->(\@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.

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

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.

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

APL, 38 37 bytes

{⊃{⍵,,⍺∘.,⍵}/⍵⍴⊂,¨⎕a,⎕d,⍨⎕ucs 96+⍳26}
\$\endgroup\$
  • \$\begingroup\$ I have to ask, how does one get around if they can't COMMUTE? (⎕ucs 96+⍳26),⎕d => ⎕d,⍨⎕ucs 96+⍳26 \$\endgroup\$ – Zacharý Jul 31 '17 at 22:54
  • \$\begingroup\$ 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... \$\endgroup\$ – lstefano Aug 2 '17 at 9:14
0
\$\begingroup\$

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.

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

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.
\$\endgroup\$
0
\$\begingroup\$

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!

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

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!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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