Counting in bijective base 62

20

4

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.

Simon Landry

Posted 2016-05-03T22:38:43.503

Reputation: 441

What do you mean by outputting an array? – frederick – 2016-05-03T22:41:26.050

So letters and numbers only? What order do you use? In ASCII numbers come first, then uppercase letters, the lowercase – Luis Mendo – 2016-05-03T22:43:18.443

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). – Simon Landry – 2016-05-03T22:45:10.550

@LuisMendo The order doesn't matter, as long as all the number/letter combinaisons are there. – Simon Landry – 2016-05-03T22:46:24.167

1@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. – Alex A. – 2016-05-03T23:02:50.370

@edc65 An input of 3 would print all the string from 'a' to '999'‚ an input of 5 up to '99999' and so on. – Simon Landry – 2016-05-03T23:03:25.453

Is a leading newline for the output okay :p? – Adnan – 2016-05-03T23:22:45.273

So, you're basically listing base 62 numbers up to N digits, where N is the input? – Wildcard – 2016-05-04T01:19:09.857

@Wildcard Exactly. – Simon Landry – 2016-05-04T02:01:41.003

@Adnan Sure as long as the rest of the input is fine, I'll allow a newline. :) – Simon Landry – 2016-05-04T02:04:35.830

Since a leading newline is allowed, would be returning an array that starts with an empty string be acceptable as well? – Dennis – 2016-05-04T03:14:35.963

@Dennis I guess that's only fair :) – Simon Landry – 2016-05-04T03:23:06.613

3OK, 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. – Dennis – 2016-05-04T03:44:06.783

It's not really base 62 (you would not have 00, 000 and so on). It's more like Excel column naming. We had a challenge about that: http://codegolf.stackexchange.com/q/37515/21348

– edc65 – 2016-05-04T08:17:20.977

@Dennis, the question still looks to me like it requires bijective base 62 (and if I didn't have a supervote, I'd vote to close as a dupe of http://codegolf.stackexchange.com/q/54105/194 )

– Peter Taylor – 2016-05-04T08:35:50.073

@PeterTaylor it's close but simpler. Not all challenges about bijective base are dupes – edc65 – 2016-05-04T08:49:06.787

What is the largest expected input? – Digital Trauma – 2016-05-05T22:46:19.857

@DigitalTrauma Most people have been using 2 and 3 as their test case, if it works with both your answer will be considered good. – Simon Landry – 2016-05-06T01:14:33.397

Answers

13

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.

Dennis

Posted 2016-05-03T22:38:43.503

Reputation: 196 637

1When writing your own language for codegolf, couldn't you just fork it, modify it and use a 1 byte solution? – Florian Wendelborn – 2016-05-04T05:44:24.780

9No. 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. – Dennis – 2016-05-04T05:46:19.330

Ah, that makes sense. A little bit ridiculous that this rule exists though. ;) – Florian Wendelborn – 2016-05-04T05:47:44.267

8How is that ridiculous? If it was allowed, each challenge would be solved with 1 byte – Zibelas – 2016-05-04T08:19:11.940

1Do those characters fit in a byte each? – UncleZeiv – 2016-05-04T08:33:54.263

7@UncleZeiv the Jelly code page is linked in the post title – edc65 – 2016-05-04T08:50:44.670

@edc65 sure, I was just wondering what kind of character set are we talking about here? One that contains Ø, W, Ṗ, ṗ along the standard alphanumeric characters in a single table of 256 symbols? – UncleZeiv – 2016-05-04T11:14:13.693

7@UncleZeiv There's really only one character set that does it, which is the Jelly code page. – isaacg – 2016-05-04T18:17:27.150

ah apologies to @edc65 then, when you said "code page" I actually interpreted it as "webpage for the source code" rather than "character set", and also I missed the second link and only followed the first one! – UncleZeiv – 2016-05-05T16:32:59.990

Jelly is angry ;/ or jealous. – Bálint – 2016-05-06T07:13:16.390

8

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 

nimi

Posted 2016-05-03T22:38:43.503

Reputation: 34 639

5

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.

Neil

Posted 2016-05-03T22:38:43.503

