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 theprintable ASCII characters isASCIItable.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 a34 " | 66 B | 98 b35 # | 67 C | 99 c36 $ | 68 D | 100 d37 % | 69 E | 101 e38 & | 70 F | 102 f39 ' | 71 G | 103 g40 ( | 72 H | 104 h41 ) | 73 I | 105 i42 * | 74 J | 106 j43 + | 75 K | 107 k44 , | 76 L | 108 l45 - | 77 M | 109 m46 . | 78 N | 110 n47 / | 79 O | 111 o48 0 | 80 P | 112 p49 1 | 81 Q | 113 q50 2 | 82 R | 114 r51 3 | 83 S | 115 s52 4 | 84 T | 116 t53 5 | 85 U | 117 u54 6 | 86 V | 118 v55 7 | 87 W | 119 w56 8 | 88 X | 120 x57 9 | 89 Y | 121 y58 : | 90 Z | 122 z59 ; | 91 [ | 123 {60 < | 92 \ | 124 |61 = | 93 ] | 125 }62 > | 94 ^ | 126 ~63 ? | 95 _ | 127 DELTrailing spaces on each line, and a trailing newline are permitted. Since this is akolmogorov-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:00CommentedJan 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:00CommentedJan 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:00CommentedJan 7, 2017 at 16:12
- \$\begingroup\$@FlipTack It still has Space and DEL\$\endgroup\$simon– simon2017-01-07 16:20:18 +00:00CommentedJan 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:00CommentedJan 7, 2017 at 16:42
40 Answers40
Python2 3.6,185183175159 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,185183 175 bytes
print("Dec Chr | "*3)[:-5]+"\n"+"-"*34a=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" + "-"*34def 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:00CommentedApr 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| DELV,98, 96, 94 bytes
i32 | 64 | 9631ñÙl.l.ñÍä«/& & ÎéiD@"bsDELF 27kdH5lRSpaceÄÒ-Ä3RDec Chr³ | Î35|DJustbarely 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.3100000010: f1d9 016c 2e6c 2ef1 cde4 ab2f 2620 2020 ...l.l...../& 00000020: 1616 2620 2020 200a cee9 6944 4022 0a62 ..& ...iD@".b00000030: 7344 454c 1b46 2016 3237 6b64 4835 6c52 sDEL.F .27kdH5lR00000040: 5370 6163 651b c4d2 2dc4 3352 4465 6320 Space...-.3RDec 00000050: 2043 6872 b320 7c20 1bce 3335 7c44 Chr. | ..35|DAnd here's how it works:
i32 | 64 | 96<esc> " Insert some starting text31ñ ñ " 31 times: Ù " Duplicate this line <C-a> " Increment the first number on this line l. " Increment the next number l. " Increment the next numberHere 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 above0x7f, 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 toinsert 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 keystrokesAt 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 _ | 127Now 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 manyH5l " 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 lineJavaScript (Node.js), 158 bytes
-11 by switching to Node.js, as suggested byFhuvi
-1 thanks toShaggy
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:00CommentedJan 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:00CommentedJan 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:00CommentedJan 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:00CommentedSep 10, 2024 at 13:48 - 1
Pyth,898579 77 bytes
PP*"Dec Chr | "3*\-34V32PPsm++.[`=+N32;5.[?qN32"Space"?qN127"DEL"CN;6"| "3Perl, 120 bytes
$,="| ";say+("Dec Chr ")x3;say"-"x32;say map{sprintf"%-5s%-6s",$_,$_-32?$_-127?chr:DEL:Space}$_,$_+32,$_+64for 32..63Run 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:00CommentedJan 8, 2017 at 13:46
F#, 222 bytes
let c,p=String.concat" | ",printfn"%s"Seq.replicate 3"Dec Chr "|>c|>pp(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:00CommentedApr 25, 2018 at 13:24
- 1\$\begingroup\$The first line aliases two functions to single-character names. Now if you do
["ab"; "cd"] |> c |> pit 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:00CommentedApr 25, 2018 at 18:41
V,151150148136135130129 125 bytes
12 bytes saved thanks to @nmjcman101 for using<C-v>g<C-a> for the numbers instead ofline('.')
2 byte saved thanks to @DJMcMayhem for removing lines with leading spaces usingÇÓ/d and by usingdê to remove extra spaces and rearranging stuff
This answer is in competition with @nmjcman101's Vanswer (which uses:set ve=all).But now, I found a way to remove thoseA ^[s and saved some bytes and we are at an even bytecount
iSpace ¬!~Ó./&òiDELí^/31 HlgGo| 63ÙkGld/Sp$p/`G$d/@$p/64G$d/S$pÇÓ/d/dhdê/32O34é-O!| !| !Ó!/Dec ChrHexdump:
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.G00000030: 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 ChrExplanation (incomplete and outdated)
The strategy here is that I'm using the line numbers to generate the ASCII code points.
Note:^[ is0x1b,^V isC-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 DELpart of your code:Try it online! It uses that neat thing where you can highlight a bunch of numbers, and theng^Amakes it an increasing sequence (new in Vim 8?)\$\endgroup\$nmjcman101– nmjcman1012017-01-07 21:14:52 +00:00CommentedJan 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 commandTry it online!\$\endgroup\$DJMcMayhem– DJMcMayhem2017-01-07 21:22:56 +00:00CommentedJan 7, 2017 at 21:22
- \$\begingroup\$Either way, once you get that you can do a
:set ve=alland 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:00CommentedJan 7, 2017 at 21:39 - \$\begingroup\$@nmjcman101 Re
g^Athanks, it saved me 12 bytes :)\$\endgroup\$user41805– user418052017-01-08 08:06:50 +00:00CommentedJan 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:00CommentedJan 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]dsbxHow 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 spacesx # 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,130120 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 someA <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=all12|êr|2ñ031j$x)PñHd)ÄÒ-Ä3RDec Chr³ | /dhdêÎ35|DHexdump 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.100000030: 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..300000060: 357c 44 5|D- 1\$\begingroup\$The global command could be shortened by quite a but. For one, the
ddis implicit if you just dod. You can also do<M-s>which is always equivalent to\sin 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 linenot matching a non-whitespace character.ÇÓ/dwhich is equivalent to:g!/\S/normal dd\$\endgroup\$DJMcMayhem– DJMcMayhem2017-01-11 22:59:18 +00:00CommentedJan 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:00CommentedApr 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 countTry it online!\$\endgroup\$Jörg Hülsermann– Jörg Hülsermann2017-06-25 15:21:12 +00:00CommentedJun 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 printfmain(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
Pbeprintf(, as shown here:repl.it/JBRD\$\endgroup\$Adalynn– Adalynn2017-06-25 15:18:39 +00:00CommentedJun 25, 2017 at 15:18 - \$\begingroup\$And by removing the space on your fourth line.\$\endgroup\$Adalynn– Adalynn2017-06-25 15:20:54 +00:00CommentedJun 25, 2017 at 15:20
- \$\begingroup\$229 bytes\$\endgroup\$ceilingcat– ceilingcat2020-03-09 00:41:17 +00:00CommentedMar 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)yI tried really hard to get under 200 bytes but thesepaste andcollapse are killing me.Returns a list of lines.
Java (JDK), 192194200 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 usesString.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%3to save five bytes\$\endgroup\$Khuldraeseth na'Barya– Khuldraeseth na'Barya2024-09-05 16:31:49 +00:00CommentedSep 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:00CommentedSep 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 -34Tcl, 215 bytes
Requires Tcl >= 8.7 to allow use oflseq
puts [set h "Dec Chr"][set S " | "]$h$S$h\n[string repe - 34]proc p x {format %-5d%c $x $x}lmap i [lseq 32 63] {puts [regsub \ {8} [regsub \177 "[p $i] $S[p [expr $i+32]] $S[p [expr $i+64]]" DEL] " Space"]}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
32for example (a ' ' instead ofSpace), and33!(no spacein-between33and!).\$\endgroup\$NoOneIsHere– NoOneIsHere2017-01-09 02:23:44 +00:00CommentedJan 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:00CommentedJan 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:00CommentedJan 9, 2017 at 2:58
- \$\begingroup\$No problem, it was my fault\$\endgroup\$sergiol– sergiol2017-01-09 02:59:31 +00:00CommentedJan 9, 2017 at 2:59
- \$\begingroup\$@SeeOneRhino: I achieved it! I outgolfed myself!\$\endgroup\$sergiol– sergiol2017-01-10 00:07:08 +00:00CommentedJan 10, 2017 at 0:07
PowerShell, 159 bytes
,'Dec Chr'*3-join' | ''-'*3432..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-joins that array together to create the headers. The second is just a straight string multiplication.
The third line loops over32..63 and each iteration sends three values$_,($_+32), and($_+64) into an inner loop. The inner loop does aPadRight on the value (adds the appropriate spaces to pad to5 characters). That is then string concatenated+ with the result of a nested pseudo-ternary( )[ ]. The pseudo-ternary selects either thechar representation of that number, or elseSpace orDEL if it's the appropriate value. Again, wePadRight 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 implicitWrite-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/;printPython 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:00CommentedJan 7, 2017 at 19:42
- \$\begingroup\$Unrequired whitespace at
ljust(6) for.\$\endgroup\$Yytsi– Yytsi2017-01-13 17:28:19 +00:00CommentedJan 13, 2017 at 17:28 - \$\begingroup\$Another at
x in [. And IIRC the square brackets insidejoincan deleted.\$\endgroup\$Yytsi– Yytsi2017-01-14 08:29:53 +00:00CommentedJan 14, 2017 at 8:29
05AB1E,82 76 bytes
žQSDÇƵQ¸«.Bs𔇲”:"DEL"¸«.B)øvyð2×ý})3äøvy… | ©ý}®”†… Chr ÿ”3ר¨'-34×.Á.Á»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 DELJava,434422 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)todandc.\$\endgroup\$Adalynn– Adalynn2017-06-25 15:15:52 +00:00CommentedJun 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:00CommentedJun 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:00CommentedJun 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:00CommentedJun 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),199193 191 bytes
-5 by realizingintercalate x [y] == y, -1 by not usingfmap on a list like a silly goose, -2 by getting rid of some unnecessary parentheses
import Text.Printf(#)=replicateo=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 cellShorterintercalate " | " adapted from atip 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,6564636260 59bytes
Still not happy the insertion of the line of dashes. And thetrim 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 newlinesVyxal 3-j, 52 bytes
"∑ᵂ”kPḢ\"DEL"W5»₃₆Rᵛᶲ5»ᶻ+₃ẆT'-37×p"ġ!-x7ᵏ”2Ḋ3YpƛᶲḢḢṪBeats Stax :)
"∑ᵂ”kPḢ\"DEL"W5»₃₆Rᵛᶲ5»ᶻ+₃ẆT'-37×p"ġ!-x7ᵏ”2Ḋ3YpƛᶲḢḢṪ"∑ᵂ” # 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 ₃₆R # Range 32-127 ᵛᶲ5» # stringified and made to length 5 (better to do separately) ᶻ+ # Zip both lists and concatenate ₃ẆT # Transpose groups of length 32 to make columns '-37×p # prepend 37 dashes "ġ!-x7ᵏ” # "Dec Chr" 2Ḋ3Y # append two spaces and repeat 3 times p # prepend that list ƛᶲḢḢṪ # The weird part explained below💎Created with the help ofLuminespire.
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.
















