6
\$\begingroup\$

Challenge: Print the entire printable ASCII charset (not just a range!) in order.

[space]!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

(Of course, replace space with the actual space character.)

Rules: read closely

  • No other characters allowed in the output.

  • Program/function does not accept input.

  • Try not to hardcode/embed the output.

  • This is code golf, shortest answer wins.

\$\endgroup\$
  • 5
    \$\begingroup\$ Well they are a couple main things about this challenge that are why it's getting downvoted. A) the community really doesn't like it when you ban golfing language (whether or not I agree with this is a different matter) B) it's rather simple but I wouldn't say it's exactly trivial (kinda on the edge). Also saying "Try not to ..." usually isn't a good sign because it's not actually enforcing it (because it's hard to enforce this objectively), and it might mean your challenge may not be found that interesting (therefore getting downvotes). \$\endgroup\$ – Downgoat Jul 11 '16 at 0:38
  • 8
    \$\begingroup\$ I'm not sure how to interpret try not to hardcode the output here. For a constant output challenge, that's the only way... \$\endgroup\$ – Dennis Jul 11 '16 at 0:50
  • 1
    \$\begingroup\$ Do we have to print the output from a function or is returning a string acceptable? \$\endgroup\$ – Dennis Jul 11 '16 at 5:53
  • 2
    \$\begingroup\$ Does "no other characters" include embedded newlines? \$\endgroup\$ – Neil Jul 11 '16 at 7:48
  • 2
    \$\begingroup\$ This is NOT a duplicate of that challenge! Programs there had to accept input and print only part of the table. This challenge is easier and different. \$\endgroup\$ – dkudriavtsev Jul 11 '16 at 23:23

62 Answers 62

5
\$\begingroup\$

MATL, 3 bytes

6Y2

Try it Online

And for the sake of a non-built-in (7 bytes)

32:127c
\$\endgroup\$
  • \$\begingroup\$ Non-builtin: '~':. \$\endgroup\$ – Stewie Griffin Apr 30 '18 at 12:09
  • \$\begingroup\$ @StewieGriffin Unfortunately that didn't work until 18.5.0. Here's what it looked like at the time of this challenge. \$\endgroup\$ – Suever Apr 30 '18 at 12:42
15
\$\begingroup\$

Brainfuck, 30 27 bytes

+[[>++<<+>-]>]<<<++[<.+>++]

Try it online!

How it works

+ changes the initial cell to 1. After this step, we enter the following, nested loop.

[        While the current cell is non-zero:
  [        While the current cell (C) is non-zero:
    >++      Increment the cell to C's right twice.
    <<+      Increment the cell to C's left.
    >-       Decrement C.
  ]
  >        Advance to the cell to C's right.
]

This computes consecutive powers of 2 until the the value 256 = 0 (mod 256) is reached. When the outer loop finishes, the tape is in the following state.

                                     v
001 002 004 008 016 032 064 128 000 000 000

<<<++ retrocedes three cells and increments twice, leaving the tape as follows.

                         v
001 002 004 008 016 032 066 128 000 000 000

Now we're ready to print the actual output. As a stop condition, we increment the cell above twice each time we print and increment the cell to its left. Since 66 + 95 × 2 = 256 = 0 (mod 256), we stop after printing all 95 printable ASCII characters. We achieve this as follows.

[      While the current cell (C) is non-zero:
  <      Retrocede to the cell to C's left.
  .+     Print its content and increment.
  >++    Increment C twice.
]
\$\endgroup\$
  • 2
    \$\begingroup\$ with Dennis brainfuck outgolfs procedual programming languages! \$\endgroup\$ – downrep_nation Jul 11 '16 at 6:59
  • \$\begingroup\$ @Dennis - You may want to add a leading >. Your implementation runs off the tape to the left, which few interpreters support. \$\endgroup\$ – owacoder Jul 11 '16 at 14:14
  • 3
    \$\begingroup\$ @owacoder On PPCG, languages are defined by their implementations. As long as there is one interpreter that behaves as desired (and the one on Try it Online! does), it's considered valid. \$\endgroup\$ – Dennis Jul 11 '16 at 15:23
  • \$\begingroup\$ @owacoder Well actually a lot of interpreters support negative tape indices nowadays \$\endgroup\$ – ASCII-only Apr 27 '18 at 5:12
6
\$\begingroup\$

Brainfuck, 40 39 bytes

++++[->++++[->++>++++++<<]<]>>>-[-<.+>]

Try it online.

Explanation

++++[->++++[->++>++++++<<]<]