Reputation: 95 035

4I can't get your code to work, says function f is undefined. – Simon Landry – 2016-05-04T01:10:30.923

1@SimonLandry Whoops, I forgot the f= at the start. (I always forget to do that for recursive answers.) – Neil – 2016-05-04T07:38:09.803

Doesn't work for reasons above. – CalculatorFeline – 2016-05-04T15:49:02.850

@CatsAreFluffy I put the f= in, any further problems are due to the way you're trying to call it. – Neil – 2016-05-04T16:36:59.963

5

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.

xnor

Posted 2016-05-03T22:38:43.503

Reputation: 115 687

4

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

Adnan

Posted 2016-05-03T22:38:43.503

Reputation: 41 965

4

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.

a spaghetto

Posted 2016-05-03T22:38:43.503

Reputation: 10 647

4

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']

deustice

Posted 2016-05-03T22:38:43.503

Reputation: 61

1you can remove the spaces between parenthesis and letters. – Maltysen – 2016-05-03T23:35:23.940

Try i in range(n) with repeat=i+1 – NonlinearFruit – 2016-05-04T01:12:26.993

+1 for the negative input. Is that built in into the range function? – Kevin Cruijssen – 2016-06-23T11:10:06.920

4

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}

Value Ink

Posted 2016-05-03T22:38:43.503

Reputation: 10 608

3

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

Maltysen

Posted 2016-05-03T22:38:43.503

Reputation: 25 023

Nice one! Care to provide an explanation? – Simon Landry – 2016-05-03T22:51:24.240

I thought there's a command to iterate through the strings in lexicographical order? – Leaky Nun – 2016-05-03T23:20:10.270

@KennyLau nvm, doesn't do numbers. – Maltysen – 2016-05-03T23:26:12.270

rBG1 save one byte over +GrG1 – Jakube – 2016-05-04T00:00:30.863

@Jakube oh, Bifurcate works with arguments? thanks. – Maltysen – 2016-05-04T00:01:10.150

3

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.

Dennis

Posted 2016-05-03T22:38:43.503

Reputation: 196 637

Had almost the same idea, but a few bytes longer... – Byte Commander – 2016-05-04T09:31:00.437

Wow 2 answers from you @Dennis, you're killing it! :) – Simon Landry – 2016-05-05T06:18:13.687

2

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

Luis Mendo

Posted 2016-05-03T22:38:43.503

Reputation: 87 464

1

, 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

Mama Fun Roll

Posted 2016-05-03T22:38:43.503

Reputation: 7 234

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? :) – Simon Landry – 2016-05-05T06:14:04.290

1github.com/molarmanful/ESMin – Mama Fun Roll – 2016-05-05T12:41:11.143

Is the name of the language seriously 4 spaces? – Bálint – 2016-05-06T07:08:49.367

No, but your browser might not render the doublestruck characters correctly. In ASCII, it's called ESMin. – Mama Fun Roll – 2016-05-06T12:36:33.070

1

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.

Ed.

Posted 2016-05-03T22:38:43.503

Reputation: 141

1

APL, 38 37 bytes

{⊃{⍵,,⍺∘.,⍵}/⍵⍴⊂,¨⎕a,⎕d,⍨⎕ucs 96+⍳26}

lstefano

Posted 2016-05-03T22:38:43.503

Reputation: 850

I have to ask, how does one get around if they can't COMMUTE? (⎕ucs 96+⍳26),⎕d => ⎕d,⍨⎕ucs 96+⍳26 – Zacharý – 2017-07-31T22:54:28.800

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... – lstefano – 2017-08-02T09:14:01.277

1

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.

miles

Posted 2016-05-03T22:38:43.503

Reputation: 15 654

0

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!

Giuseppe

Posted 2016-05-03T22:38:43.503

Reputation: 21 077

0

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!

jq170727

Posted 2016-05-03T22:38:43.503

Reputation: 411

0

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.

Digital Trauma

Posted 2016-05-03T22:38:43.503

Reputation: 64 644

0

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.

Digital Trauma

Posted 2016-05-03T22:38:43.503

Reputation: 64 644