26
\$\begingroup\$

Frequently while I'm code-golfing, I'll want to know what the ASCII value of a certain character is. One of my favorite resources for quickly looking up all of the printable ASCII characters is ASCIItable.com. This has a really nice image that not only shows the printable ASCII characters and their values, but also the unprintable and extended characters, and the values in hexadecimal, octal, and HTML:

enter image description here

Today's challenge is to recreate that ASCII table as an ASCII table instead of an image. To keep things simpler, we will not use control-characters (characters below 32) and we'll only show the decimal value and the character. In other words, your challenge is to write either a full-program or a function that prints or returns the following text:

Dec  Chr   | Dec  Chr   | Dec  Chr
----------------------------------
32   Space | 64   @     | 96   `
33   !     | 65   A     | 97   a
34   "     | 66   B     | 98   b
35   #     | 67   C     | 99   c
36   $     | 68   D     | 100  d
37   %     | 69   E     | 101  e
38   &     | 70   F     | 102  f
39   '     | 71   G     | 103  g
40   (     | 72   H     | 104  h
41   )     | 73   I     | 105  i
42   *     | 74   J     | 106  j
43   +     | 75   K     | 107  k
44   ,     | 76   L     | 108  l
45   -     | 77   M     | 109  m
46   .     | 78   N     | 110  n
47   /     | 79   O     | 111  o
48   0     | 80   P     | 112  p
49   1     | 81   Q     | 113  q
50   2     | 82   R     | 114  r
51   3     | 83   S     | 115  s
52   4     | 84   T     | 116  t
53   5     | 85   U     | 117  u
54   6     | 86   V     | 118  v
55   7     | 87   W     | 119  w
56   8     | 88   X     | 120  x
57   9     | 89   Y     | 121  y
58   :     | 90   Z     | 122  z
59   ;     | 91   [     | 123  {
60   <     | 92   \     | 124  |
61   =     | 93   ]     | 125  }
62   >     | 94   ^     | 126  ~
63   ?     | 95   _     | 127  DEL

Trailing spaces on each line, and a trailing newline are permitted. Since this is a challenge, your submission may not take any input, or access any external resources (such as a file or the web), and your goal is to compress the code to output this text as much as possible.

Standard loopholes apply, and the shortest answer in bytes wins. Happy golfing!

\$\endgroup\$
  • 3
    \$\begingroup\$ I have that exact ASCII table image stuck on my wall... Anyway, can a list of lines be returned? \$\endgroup\$ – FlipTack Jan 7 '17 at 15:48
  • 2
    \$\begingroup\$ @fliptack Of course you can. Why wouldn't I allow something perfectly reasonable like that? \$\endgroup\$ – DJMcMayhem Jan 7 '17 at 15:57
  • 10
    \$\begingroup\$ I wouldn't say this is a dupe - the other one requires hex values, names of unprintables, and is a different table format. This sticks to visible ASCII and allow golfier code by not asking for the 3-letter codes of all the unprintables. \$\endgroup\$ – FlipTack Jan 7 '17 at 16:12
  • \$\begingroup\$ @FlipTack It still has Space and DEL \$\endgroup\$ – simon Jan 7 '17 at 16:20
  • 2
    \$\begingroup\$ @gurka yes, but the other one has every single control character. \$\endgroup\$ – FlipTack Jan 7 '17 at 16:42

28 Answers 28

11
\$\begingroup\$

Python 2, 185 183 175 159 bytes

Saved 8 bytes thanks to FlipTack!

Still quite new to golfing in Python.

for a in["Dec  Chr   | "*3,"-"*39]+["".join(["%-5d%-6s| "%(l,('Space',chr(l),'DEL')[(l>32)+(l>126)]) for l in(i,32+i,64+i)])for i in range(32,64)]:print a[:-5]

Uses a nested list comprehension to generate the table body.

Ungolfed:

lines =   \
  ["Dec  Chr   | "*3, "-"*39] +                    # first two lines
    ["".join(                                        # join 3 parts of each line
      ["%-5d%-6s| " % (l, ('Space',chr(l),'DEL')[(l>32)+(l>126)]) 
        for l in (i,32+i,64+i)]                      # generate 3 parts of a line
      )
      for i in range(32,64)]

for line in lines: print line[:-5]

Old attempt, 185 183 175 bytes

print("Dec  Chr   | "*3)[:-5]+"\n"+"-"*34
a=lambda x:('Space',chr(x),'DEL')[(x>32)+(x>126)]
for l in range(32,64):print("%-5d%-6s| "*3%(l,a(l),l+32,a(l+32),l+64,a(l+64)))[:-5]

Ungolfed:

print ("Dec  Chr   | "*3)[:-5] + "\n" + "-"*34
def a(x):
    return "Space" if x==32 else "DEL" if x==127 else chr(x)
for line in range(32,64):
    print ("%-5d%-6s| "*3 % (line, a(line), line+32, a(line+32), 
      line+64, a(line+64))) [:-5]
\$\endgroup\$
  • \$\begingroup\$ I see a space at ` for l in(i,32+i,64+i)])`, could be removed to save one byte. \$\endgroup\$ – maxb Apr 25 '18 at 8:37
6
\$\begingroup\$

Befunge, 176 172 bytes

<v"Dec  Chr   "0
^>:#,_$1+:2`#v_" |",,
\:#->#1_55+,v>55+,"!-":>,#:
+2*,:"_"`#@_v>1+:8-#v_$1+:3%!:7g,!29+*5
*84+1%3\/3::<^,gg00:<`"c"p00+5+`"~"\`*84::p62:.:+*
  Space

| DEL

Try it online!

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

Pyth, 89 85 79 77 bytes

PP*"Dec  Chr   | "3*\-34V32PPsm++.[`=+N32;5.[?qN32"Space"?qN127"DEL"CN;6"| "3

Try it online!

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

JavaScript (ES6), 179 173 bytes

f=n=>n?(n>>6?' | ':`
`)+n+(99<n?'  ':'   ')+(126<n?'DEL':String.fromCharCode(n)+'    '+f(n>95?n-63:n+32)):`${x='Dec  Chr   '}| ${x}| ${x}
${'-'.repeat(34)}
32   Space`+f(64)

console.log(f())

\$\endgroup\$
  • \$\begingroup\$ Nice answer! You can save one more byte by replacing `${x='Dec Chr '}| ${x}| ${x} at the end of line 2 by (x='Dec Chr ')+(' |'+x)*2+` \$\endgroup\$ – Luke Jan 7 '17 at 18:53
  • \$\begingroup\$ @L.Serné I don't know of any version of ECMAScript that would repeat a string with the * operator. Or am I somehow misunderstanding your suggestion? \$\endgroup\$ – Arnauld Jan 7 '17 at 18:58
  • \$\begingroup\$ Oops, my bad. That's what you get for trying to golf a python solution and then javascript. \$\endgroup\$ – Luke Jan 7 '17 at 18:59
5
\$\begingroup\$

V, 98, 96, 94 bytes

i32 | 64 | 9631ñÙl.l.ñÍä«/&   &    
ÎéiD@"
bsDELF 27kdH5lRSpaceÄÒ-Ä3RDec  Chr³ | Î35|D

Try it online!

Just barely squeaking in under a hundred. I'm gonna see if I can beat Pyth, but I won't make any promises.

Here is a hexdump:

00000000: 6933 3220 7c20 3634 207c 2039 361b 3331  i32 | 64 | 96.31
00000010: f1d9 016c 2e6c 2ef1 cde4 ab2f 2620 2020  ...l.l...../&   
00000020: 1616 2620 2020 200a cee9 6944 4022 0a62  ..&    ...iD@".b
00000030: 7344 454c 1b46 2016 3237 6b64 4835 6c52  sDEL.F .27kdH5lR
00000040: 5370 6163 651b c4d2 2dc4 3352 4465 6320  Space...-.3RDec 
00000050: 2043 6872 b320 7c20 1bce 3335 7c44        Chr. | ..35|D

And here's how it works:

i32 | 64 | 96<esc>      " Insert some starting text
31ñ          ñ          " 31 times:
   Ù                    "   Duplicate this line
    <C-a>               "   Increment the first number on this line
         l.             "   Increment the next number
           l.           "   Increment the next number

Here is where it get's interesting. First, let me explain a vim-trick. While in insert mode, certain characters are inserted (all printable ASCII-characters, most unmapped characters above 0x7f, and a few others), but other characters have a side-effect. For example, 0x1b (<esc>) will escape to normal mode. 0x01 (<C-a>) will re-insert the last inserted text, etc. Sometimes, we want to insert these characters literally. So to insert a literal escape character, you must type <C-v><esc>. This works for all characters that have a side effect. So essentially, <C-v> is the equivalent of a backslash in languages with string literals that allow you to escape certain characters in a string.

The other useful trick with <C-v> in insert mode, is that it can be used to insert characters by code-point, in either Decimal, Hexadecimal, Octal, or Hexadecimal Unicode. Since we already have the numbers that correspond to certain ASCII values, we just need to put a <C-v> before those characters, and run the corresponding text as vim-keystrokes. This can be achieved with a regex command, and a "Do 'x' on every line" command. So we:

Í                       " Substitute globally:
 ä«                     "   One or more digits
   /                    " With:
    &                   "   The matched number + some spaces
        <C-v><C-v>&     "   A ctrl-v character, then the matched number again
                        "   Since ctrl-v is like a backslash, we need two to enter a literal ctrl-v character
Î                       " On every line:
 éi                     "   Insert an 'i'
   D                    "   Delete this line
    @"                  "   Run it as vim keystrokes

At this point, the buffer looks like this

32         | 64   @     | 96   `    
33   !     | 65   A     | 97   a    
34   "     | 66   B     | 98   b    
35   #     | 67   C     | 99   c    
36   $     | 68   D     | 100   d    
37   %     | 69   E     | 101   e    
38   &     | 70   F     | 102   f    
39   '     | 71   G     | 103   g    
40   (     | 72   H     | 104   h    
41   )     | 73   I     | 105   i    
42   *     | 74   J     | 106   j    
43   +     | 75   K     | 107   k    
44   ,     | 76   L     | 108   l    
45   -     | 77   M     | 109   m    
46   .     | 78   N     | 110   n    
47   /     | 79   O     | 111   o    
48   0     | 80   P     | 112   p    
49   1     | 81   Q     | 113   q    
50   2     | 82   R     | 114   r    
51   3     | 83   S     | 115   s    
52   4     | 84   T     | 116   t    
53   5     | 85   U     | 117   u    
54   6     | 86   V     | 118   v    
55   7     | 87   W     | 119   w    
56   8     | 88   X     | 120   x    
57   9     | 89   Y     | 121   y    
58   :     | 90   Z     | 122   z    
59   ;     | 91   [     | 123   {    
60   <     | 92   \     | 124   |    
61   =     | 93   ]     | 125   }    
62   >     | 94   ^     | 126   ~    
63   ?     | 95   _     | 127       

Now we just need some general clean up, which accounts for most of the bytes in this answer

bsDEL<esc>              " Change the literal 0x7f character to "DEL"
          F <C-v>27kd   " Remove a space from the lines that have too many
H5l                     " Move to the first space character
   RSpace<esc>          " And replace it with "Space"
Ä                       " Duplicate this line
 Ò-                     " And replace it with '-'s
   Ä                    " Duplicate this line
    3R                  " And replace it with three copies of the following string:
      Dec  Chr³ | <esc> " 'Dec  Chr   | '

Î35|D                   " Remove all but the first 35 characters of each line
\$\endgroup\$
5
\$\begingroup\$

F#, 222 bytes

let c,p=String.concat" | ",printfn"%s"
Seq.replicate 3"Dec  Chr  "|>c|>p
p(String.replicate 34"-")
for i=32 to 63 do[for j in[i;i+32;i+64]->sprintf"%-5d%-5s"j (match j with 32->"Space"|127->"DEL"|_->string(char j))]|>c|>p

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Can I ask for an ungolfed version of this please? I'm really new to learning F#, and I'd love to understand properly how you did this! \$\endgroup\$ – Ciaran_McCarthy Apr 25 '18 at 13:24
  • 1
    \$\begingroup\$ The first line aliases two functions to single-character names. Now if you do ["ab"; "cd"] |> c |> p it concatenates with "|" characters and prints them, like "ab | cd" which is the basic idea for printing the table. The rest is fairly straightforward, just avoiding whitespace wherever possible. \$\endgroup\$ – Asik Apr 25 '18 at 18:41
4
\$\begingroup\$

dc, 167 bytes

[[Space]nq]sp[[DEL]nq]sq[[ ]n]sc[Dec  Chr]dsen[   | ]dsfnlenlfnlen10P34[[-]n1-d0<a]dsax10P0[[32+dndZ2=c[  ]ndd32=pd127=qP[    ]n]dswx[ | ]nlwx[ | ]nlwx10P95-d32>b]dsbx

Try it online!

How it works:

[[Space]nq]sp     # p is a macro that prints "Space" and then quits from the call one level up
[[DEL]nq]sq       # q is a macro that prints "DEL" and then quits from the call one level up
[[ ]n]sc          # c is a macro that prints a space
[Dec  Chr]dsen    # Save the string "Dec  Chr" in register e, and print it.
[   | ]dsfn       # Save the string "   | " in register f, and print it.
len               # Print "Dec  Chr" again.
lfn               # Print "   | " again.
len               # Print "Dec  Chr" again.
10P               # Print a newline.
34                # Push 34 on the stack.

[[-]n1-d0<a]dsa   # a is a macro that repeatedly prints "-" and decrements the top of the stack, while the top of the stack is positive.

x10P              # Execute macro a, followed by a newline. (This prints the line of hyphens.)

0                 # Push 0 on the stack.

[                 # Starting a large macro (which will be stored in register b) for printing the table row by row.

[32+dndZ2=c[  ]ndd32=pd127=qP[    ]n]dsw

                  # w is a macro which:
                        (1) adds 32 to the top of the stack;
                        (2) prints it as a number;
                        (3) uses Z to compute the number of characters the number required to print that number;
                        (4) if it required 2 characters to print the number, calls the macro c to print an extra space
                        (5) prints the string "Space" (for ASCII code 32) or the string "DEL" (for ASCII code 127) or the appropriate character, followed by the right number of spaces

x                 # Execute macro w to print an entry in column 1.
[ | ]n            # Print a column divider.
lwx               # Execute macro w to print an entry in column 2 (ASCII code 32 higher than the previous entry).
[ | ]n            # Print a column divider.
lwx               # Execute macro w to print an entry in column 3 (ASCII code 32 higher than the previous entry).

10P               # Print a newline.
95-               # Subtract 95 to adjust to go to the beginning of the next line.

d32>b             # If the top of stack is <= 32, execute macro b again, effectively looping to print all the rows of the table.

]dsb              # End the definition of the large macro, and store it in register b.

x                 # Execute the macro that's in b (with 0 at the top of the stack initially).
\$\endgroup\$
4
\$\begingroup\$

Perl, 120 bytes

$,="| ";say+("Dec  Chr   ")x3;say"-"x32;say map{sprintf"%-5s%-6s",$_,$_-32?$_-127?chr:DEL:Space}$_,$_+32,$_+64for 32..63

Run with -E flag:

perl -E '$,="| ";say+("Dec  Chr   ")x3;say"-"x32;say map{sprintf"%-5s%-6s",$_,$_-32?$_-127?chr:DEL:Space}$_,$_+32,$_+64for 32..63'

-2 bytes thanks to @G B.

\$\endgroup\$
  • \$\begingroup\$ If I understand a little Perl, maybe you can cut 2 spaces by using "%-5s" instead of "%-3s" (incidentally, that's what I do in ruby) \$\endgroup\$ – G B Jan 8 '17 at 13:46
3
\$\begingroup\$

C, 179 bytes

i;f(){for(;i++<37;)printf(i<4?"Dec  Chr%s":"-",i<3?"   | ":"\n");printf("\n32   Space | ");for(i=64;i<127;i+=i>95?-63:32)printf("%-5d%-6c%s",i,i,i>95?"\n":"| ");puts("127  DEL");}

Try it online!

Semi-ungolfed:

i;
f() {
  for(;i++<37;) printf(i<4?"Dec  Chr%s":"-",i<3?"   | ":"\n");

  printf("\n32   Space | ");
  for(i=64;i<127;i+=i>95?-63:32) printf("%-5d%-6c%s",i,i,i>95?"\n":"| ");
  puts("127  DEL");
}
\$\endgroup\$
3
\$\begingroup\$

Ruby, 124 bytes

 puts [["Dec  Chr   "]*3*"| ",?-*34,(0..31).map{|d|(1..3).map{|x|"%-5s%-6s"%[y=x*32+d,y<33?"Space":y>126?"DEL":y.chr]}*"| "}]
\$\endgroup\$
3
\$\begingroup\$

V, 151 150 148 136 135 130 129 125 bytes

12 bytes saved thanks to @nmjcman101 for using <C-v>g<C-a> for the numbers instead of line('.')

2 byte saved thanks to @DJMcMayhem for removing lines with leading spaces using ÇÓ/d and by using to remove extra spaces and rearranging stuff

This answer is in competition with @nmjcman101's V answer (which uses :set ve=all). But now, I found a way to remove those A ^[s and saved some bytes and we are at an even bytecount

iSpace 
¬!~Ó./&ò
iDELí^/31   
HlgGo| 63ÙkGld/Sp
$p/`
G$d/@
$p/64
G$d/S
$pÇÓ/d
/d
hdê/32
O34é-O!| !| !Ó!/Dec  Chr   

Try it online!

Hexdump:

00000000: 6953 7061 6365 200a 1bac 217e d32e 2f26  iSpace ...!~../&
00000010: f20a 6944 454c 1bed 5e2f 3331 2020 200a  ..iDEL..^/31   .
00000020: 1648 6c67 0147 6f7c 201b 3633 d96b 1647  .Hlg.Go| .63.k.G
00000030: 6c64 2f53 700a 2470 2f60 0a16 4724 642f  ld/Sp.$p/`..G$d/
00000040: 400a 2470 2f36 340a 1647 2464 2f53 0a24  @.$p/64..G$d/S.$
00000050: 70c7 d32f 640a 2f64 0a68 64ea 2f33 320a  p../d./d.hd./32.
00000060: 4f1b 3334 e92d 4f21 7c20 217c 2021 1bd3  O.34.-O!| !| !..
00000070: 212f 4465 6320 2043 6872 2020 20         !/Dec  Chr 

Explanation (uncomplete and outdated)

The strategy here is that I'm using the line numbers to generate the ASCII code points.

Note: ^[ is 0x1b, ^V is C-v

First we generate all the characters.

iSpace             " insert Space
^[¬!~              " insert every character between ! and ~

The current buffer looks like

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

Now we insert a newline between these characters

     Ó./&ò         " insert a newline before every character (:s/./&\r/g)
\$\endgroup\$
  • \$\begingroup\$ Here's a drop in replacement for the 32 SPACE ... 127 DEL part of your code: Try it online! It uses that neat thing where you can highlight a bunch of numbers, and then g^A makes it an increasing sequence (new in Vim 8?) \$\endgroup\$ – nmjcman101 Jan 7 '17 at 21:14
  • \$\begingroup\$ @nmjmcman101 Yeah, it was added in 7.4.something, but officially added in 8. Even better would be to use the norm command Try it online! \$\endgroup\$ – DJMcMayhem Jan 7 '17 at 21:22
  • \$\begingroup\$ Either way, once you get that you can do a :set ve=all and then the cursor will move into places that there is no text, letting you insert the pipes easier and copy/paste in the places you need to without sprinkling A <esc> everywhere \$\endgroup\$ – nmjcman101 Jan 7 '17 at 21:39
  • \$\begingroup\$ @nmjcman101 Re g^A thanks, it saved me 12 bytes :) \$\endgroup\$ – Cows quack Jan 8 '17 at 8:06
  • \$\begingroup\$ @DJMcMayhem For some reason, using norm and then incrementing the numbers doesn't work \$\endgroup\$ – Cows quack Jan 8 '17 at 8:09
3
\$\begingroup\$

V, 130 120 99 bytes

Sub 100 club. I'm no longer convinced that :se ve=all is the best way of doing this. It's an extra... 11 bytes just for writing the |'s! But that's what I have.

I'm posting this almost in competition with @KritixiLuthos answer using :se ve=all to avoid some A <esc>'s. I'm not convinced that either method is better yet, so hopefully this can inspire some golfing on both parties and see which method takes the cake.

I'm also half expecting @DJMcMayhem to kick both our pants

iSpace
¬!~Ó./&ò
iDELí^/31   
Hlg:se ve=all
12|êr|2ñ031j$x)PñHd)ÄÒ-Ä3RDec  Chr³ | /d
hdêÎ35|D

Try it online!

Hexdump for the curious (if there's interest I'll just change this to a vim-style hidden character block)

00000000: 6953 7061 6365 0a1b ac21 7ed3 2e2f 26f2  iSpace...!~../&.
00000010: 0a69 4445 4c1b ed5e 2f33 3120 2020 0a16  .iDEL..^/31   ..
00000020: 486c 6701 3a73 6520 7665 3d61 6c6c 0a31  Hlg.:se ve=all.1
00000030: 327c 16ea 727c 32f1 3016 3331 6a24 7829  2|..r|2.0.31j$x)
00000040: 50f1 4864 29c4 d22d c433 5244 6563 2020  P.Hd)..-.3RDec  
00000050: 4368 72b3 207c 201b 2f64 0a68 64ea ce33  Chr. | ./d.hd..3
00000060: 357c 44                                  5|D
\$\endgroup\$
  • 1
    \$\begingroup\$ The global command could be shortened by quite a but. For one, the dd is implicit if you just do d. You can also do <M-s> which is always equivalent to \s in regex. So you could shorten it to: ç^ó*/d. But if you switch out the global command for the inverse :g! you could search for every line not matching a non-whitespace character. ÇÓ/d which is equivalent to :g!/\S/normal dd \$\endgroup\$ – DJMcMayhem Jan 11 '17 at 22:59
3
\$\begingroup\$

AWK, 200 bytes

BEGIN{b="-----------";a="Dec  Chr   ";print a"|",a"|",a"\n-"b b b;for(a=31;a++<63;){printf"%-5d%-6s| %-5d%-6c| %-5d%-5s\n",a,a<33?"Space":sprintf("%c",a),a+32,a+32,a+64,a<63?sprintf("%c",a+64):"DEL"}}

Formatted:

BEGIN {
  b="-----------"
  a="Dec  Chr   "
  print a"|",a"|",a"\n-"b b b
  for(a=31;a++<63;) {
    printf "%-5d%-6s| %-5d%-6c| %-5d%-5s\n",
      a,    a<33 ? "Space" : sprintf("%c", a),
      a+32, a+32,
      a+64, a<63 ? sprintf("%c", a+64) : "DEL"
  }
}

Try it online!

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

C 188 Bytes

f(){i=31;printf("Dec Chr | Dec Chr | Dec Chr");printf("\n--------------------------");for(;i<63;i++)printf("\n%d%4c  | %d%4c  | %d%4c",(i+1),(i+1),(i+33),(i+33),(i+65),(i+65));puts("DEL");

Normally looks like this:

f()
{
    int  i=31;
    printf("Dec Chr | Dec Chr | Dec Chr");
    printf("\n--------------------------"); 
    for(;i<63;i++)  
      printf("\n%d%4c  | %d%4c  | %d%4c", (i+1),(i+1),(i+33),(i+33),  (i+65),(i+65));
    puts("DEL");
}
\$\endgroup\$
2
\$\begingroup\$

C (249 bytes)

Newlines added for clarity.

#define L(s,e)for(i=s;i<e;++i)
#define P printf
main(i){L(0,3)P("Dec  Chr  %s",i<2?" | ":"\n");
L(0,34)P("-");P("\n");L(32,64){P("%-5d", i);
i==32?P("Space"):P("%-5c",i);
P(" | %-5d%-5c | %-5d ",i+32,i+32,i+64);
i==63?P("DEL"):P("%-5c",i+64);P("\n");}}
\$\endgroup\$
  • \$\begingroup\$ You can definitely save some bytes by making P be printf(, as shown here: repl.it/JBRD \$\endgroup\$ – Zacharý Jun 25 '17 at 15:18
  • \$\begingroup\$ And by removing the space on your fourth line. \$\endgroup\$ – Zacharý Jun 25 '17 at 15:20
2
\$\begingroup\$

Java, 434 422 321 bytes

class A{public static void main(String[]a){
    int k=1,r,s=32;
    for(;k<4;k++)
        o("Dec   Chr  ",k);
    for(;k<37;k++)                                                                              
        o("-",k==36?3:4);
    for(k=r=s;!(k==64&&r==-63);r=k>95?-63:s,k+=r)
        o(k+"   "+((k>99)?"":" ")+(k==s?"Space":k==127?"DEL  ":((char)k+"    ")),k/s);
    }
    static void o(String s,int j){
        System.out.print(s+(j==4?"":j==3?"\n":"|"));
    }
}

Java is probably not the best language for this as there is the overhead of classes and main method...

You can eliminate main method using a static declaration, reducing the byte count down further:

class A{
    static{...}

but this results in an error (after otherwise successfully running):

Exception in thread "main" java.lang.NoSuchMethodException: A.main([Ljava.lang.String;)
    at java.lang.Class.getMethod(Class.java:1786)
    ...

The byte count does int include newlines or indentation.

\$\endgroup\$
  • \$\begingroup\$ I don't think you need a space after void main(){, and neither between while(s.length()<5) and s+=" ". (Unless you didn't count that in your byte count). And you can save a few bytes by changing d="Dec",c="Chr" to d=p("Dec"),c=p("Chr") and making changing your calls p(d) and p(c) to d and c. \$\endgroup\$ – Zacharý Jun 25 '17 at 15:15
  • \$\begingroup\$ The 434 was removing all unnecessary whitespace. Your suggestion brings it down to 425. Thanks! \$\endgroup\$ – xirt Jun 28 '17 at 17:03
  • \$\begingroup\$ Might want to add a note about the 434 being w/o unneeded whitespace into the answer \$\endgroup\$ – Zacharý Jun 28 '17 at 17:47
  • \$\begingroup\$ Done. Note: program has significantly changed since (reduced further) so comments above may no longer be relevant \$\endgroup\$ – xirt Jun 29 '17 at 19:32
2
\$\begingroup\$

Tcl, 233 bytes

lassign {"Dec  Chr" "   | " 31} h S i
set H $h$S$h$S$h\n[string repe - 34]
proc p x {format %-5d%c $x $x}
time {set H "$H
[p [incr i]]  $S[p [expr $i+32]]  $S[p [expr $i+64]]"} 32
puts [regsub {\ {8}} [regsub \177 $H DEL] "   Space"]

Try it online!

\$\endgroup\$
  • \$\begingroup\$ I ran this and I get 32 for example (a ' ' instead of Space), and 33! (no space in-between 33 and !). \$\endgroup\$ – NoOneIsHere Jan 9 '17 at 2:23
  • \$\begingroup\$ @SeeOneRhino: You are clearly not paying attention to the code. I modified the code of the link after pasting here, in attempt to golf it even more, but I did not succeed yet. I just went to page now and commented my failed attempt and repasted the code from here;If you go there now, you will see code exactly equal and you will see it print things right! \$\endgroup\$ – sergiol Jan 9 '17 at 2:56
  • \$\begingroup\$ I am sorry, I didn't see your edit to the link. I assumed that the code was the same as what you had posted here. \$\endgroup\$ – NoOneIsHere Jan 9 '17 at 2:58
  • \$\begingroup\$ No problem, it was my fault \$\endgroup\$ – sergiol Jan 9 '17 at 2:59
  • \$\begingroup\$ @SeeOneRhino: I achieved it! I outgolfed myself! \$\endgroup\$ – sergiol Jan 10 '17 at 0:07
2
\$\begingroup\$

R, 235 228 221 212 bytes

y=apply(rbind(rep("Dec  Chr  ",3),1,matrix(sapply(1:96,function(i)paste(i+31,rep(" ",i<69),"if"(i<2,"Space","if"(i>95,"DEL",intToUtf8(c(i+31,rep(32,4))))))),nrow=32)),1,paste,collapse=" | ")
y[2]=strrep("-",34)
y

Try it online!

I tried really hard to get under 200 bytes but these paste and collapse are killing me. Returns a list of lines.

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

JavaScript (ES6), 258 bytes

a="Dec  Chr   | ".repeat(2)+"Dec  Chr\n"+"-".repeat(34);for(b=32;64>b;b++)a+="\n"+b+"   "+(32==b?"Space  ":String.fromCharCode(b)+"      ")+"| "+(b+32)+"   "+String.fromCharCode(b+32)+"     | "+(b+64)+(35<b?"  ":"   ")+(63==b?"DEL":String.fromCharCode(b+64))

console.log(a)
\$\endgroup\$
1
\$\begingroup\$

PowerShell, 159 bytes

,'Dec  Chr'*3-join'   | '
'-'*34
32..63|%{($_,($_+32),($_+64)|%{"$_".PadRight(5)+"$(([char]$_,('Space','DEL')[$_-ne32])[$_-in32,127])".padRight(5)})-join' | '}

Try it online!

The first two lines are just creating literal strings and leaving them on the pipeline. The first uses the comma operator , to create an array, and then -joins that array together to create the headers. The second is just a straight string multiplication.

The third line loops over 32..63 and each iteration sends three values $_, ($_+32), and ($_+64) into an inner loop. The inner loop does a PadRight on the value (adds the appropriate spaces to pad to 5 characters). That is then string concatenated + with the result of a nested pseudo-ternary ( )[ ]. The pseudo-ternary selects either the char representation of that number, or else Space or DEL if it's the appropriate value. Again, we PadRight the appropriate characters.

Those three strings (for example, 32 Space, 64 @, 96 `) are encapsulated in parens and -joined with the column markers into a single string. Each of those 32 strings are then left on the pipeline. At the end of execution, an implicit Write-Output inserts a newline between elements on the pipeline, so we get that for free.

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