The nested loops basically mean you multiply the number of plusses together, so 4 × 4 × 2 = 32 in one cell and 4 × 4 × 6 = 96. Here is the tape after running this:

00 00 32 96
 ^

>>>- moves the pointer to the fourth cell and decrements it. Now we're done with the setup. 32 is the code for space, the first printable ASCII character. 95 is the number of characters we have to print. Here is the tape now:

00 00 32 95
          ^

[-<.+>] runs until the current cell (the fourth one) is zero. It decrements the counter and prints the character and increments it for the next time.

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

Cheddar, 29 bytes

->(32:126).map((i)->@"i).fuse

Range from 32-126, loop over it and get the string at the given char code @" and the fuse together (join)

Cheddar, 7 bytes

32@"126

Unfortunately this is broken as of the current release but I'm sure you can go back some versions where this works

\$\endgroup\$
  • \$\begingroup\$ eyyy my @" operator! \$\endgroup\$ – Conor O'Brien Jul 12 '16 at 0:22
  • \$\begingroup\$ @CᴏɴᴏʀO'Bʀɪᴇɴ it's a very useful operator 10/10 \$\endgroup\$ – Downgoat Jul 12 '16 at 0:22
5
\$\begingroup\$

Retina, 18 bytes


~
{2`
$`
T01`p`_p

The leading linefeed is significant.

Try it online!

Explanation

Stage 1: Substitution


~

We start by replacing the empty (non-existent) input with a single ~.

Stage 2: Substitution

{2`
$`

The regex of this substitution is still empty, since the ` separates configuration from regex and {2 is therefore just the configuration. The { indicates that the remaining two stages should be run in a loop until they stop changing the output. The 2 indicates that this specific stage has a limit of 2, meaning that only the first two matches of the regex will be replaced. Since the regex is empty, that means we get an empty match in front of the string and an empty match after the first character.

This match is replaced with the prefix $` which refers to everything in front of the match. For the first match, there is nothing in front of it, so this doesn't insert anything, but for the second match, there is the leading character in front of it, which therefore gets duplicated.

Stage 3: Transliteration

T01`p`_p

Here, T activates transliteration mode, and 0 and 1 are limits (where 0 just means "don't set this limit"). Together, they mean "transliterate only the first character in the string". The actual transliteration maps from p to _p. Here, p expands to the printable ASCII characters and _ means "remove" this character, so the expanded lists look like this:

 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
_ !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

That means spaces get removed and all other characters get decremented by one.

To see how the last two stages act together here is the string after each of the first few and last stages:

~
~~
}~
}}~
|}~
||}~
{|}~

...

"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
""#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
!!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
  !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~
 !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~

Since the state of the string is only checked after every other stage to determine whether to end the loop, and the two stages cancel each other once we reach the leading space (since Stage 2 adds a space and Stage 3 removes it), this terminates the loop and therefore the program.

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

Python 2.7, 36 bytes:

print''.join(map(chr,range(32,127)))

Simple enough. A full program that prints out the entire ASCII sequence in order.

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

Python 2, 30 bytes

print bytearray(range(32,127))

Try it online!

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

Pyth, 5 bytes

srd\

Try it here.

 r         range from
  d        space
   \<del>  to the delete character, 0x7F (included literally in the program)
s          sum (concatenate all)
\$\endgroup\$
3
\$\begingroup\$

Octave, 13 bytes

disp(' ':'~')

Try it on ideone.

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

JavaScript, 54 bytes

for(i=32;i<128;i++)console.log(String.fromCharCode(i))

Wasted quite a few bytes printing it... Also prints each char on a new line.

\$\endgroup\$
  • 5
    \$\begingroup\$ If you start the loop in 31, you can save one byte removing i++ and using String.fromCharCode(++i). \$\endgroup\$ – Washington Guedes Jul 11 '16 at 12:29
2
\$\begingroup\$

CJam, 6 bytes

',32>

The second byte is a DEL character. Try it online!

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

Perl 6, 17 bytes

print |(' '..'~')
print chrs ^95+32
\$\endgroup\$
2
\$\begingroup\$

C, 40 bytes

