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:
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 kolmogorov-complexity 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!
-
3\$\begingroup\$ I have that exact ASCII table image stuck on my wall... Anyway, can a list of lines be returned? \$\endgroup\$FlipTack– FlipTack2017年01月07日 15:48:45 +00:00Commented Jan 7, 2017 at 15:48
-
2\$\begingroup\$ @fliptack Of course you can. Why wouldn't I allow something perfectly reasonable like that? \$\endgroup\$DJMcMayhem– DJMcMayhem2017年01月07日 15:57:15 +00:00Commented Jan 7, 2017 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– FlipTack2017年01月07日 16:12:03 +00:00Commented Jan 7, 2017 at 16:12
-
\$\begingroup\$ @FlipTack It still has Space and DEL \$\endgroup\$simon– simon2017年01月07日 16:20:18 +00:00Commented Jan 7, 2017 at 16:20
-
2\$\begingroup\$ @gurka yes, but the other one has every single control character. \$\endgroup\$FlipTack– FlipTack2017年01月07日 16:42:38 +00:00Commented Jan 7, 2017 at 16:42
40 Answers 40
Python (削除) 2 (削除ここまで) 3.6, (削除) 185 (削除ここまで) (削除) 183 (削除ここまで) (削除) 175 (削除ここまで) (削除) 159 (削除ここまで) 156 bytes
Saved 8 bytes thanks to FlipTack!
Still quite new to golfing in Python.
for a in["Dec Chr | "*3,"-"*39]+["".join(f"{l:<5}{('Space',chr(l),'DEL')[(l>32)+(l>126)]:<6}| "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
f"{l:<5}{('Space',chr(l),'DEL')[(l>32)+(l>126)]:<6}| "
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]
Update: apparently using f-string here is shorter than the %
operator.
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]
-
\$\begingroup\$ I see a space at ` for l in(i,32+i,64+i)])`, could be removed to save one byte. \$\endgroup\$maxb– maxb2018年04月25日 08:37:06 +00:00Commented Apr 25, 2018 at 8:37
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
V, (削除) 98, 96 (削除ここまで), 94 bytes
i32 | 64 | 9631ñÙl.l.ñÍä«/& &
ÎéiD@"
bsDELF 27kdH5lRSpaceÄÒ-Ä3RDec Chr3 | Î35|D
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 Chr3 | <esc> " 'Dec Chr | '
Î35|D " Remove all but the first 35 characters of each line
JavaScript (Node.js), 158 bytes
-11 by switching to Node.js, as suggested by Fhuvi
-1 thanks to Shaggy
f=n=>n?(n>>6?" | ":`
`)+(n+"").padEnd(5)+(126<n?"DEL":Buffer([n])+" "+f(n+=n>95?-63:32)):`
${"-".repeat(34)}
32 Space`.padStart(82,"Dec Chr | ")+f(64)
-
\$\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– Luke2017年01月07日 18:53:12 +00:00Commented Jan 7, 2017 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– Arnauld2017年01月07日 18:58:00 +00:00Commented Jan 7, 2017 at 18:58 -
\$\begingroup\$ Oops, my bad. That's what you get for trying to golf a python solution and then javascript. \$\endgroup\$Luke– Luke2017年01月07日 18:59:56 +00:00Commented Jan 7, 2017 at 18:59
-
1\$\begingroup\$ It seems possible to use
Buffer([n])
instead ofString.fromCharCode(n)
\$\endgroup\$Fhuvi– Fhuvi2024年09月10日 13:48:56 +00:00Commented Sep 10, 2024 at 13:48 -
1
Pyth, (削除) 89 (削除ここまで) (削除) 85 (削除ここまで) (削除) 79 (削除ここまで) 77 bytes
PP*"Dec Chr | "3*\-34V32PPsm++.[`=+N32;5.[?qN32"Space"?qN127"DEL"CN;6"| "3
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.
-
\$\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– G B2017年01月08日 13:46:46 +00:00Commented Jan 8, 2017 at 13:46
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
-
\$\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– Ciaran_McCarthy2018年04月25日 13:24:25 +00:00Commented Apr 25, 2018 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– Asik2018年04月25日 18:41:06 +00:00Commented Apr 25, 2018 at 18:41
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 dê
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
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 (incomplete 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)
-
\$\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 theng^A
makes it an increasing sequence (new in Vim 8?) \$\endgroup\$nmjcman101– nmjcman1012017年01月07日 21:14:52 +00:00Commented Jan 7, 2017 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– DJMcMayhem2017年01月07日 21:22:56 +00:00Commented Jan 7, 2017 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 sprinklingA <esc>
everywhere \$\endgroup\$nmjcman101– nmjcman1012017年01月07日 21:39:06 +00:00Commented Jan 7, 2017 at 21:39 -
\$\begingroup\$ @nmjcman101 Re
g^A
thanks, it saved me 12 bytes :) \$\endgroup\$user41805– user418052017年01月08日 08:06:50 +00:00Commented Jan 8, 2017 at 8:06 -
\$\begingroup\$ @DJMcMayhem For some reason, using norm and then incrementing the numbers doesn't work \$\endgroup\$user41805– user418052017年01月08日 08:09:44 +00:00Commented Jan 8, 2017 at 8:09
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
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).
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 Chr3 | /d
hdêÎ35|D
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
-
1\$\begingroup\$ The global command could be shortened by quite a but. For one, the
dd
is implicit if you just dod
. 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– DJMcMayhem2017年01月11日 22:59:18 +00:00Commented Jan 11, 2017 at 22:59
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");}
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");
}
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]}*"| "}]
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"
}
}
-
\$\begingroup\$
END{print(a="Dec Chr ")"|",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"}}
to shave a few \$\endgroup\$xrs– xrs2025年04月04日 20:00:00 +00:00Commented Apr 4 at 20:00
PHP, (削除) 163 149 (削除ここまで) 146 bytes
<?=($p=str_pad)(D,31,"ec Chr | D"),$p("
",32,"-");while($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.
-
\$\begingroup\$ Looks better in my opinion with the same byte count Try it online! \$\endgroup\$Jörg Hülsermann– Jörg Hülsermann2017年06月25日 15:21:12 +00:00Commented Jun 25, 2017 at 15:21
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");
}
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");}}
-
\$\begingroup\$ You can definitely save some bytes by making
P
beprintf(
, as shown here: repl.it/JBRD \$\endgroup\$Adalynn– Adalynn2017年06月25日 15:18:39 +00:00Commented Jun 25, 2017 at 15:18 -
\$\begingroup\$ And by removing the space on your fourth line. \$\endgroup\$Adalynn– Adalynn2017年06月25日 15:20:54 +00:00Commented Jun 25, 2017 at 15:20
-
\$\begingroup\$ 229 bytes \$\endgroup\$ceilingcat– ceilingcat2020年03月09日 00:41:17 +00:00Commented Mar 9, 2020 at 0:41
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
I tried really hard to get under 200 bytes but these paste
and collapse
are killing me.
Returns a list of lines.
Java (JDK), 192 (削除) 194 (削除ここまで) (削除) 200 (削除ここまで) bytes
-6 bytes with the help of Khuldraeseth na'Barya
-2 bytes with more refactoring (column separators conditioned instead of concatenated, and-
managed by pairs to keep the modulus correct without a specific condition for the last one. But this somehow made the decimal values calculations longer...)
Function that outputs the wanted text.
It uses String.format "%1$-5s%2$-6s"
to manage the space padding to match the columns' fixed size.
And there is some calculation to get the correct decimal values and their corresponding characters.
The header line (Dec Chr
) is managed inside of the loop.
()->{var r="";for(int i=0,j=0;++i<216;j=i/2%3*32+28+i/2/3)r+=i>6&i<24?"--":i%2>0?String.format("%1$-5s%2$-6s",i<9?"Dec":j,i<9?"Chr":j<33?"Space":j>126?"DEL":(char)j):i%3>0?"| ":"\n";return r;}
Alternative close try (193 bytes) where we manage each "half-column" with mutualized formatting code:
()->{var r="";for(int i=0,j=0;++i<315;j=i%9/3*32+i/9+29){r+=i>9&i<27?"--":i%3>0?String.format("%1$-5s",i%3<2?i<9?"Dec":j:i<9?"Chr":j<33?"Space":j>126?"DEL":(char)j):i%9>0?" | ":"\n";}return r;}
Alternative close try (194 bytes) where we manage the header line outside of the loop:
()->{String p=" | ",r="Dec Chr ";r+=p+r+p+r+"\n";for(int i=2,j=0;++i<133;j=i%3*32+20+i/3)r+=i<37?"-":String.format((i%3==1?"\n":p)+"%1$-5s%2$-5s",j,j<33?"Space":j>126?"DEL":(char)j);return r;}
-
1\$\begingroup\$
(i-36)%3
->i%3
to save five bytes \$\endgroup\$Khuldraeseth na'Barya– Khuldraeseth na'Barya2024年09月05日 16:31:49 +00:00Commented Sep 5, 2024 at 16:31 -
\$\begingroup\$ @Khuldraesethna'Barya Oh you're right, thank you! I was so busy with the refactorings and multiple alternative tries that i didn't see this simplification :) \$\endgroup\$Fhuvi– Fhuvi2024年09月06日 08:03:25 +00:00Commented Sep 6, 2024 at 8:03
Zsh, 151 bytes
t='Dec Chr | ';<<<$t$t$t[1,8];jot -s-- -b- 12;c=(Space {!..~} DEL)
for n ({0..31})(for i (32 64 96)printf '%-5s%-6s| ' $[n+i] $c[n+i-31])|cut -c -34
Try it online!
(削除) 164b (削除ここまで)
(削除) 174b (削除ここまで)
(削除) 182b (削除ここまで)
(削除) 193b (削除ここまで)
(削除) 239b (削除ここまで)
Tcl, 217 bytes
puts [set h "Dec Chr"][set S " | "]$h$S$h\n[string repe - 34]
proc p x {format %-5d%c $x $x}
time {puts [regsub \ {8} [regsub 177円 "[p $i] $S[p [expr $i+32]] $S[p [expr [incr i]+63]]" DEL] " Space"]} [set i 32]
-
\$\begingroup\$ I ran this and I get
32
for example (a ' ' instead ofSpace
), and33!
(no space in-between33
and!
). \$\endgroup\$NoOneIsHere– NoOneIsHere2017年01月09日 02:23:44 +00:00Commented Jan 9, 2017 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– sergiol2017年01月09日 02:56:55 +00:00Commented Jan 9, 2017 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– NoOneIsHere2017年01月09日 02:58:58 +00:00Commented Jan 9, 2017 at 2:58
-
\$\begingroup\$ No problem, it was my fault \$\endgroup\$sergiol– sergiol2017年01月09日 02:59:31 +00:00Commented Jan 9, 2017 at 2:59
-
\$\begingroup\$ @SeeOneRhino: I achieved it! I outgolfed myself! \$\endgroup\$sergiol– sergiol2017年01月10日 00:07:08 +00:00Commented Jan 10, 2017 at 0:07
PowerShell, 159 bytes
,'Dec Chr'*3-join' | '
'-'*34
32..63|%{($_,($_+32),($_+64)|%{"$_".PadRight(5)+"$(([char]$_,('Space','DEL')[$_-ne32])[$_-in32,127])".padRight(5)})-join' | '}
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 -join
s 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 -join
ed 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.
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
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"
Incase you're wondering, the first version was a base64 encoded string.
-
\$\begingroup\$ @FlipTack Changed it to an actual solution \$\endgroup\$sagiksp– sagiksp2017年01月07日 19:42:43 +00:00Commented Jan 7, 2017 at 19:42
-
\$\begingroup\$ Unrequired whitespace at
ljust(6) for
. \$\endgroup\$Yytsi– Yytsi2017年01月13日 17:28:19 +00:00Commented Jan 13, 2017 at 17:28 -
\$\begingroup\$ Another at
x in [
. And IIRC the square brackets insidejoin
can deleted. \$\endgroup\$Yytsi– Yytsi2017年01月14日 08:29:53 +00:00Commented Jan 14, 2017 at 8:29
05AB1E, (削除) 82 (削除ここまで) 76 bytes
žQSDÇƵQ ̧«.Bsð"‡2":"DEL" ̧«.B)×ばつý})3äøvy... | ©ý}®"†... Chr ×ばつ ̈ ×ばつ.Á.Á»
Still golfing, this can be improved a lot.
žQSDÇƵQ ̧«.Bsð"‡2":"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 ']]
×ばつý})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 ×ばつ ̈ ×ばつ.Á.Á»
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
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.
-
\$\begingroup\$ I don't think you need a space after
void main(){
, and neither betweenwhile(s.length()<5)
ands+=" "
. (Unless you didn't count that in your byte count). And you can save a few bytes by changingd="Dec",c="Chr"
tod=p("Dec"),c=p("Chr")
and making changing your callsp(d)
andp(c)
tod
andc
. \$\endgroup\$Adalynn– Adalynn2017年06月25日 15:15:52 +00:00Commented Jun 25, 2017 at 15:15 -
\$\begingroup\$ The 434 was removing all unnecessary whitespace. Your suggestion brings it down to 425. Thanks! \$\endgroup\$xirt– xirt2017年06月28日 17:03:11 +00:00Commented Jun 28, 2017 at 17:03
-
\$\begingroup\$ Might want to add a note about the 434 being w/o unneeded whitespace into the answer \$\endgroup\$Adalynn– Adalynn2017年06月28日 17:47:44 +00:00Commented Jun 28, 2017 at 17:47
-
\$\begingroup\$ Done. Note: program has significantly changed since (reduced further) so comments above may no longer be relevant \$\endgroup\$xirt– xirt2017年06月29日 19:32:07 +00:00Commented Jun 29, 2017 at 19:32
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))
Haskell (GHC2024), (削除) 199 (削除ここまで) (削除) 193 (削除ここまで) 191 bytes
-5 by realizing intercalate x [y] == y
, -1 by not using fmap
on a list like a silly goose, -2 by getting rid of some unnecessary parentheses
import Text.Printf
(#)=replicate
o=drop 3.(>>=(" | "++))<$>3#"Dec Chr ":[34#'-']:(zipWith(printf"%-5d%-5s")<*>map(\case{32->"Space";127->"DEL";c->[toEnum c]})<$>[[c,c+32..127]|c<-[32..63]])
drop 3.(>>=(" | "++))<$>3#"Dec Chr ":[34#'-']:(zipWith(printf"%-5d%-5s")<*>map(\case{32->"Space";127->"DEL";c->[toEnum c]})<$>[[c,c+32..127]|c<-[32..63]])
drop 3.(>>=(" | "++)) `intercalate " | "` without the Data.List import
<$> map over list:
3#"Dec Chr ": First line in three pieces minus the " | " separators, followed by
[34#'-']: Second line in one piece (unchanged by intercalate), followed by
( )
[[c,c+32..127]|c<-[32..63]] For each row, the three characters in that row as numbers...
zipWith( )<*> <$>[[c,c+32..127]|c<-[32..63]] ... combined with a copy of that row transformed by...
map(\case{32->"Space";127->"DEL";c->[toEnum c]}) ... self-explanatory...
printf"%-5d%-5s" ... using this pretty-printing function to make a table cell
Shorter intercalate " | "
adapted from a tip by @corvus_192.
Here's another 191-byter that takes a different approach to translating numbers to strings: drop 3.(>>=(" | "++))<$>3#"Dec Chr ":[34#'-']:(zipWith(printf"%-5d%-5s".(+32))<*>map(("Space":map pure['!'..'~']++["DEL"])!!)<$>[[c,c+32..95]|c<-[0..31]])
Japt -R
, (削除) 65 (削除ここまで) (削除) 64 (削除ここまで) (削除) 63 (削除ここまで) (削除) 62 (削除ここまで) (削除) 60 (削除ここまで) 59 bytes
Still not happy the insertion of the line of dashes. And the trim
is annoying me now, too! Another approach may be needed.
Hó96
;íE¬h`Spa` p"DEL")òUÎ Ëi"DCehcr"ó)ú5 m¬ÃÕËq|û3)xÃyÈiÉ
Hó96\n;íE¬h`Spa` p"DEL")òUÎ Ëi"DCehcr"ó)ú5 m¬ÃÕËq|û3)xÃyÈiÉ
H :32
ó96 :Range [H,H+96)
\n :Assign to variable U
í :Interleave with
; E : ASCII
¬ : Split to an array
h : Replace the first element with
`Spa` : Compressed string "Space"
p"DEL" : Push "DEL"
) :End interleave
ò :Partitions of length
UÎ : First element of U (the ";" changes the H variable to 65)
Ë :Map
i : Prepend
"DCehcr"ó : Uninterleave "DCehcr"
) : End prepend
ú5 : Right pad all elements with spaces to length 5
m : Map
¬ : Join
à :End map
Õ :Transpose
Ë :Map
q| : Join with "|"
û3 : Centre padded with space to length 3
) : End join
x : Trim
à :End map
y :Transpose
È :Pass each row through the following function & transpose back
iÉ : Insert a "-" at 0-based index 1
:Implicit output joined with newlines
Vyxal 3 -j
, 52 bytes
"∑W"k×ばつp"ġ!-x7k"2Ḋ3YpƛɸḢḢṪ
Beats Stax :)
"∑W"k×ばつp"ġ!-x7k"2Ḋ3YpƛɸḢḢṪ
"∑W" # Compressed string "Space"
kPḢ\ # Ascii characters without space pushed onto the stack individually
"DEL" # literal "DEL"
W5» # Wrap the stack into a list and append spaces to make each length 5
36R # Range 32-127
vɸ5» # stringified and made to length 5 (better to do separately)
z+ # Zip both lists and concatenate
3ẆT # Transpose groups of length 32 to make columns
×ばつp # prepend 37 dashes
"ġ!-x7k" # "Dec Chr"
2Ḋ3Y # append two spaces and repeat 3 times
p # prepend that list
ƛɸḢḢṪ # The weird part explained below
💎
Created with the help of Luminespire.
Vyxal prints its lists in python form, but all lists are actually stored as [ a | b | c ]. The stringify modifier converts the list of lists to a list of strings in this form rather than the pretty printed one, so the map function also removes the brackets and extra leading space. This leaves the nested list delimiters with proper spacing for the challenge. The line of dashes is not a list, so it has to be length 37 to account for the extra three removed by the list to string formatting.
Explore related questions
See similar questions with these tags.