Perl, 165 155 bytes

$s='Dec  Chr   ';$_=join"\n",("$s| $s| $s","-"x34,map{join"| ",map{sprintf'%1$-5d%1$-6c',$_}($_,$_+32,$_+64)}32..63);s/ {8}/   Space/;s/\x7f.*/DEL\n/;print
\$\endgroup\$
1
\$\begingroup\$

Python 2, 1564 218 bytes

My first golf, sorry for obvious mistakes

print("Dec  Chr   | "*3)[:-2]+"\n"+"-"*34+"\n32   Space | 64   @     | 96   `"
for n in range(33,63):print"| ".join([str(n+x).ljust(5)+chr(n+x).ljust(6)for x in [0,32,64]])
print"63   ?     | 95   _     | 127  DEL"

Try it online!

Incase you're wondering, the first version was a base64 encoded string.

\$\endgroup\$
  • \$\begingroup\$ @FlipTack Changed it to an actual solution \$\endgroup\$ – sagiksp Jan 7 '17 at 19:42
  • \$\begingroup\$ Unrequired whitespace at ljust(6) for. \$\endgroup\$ – Yytsi Jan 13 '17 at 17:28
  • \$\begingroup\$ Another at x in [. And IIRC the square brackets inside join can deleted. \$\endgroup\$ – Yytsi Jan 14 '17 at 8:29
1
\$\begingroup\$

05AB1E, 82 76 bytes

žQSDÇƵQ¸«.Bs𔇲”:"DEL"¸«.B)øvyð2×ý})3äøvy… | ©ý}®”†…  Chr  ÿ”3ר¨'-34×.Á.Á»