f(){for(char i=32;i<128;i++)putchar(i);}
\$\endgroup\$
  • \$\begingroup\$ New here, but do you have to declare a func? (without the f(){ is not ok?) \$\endgroup\$ – Ring Ø Dec 27 '18 at 7:25
  • 1
    \$\begingroup\$ @RingØ In C, you can't just write code without a function. each submission has to compile or run, so in JS for example it would be ok to not put the f() but it's required for C. \$\endgroup\$ – dkudriavtsev Dec 27 '18 at 9:18
2
\$\begingroup\$

Brachylog, 3 bytes

@Pw

@P is the string that contains the printable ASCII characters so… yeah, not very interesting.

It's not clear whether OP accepts built-ins or not, and since others have posted answers that use built-ins, I'll use the 3 bytes version until OP clarifies this point.

With no built-in, 14 bytes

32:126e          Get a number between 32 and 126
       :"~c"w    Format that number to STDOUT as a char code
             \   Backtrack
\$\endgroup\$
2
\$\begingroup\$

Ruby, 17 bytes

$><<[*' '..?~]*''

or

print *[*' '..?~]
\$\endgroup\$
2
\$\begingroup\$

Bash, 54 bytes

man ascii|fold -w1|LC_ALL=C sort -u|tr -cd '[:print:]'

Try it online!

I like that this uses the ASCII man page (which contains an ASCII table) to extract the characters.

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

Canvas, 1 byte

Try it here!


Without builtin:

15 14 bytes

9⁵×4-{⁷⁷++╷c]∑

Try it here!

\$\endgroup\$
  • \$\begingroup\$ :| that's really long without builtin \$\endgroup\$ – ASCII-only Apr 27 '18 at 1:35
  • \$\begingroup\$ @ASCII-only yeah. ⁴⁷⁷++╷ is the shortest way i could find to make the number 95 (Canvas only has literals for 0-9, and predefined variables for 10, Infinity, 256, 13, 64, 11, 12, 16, and 128.) \$\endgroup\$ – hakr14 Apr 27 '18 at 1:37
  • \$\begingroup\$ lol nvm found a shorter one \$\endgroup\$ – hakr14 Apr 27 '18 at 1:42
  • 2
    \$\begingroup\$ 10 bytes \$\endgroup\$ – ASCII-only Apr 27 '18 at 13:08
2
\$\begingroup\$

Java 8, 48 bytes

v->{for(char i=31;++i<127;)System.out.print(i);}

Try it online.

Explanation:

v->{                       // Method with empty unused parameter and no return-type
  for(char i=31;++i<127;)  //  Loop characters from ' ' to '~'
    System.out.print(i);}  //   And print it
\$\endgroup\$
2
\$\begingroup\$

TSQL, 53 bytes - Vertical solution

DECLARE @ int=32x:PRINT char(@)SET @+=1IF @<127GOTO x

Fiddle

TSQL, 75 71 68 bytes - Horizontal solution

DECLARE @ char(95)=''WHILE 95>LEN(@)SET @=char(126-LEN(@))+@ PRINT @

Fiddle

\$\endgroup\$
  • \$\begingroup\$ this can be executed on any version of sqlserver from this millennium \$\endgroup\$ – t-clausen.dk Jul 11 '16 at 8:11
  • 1
    \$\begingroup\$ Horisontal=>Horizontal ;) \$\endgroup\$ – aloisdg Jul 11 '16 at 11:40
  • 1
    \$\begingroup\$ The second solution appears to be missing the leading space. You can save 3 bytes by changing SELECT to SET. \$\endgroup\$ – BradC Apr 30 '18 at 15:51
  • \$\begingroup\$ @BradC you are absolutely right, thanks. Fixed - considering my more complex answers, I can't believe i missed something this obvious. \$\endgroup\$ – t-clausen.dk May 1 '18 at 7:39
  • 1
    \$\begingroup\$ @t-clausen.dk But I still don't get a leading space from your latest code, looks like all you did was change the SELECT to SET. If you change both 94s to 95 that seems to fix it. \$\endgroup\$ – BradC May 1 '18 at 13:38
1
\$\begingroup\$

Bash + general Linux utilities, 18

jot -s '' -c 95 32
\$\endgroup\$
1
\$\begingroup\$

SQL, 76 75 bytes

(Microsoft SQL Server 2012+)

declare @ int=32;while(@<127)begin;print char(@);set @+=1;end

Demo

\$\endgroup\$
  • \$\begingroup\$ Not sure outputting on separate lines satisfies the challenge. Also, can save a few bytes with a label/and GOTO instead of while. \$\endgroup\$ – BradC Apr 30 '18 at 14:29
1
\$\begingroup\$

Julia, 18 bytes

map(print,' ':'~')

Try it online!

If returning a string from a function is acceptable, a further byte can be saved.

f()=' ':'~'|>join
\$\endgroup\$
1
\$\begingroup\$

Haskell, 16 bytes

putStr[' '..'~']
\$\endgroup\$
1
\$\begingroup\$

Pyke, 5 bytes

~KS4>

Try it here!

Pyke's printable variable isn't sorted and it contains tabs and newlines etc... :(

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

C - 35 bytes

f(i){for(i=31;++i<128;putchar(i));}

Call:

int main() {
    f();
}

Uses the horrible int-as-a-string trick I learnt from Lynn. (Will again if OP confirms that a vertical output is OK).

\$\endgroup\$
  • \$\begingroup\$ That only works on little-endian machines though. putchar(i) is the same length. \$\endgroup\$ – owacoder Jul 11 '16 at 11:50
  • \$\begingroup\$ @owacoder mmh, I had puts there originally, but then I changed it because of the newlines and didn't question it further. Thanks! \$\endgroup\$ – Quentin Jul 11 '16 at 11:55
  • \$\begingroup\$ i;f(){for(;++i<96;putchar(i+32));} also works, and saves a byte. \$\endgroup\$ – owacoder Jul 11 '16 at 11:57
1
\$\begingroup\$

Excel VBA, 57 bytes:

Prints individual characters

Sub a()
For i = 32 To 126
Debug.Print Chr(i)
Next
End Sub

Prints out joined string, 68 bytes

Sub a()
For i = 32 To 126
b = b & Chr(i)
Next
Debug.Print b
End Sub
\$\endgroup\$
  • \$\begingroup\$ 27 bytes in VBA Immediate Window: For i=32To 126:?Chr(i):Next or, for all one string with no line breaks, 28 bytes: For i=32To 126:?Chr(i);:Next \$\endgroup\$ – Engineer Toast Apr 30 '18 at 13:29
1
\$\begingroup\$

Majc (formely hashmap), 6 bytes

r af

The delete character is unprintable so here's the hex code (xxd):

0000000: 7220 7f61 660a                           r .af.
\$\endgroup\$
1
\$\begingroup\$

C#, 51 45 40 bytes

for(char x=' ';x<=128;)Debug.Write(x++);
\$\endgroup\$
  • 1
    \$\begingroup\$ I think that this doesn't qualify just yet, since it isn't a full program or a function. Also, why do you cast the x to a char when giving x a value? \$\endgroup\$ – Yytsi Jul 11 '16 at 8:23
  • 1
    \$\begingroup\$ Yeah, but there is 2 casts in your code...? \$\endgroup\$ – Yytsi Jul 11 '16 at 8:41
  • 1
    \$\begingroup\$ You can rewrite your for to be shorter ()=>{for(int x=31;x<126;)Debug.Write((char)++x);}; (I also use a lambda to be pendatic with PPCG will. Here we use an Action instead of a Func) \$\endgroup\$ – aloisdg Jul 11 '16 at 11:32
  • \$\begingroup\$ You can remove your cast by incrementing your char. ()=>for(char x=' ';x<127;)Debug.Write(x++);}; \$\endgroup\$ – aloisdg Jul 11 '16 at 11:39
  • \$\begingroup\$ Also nice first answer! Feel free to step by the Tips for code-golfing in C# page for more tips! I just referenced this post in it ;) \$\endgroup\$ – aloisdg Jul 11 '16 at 11:58
1
\$\begingroup\$

PHP, 40 30 29 26 24 bytes

Thanks to Mego, manatwork and Titus for helping me golf this down.

<?=join(range(' ','~'));
\$\endgroup\$
  • \$\begingroup\$ I think #128 is out of printable ASCII. Anyway, range() can generate character sequence directly, no need to chr() them one by one. \$\endgroup\$ – manatwork Jul 11 '16 at 7:59
  • \$\begingroup\$ range is inclusive (unlike Python's range, which includes the start but not the end), so it should be range(32,127). \$\endgroup\$ – Mego Jul 11 '16 at 8:06
  • \$\begingroup\$ join()'s first parameter “Defaults to an empty string.” Then you have an unnecessary space after the second ,. \$\endgroup\$ – manatwork Jul 11 '16 at 8:48
  • \$\begingroup\$ Yepp, my explanation officially sucks. :( To translate my previous comment: join()'s first parameter is optional. \$\endgroup\$ – manatwork Jul 11 '16 at 9:33
  • \$\begingroup\$ I'm pretty sure you can replace echo with <?= and then save a byte. \$\endgroup\$ – mid Jul 11 '16 at 12:12
1
\$\begingroup\$

C, 33 bytes

i;f(){++i<96&&putchar(i+31)|f();}
\$\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.