Try it online!

Still golfing, this can be improved a lot.


žQSDÇƵQ¸«.Bs𔇲”:"DEL"¸«.B)ø pushes padded numbers with text equivalent:

[['32 ', 'Space'], ['33 ', '!    '], ['34 ', '"    '], ['35 ', '#    '], ['36 ', '$    '], ['37 ', '%    '], ['38 ', '&    '], ['39 ', "'    "], ['40 ', '(    '], ['41 ', ')    '], ['42 ', '*    '], ['43 ', '+    '], ['44 ', ',    '], ['45 ', '-    '], ['46 ', '.    '], ['47 ', '/    '], ['48 ', '0    '], ['49 ', '1    '], ['50 ', '2    '], ['51 ', '3    '], ['52 ', '4    '], ['53 ', '5    '], ['54 ', '6    '], ['55 ', '7    '], ['56 ', '8    '], ['57 ', '9    '], ['58 ', ':    '], ['59 ', ';    '], ['60 ', '<    '], ['61 ', '=    '], ['62 ', '>    '], ['63 ', '?    '], ['64 ', '@    '], ['65 ', 'A    '], ['66 ', 'B    '], ['67 ', 'C    '], ['68 ', 'D    '], ['69 ', 'E    '], ['70 ', 'F    '], ['71 ', 'G    '], ['72 ', 'H    '], ['73 ', 'I    '], ['74 ', 'J    '], ['75 ', 'K    '], ['76 ', 'L    '], ['77 ', 'M    '], ['78 ', 'N    '], ['79 ', 'O    '], ['80 ', 'P    '], ['81 ', 'Q    '], ['82 ', 'R    '], ['83 ', 'S    '], ['84 ', 'T    '], ['85 ', 'U    '], ['86 ', 'V    '], ['87 ', 'W    '], ['88 ', 'X    '], ['89 ', 'Y    '], ['90 ', 'Z    '], ['91 ', '[    '], ['92 ', '\\    '], ['93 ', ']    '], ['94 ', '^    '], ['95 ', '_    '], ['96 ', '`    '], ['97 ', 'a    '], ['98 ', 'b    '], ['99 ', 'c    '], ['100', 'd    '], ['101', 'e    '], ['102', 'f    '], ['103', 'g    '], ['104', 'h    '], ['105', 'i    '], ['106', 'j    '], ['107', 'k    '], ['108', 'l    '], ['109', 'm    '], ['110', 'n    '], ['111', 'o    '], ['112', 'p    '], ['113', 'q    '], ['114', 'r    '], ['115', 's    '], ['116', 't    '], ['117', 'u    '], ['118', 'v    '], ['119', 'w    '], ['120', 'x    '], ['121', 'y    '], ['122', 'z    '], ['123', '{    '], ['124', '|    '], ['125', '}    '], ['126', '~    '], ['127', 'DEL  ']]

vyð2×ý})3äøvy… | ©ý} joins 'em together into the table:

['32   Space | 64   @     | 96   `    ', '33   !     | 65   A     | 97   a    ', '34   "     | 66   B     | 98   b    ', '35   #     | 67   C     | 99   c    ', '36   $     | 68   D     | 100  d    ', '37   %     | 69   E     | 101  e    ', '38   &     | 70   F     | 102  f    ', "39   '     | 71   G     | 103  g    ", '40   (     | 72   H     | 104  h    ', '41   )     | 73   I     | 105  i    ', '42   *     | 74   J     | 106  j    ', '43   +     | 75   K     | 107  k    ', '44   ,     | 76   L     | 108  l    ', '45   -     | 77   M     | 109  m    ', '46   .     | 78   N     | 110  n    ', '47   /     | 79   O     | 111  o    ', '48   0     | 80   P     | 112  p    ', '49   1     | 81   Q     | 113  q    ', '50   2     | 82   R     | 114  r    ', '51   3     | 83   S     | 115  s    ', '52   4     | 84   T     | 116  t    ', '53   5     | 85   U     | 117  u    ', '54   6     | 86   V     | 118  v    ', '55   7     | 87   W     | 119  w    ', '56   8     | 88   X     | 120  x    ', '57   9     | 89   Y     | 121  y    ', '58   :     | 90   Z     | 122  z    ', '59   ;     | 91   [     | 123  {    ', '60   <     | 92   \\     | 124  |    ', '61   =     | 93   ]     | 125  }    ', '62   >     | 94   ^     | 126  ~    ', '63   ?     | 95   _     | 127  DEL  ']

®”†… Chr ÿ”3ר¨'-34×.Á.Á» takes care of the header portion of the table:

Dec  Chr   | Dec  Chr   | Dec  Chr   
----------------------------------
32   Space | 64   @     | 96   `    
33   !     | 65   A     | 97   a    
34   "     | 66   B     | 98   b    
35   #     | 67   C     | 99   c    
36   $     | 68   D     | 100  d    
37   %     | 69   E     | 101  e    
38   &     | 70   F     | 102  f    
39   '     | 71   G     | 103  g    
40   (     | 72   H     | 104  h    
41   )     | 73   I     | 105  i    
42   *     | 74   J     | 106  j    
43   +     | 75   K     | 107  k    
44   ,     | 76   L     | 108  l    
45   -     | 77   M     | 109  m    
46   .     | 78   N     | 110  n    
47   /     | 79   O     | 111  o    
48   0     | 80   P     | 112  p    
49   1     | 81   Q     | 113  q    
50   2     | 82   R     | 114  r    
51   3     | 83   S     | 115  s    
52   4     | 84   T     | 116  t    
53   5     | 85   U     | 117  u    
54   6     | 86   V     | 118  v    
55   7     | 87   W     | 119  w    
56   8     | 88   X     | 120  x    
57   9     | 89   Y     | 121  y    
58   :     | 90   Z     | 122  z    
59   ;     | 91   [     | 123  {    
60   <     | 92   \     | 124  |    
61   =     | 93   ]     | 125  }    
62   >     | 94   ^     | 126  ~    
63   ?     | 95   _     | 127  DEL  
\$\endgroup\$
1
\$\begingroup\$

PHP, 163 149 147 bytes

<?=($p=str_pad)(D,31,"ec Chr   | D"),$p("
",32,"-");whhile($i<96)printf("%s%-4d%-6s",$i%3?"| ":"
",$o=$i%3*32+32+$i/3,$i++?$i<96?chr($o):DEL:Space);

breakdown

                        # print header
<?=($p=str_pad)(D,31,"ec Chr   | D"),$p("\n",32,"-");
while($i<96)            # loop $i from 0 to 96
    printf("%s%-4d%-6s",    # print formatted:
                            # string, 4 space decimal leftbound, 6 space string leftbound
        $i%3?"| ":"\n",                 # linebreak for 1st column, pipe+space else
        $o=$i%3*32+32+$i/3,             # ($i mapped to) ASCII value
        $i++?$i<96?chr($o):DEL:Space    # character
    );

Using %-N is worth the byte that rightbound numbers and character would save.

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

Stax, 53 bytes

éV ►Γ┐º╬¼1N_◄╒0└♀α→┴♂┐Y♣╟n»┤3°k⌡{═╥I⌂╜<─W}íè7Y♠Σ▲ÿb#√

Run and debug it

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

T-SQL, 242 bytes

DECLARE @ INT=32PRINT'Dec  Chr   | Dec  Chr   | Dec  Chr
'+REPLICATE('-',34)L:PRINT CONCAT(@,'   ',IIF(@=32,'Space | ',CHAR(@)+'     | '),@+32,'   ',CHAR(@+32),'     | ',@+64,SPACE(5-LEN(@+64)),IIF(@=63,'DEL',CHAR(@+64)))SET @+=1IF @<64GOTO L

Formatted:

DECLARE @ INT=32
PRINT'Dec  Chr   | Dec  Chr   | Dec  Chr
' + REPLICATE('-',34)
L:
    PRINT CONCAT(@,'   ',IIF(@=32,'Space | ',CHAR(@)+'     | ')
                ,@+32,'   ',CHAR(@+32),'     | ',
                 @+64,SPACE(5-LEN(@+64)),IIF(@=63,'DEL',CHAR(@+64)))
    SET @+=1
IF @<64 GOTO L

Tried a few variations, including replacing various common strings with other variables, but this was the shortest version I found.

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

Python 3, 154 bytes

for l in[['Dec  Chr  ']*3,['-'*35]]+[[f"{x:<5}{['Space',chr(x),'DEL'][(x>32)+(x>126)]:5}"for x in(c,c+32,c+64)]for c in range(32,64)]:print(' | '.join(l))

Try it online!

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

Attache, 144 bytes

(p:=PadRight)["",37,"Dec  Chr   | "]'(34*"-")'(Join&"| "=>Tr@ChopInto&3<|{p&11<|p&5@Repr@_+(<~32->$Space,127->$DEL~>@_or Char!_)}=>32:127)|Print

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.