20
\$\begingroup\$

Introduction

A code page maps integer values to a specific character. We can visualize a code page like this:

+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+|   | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+| 0 | q | w | e | r | t | z | u | i | o | p | a | s | d | f | g | j |+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+| 1 | k | l | y | x | c | v | b | n | m | Q | W | E | R | T | Z | U |+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+| 2 | I | O | P | A | S | D | F | G | H | J |   |   |   |   |   |   |+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

The rows specify the first digit and the columns the second digit of the hex-number.

The Challenge

Given a string of 16-255 unique characters, output the corresponding code page using the zero-based index of each character in the string as it's value.The inputqwertzuiopasdfgjklyxcvbnmQWERTZUIOPASDFGHJ for example would yield the table above.

  • The output has to be in the exact same format as the table above. Only a trailing newline is optional.
  • If the input length is not a multiple of 16, you need to start a new row for the remaining characters and leave the unused cells empty (=filled with 3 spaces).
  • Each character is placed exactly in the middle of a cell, padded by one space to the left and right.
  • The values in the first row and column are given by hex-numbers with the digits0123456789ABCDEF. Those are padded with one space to the left and right as well. You may choose to use lowercase hex-digits but you need to stick to one.
  • The only characters present in the output are hyphens-, pluses+, pipes|, spaces, the digits for hexadecimal numbers and the characters from the input.
  • Any builtins that are related to ASCII-art tables or trivialize the problem in any way are forbidden.
  • You may assume that the input consists only of characters of a specific encoding. Please specify if that is the case.
  • If your language canonly handle ASCII input, you may assume ASCII-only input and repeated characters.

Rules

Happy Coding!

askedApr 1, 2016 at 23:57
Denker's user avatar
\$\endgroup\$
12
  • \$\begingroup\$Can we use lowercase hex digits?\$\endgroup\$CommentedApr 1, 2016 at 23:59
  • \$\begingroup\$@Doorknob Yes, clarified it in the challenge.\$\endgroup\$CommentedApr 2, 2016 at 0:00
  • 1
    \$\begingroup\$Can we assume the input is ASCII (with possibly repeated characters)?\$\endgroup\$CommentedApr 2, 2016 at 0:05
  • \$\begingroup\$@DenkerAffe That would conflict with the word "unique" or with "255" inGiven a string of 16-255 unique characters, though...\$\endgroup\$CommentedApr 2, 2016 at 0:07
  • 1
    \$\begingroup\$@LuisMendo Hmm yea, thats true. Gonna allow that for languages that can only handle ASCII.\$\endgroup\$CommentedApr 2, 2016 at 0:14

10 Answers10

7
\$\begingroup\$

Pyth, 60 bytes

K+*"+---"hJ16\+Vm++"| "j" | "d" |"+]+]d.HMJ.e+.Hk.[bdJczJNK

The leading newline is significant.

Try it here.

answeredApr 2, 2016 at 0:18
Doorknob's user avatar
\$\endgroup\$
6
  • \$\begingroup\$Can you show the transpiled code?\$\endgroup\$CommentedApr 2, 2016 at 0:27
  • \$\begingroup\$@CatsAreFluffy Just enable the debug mode in the online interpreter.\$\endgroup\$CommentedApr 2, 2016 at 0:42
  • \$\begingroup\$What does the newline do?\$\endgroup\$CommentedApr 2, 2016 at 10:56
  • \$\begingroup\$@Adnan Whoops, that was an oversight on my part. Fixed, thanks.\$\endgroup\$CommentedApr 3, 2016 at 14:48
  • \$\begingroup\$@KennyLau It prints the first+---+---+---[...]. In Pyth, the newline function prints and returns its argument.\$\endgroup\$CommentedApr 3, 2016 at 14:48
7
\$\begingroup\$

Python 3.5,326 355 bytes:

(+29 bytes since if the length of the last row isnot a multiple of 16, unused cells should be left empty (although, in my opinion, it looks much better if those empty cells are just not even shown))

def f(r):o=' 0123456789ABCDEF';r=[r[0+i:16+i]for i in range(0,len(r),16)];print('+---'*17+'+\n|',end='');[print(' {} |'.format(h),end='')for h in o];print(''.join([str(e+' | ')if e.isdigit()or e.isalpha()else str(e)for e in''.join([str('\n'+'+---'*17+'+\n| '+x[0]+x[1])for x in zip(o[1::1],r)])]),end='');print('  |'+'   |'*(15-len(r[-1]))+'\n'+'+---'*17+'+')

Works like a charm!

Sample Inputs and Outputs:

Input: 'hopper'Output:        +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+    |   | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |    +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+    | 0 | h | o | p | p | e | r |   |   |   |   |   |   |   |   |   |   |    +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+Input: 'honkhonkhonkhonkhonk'Output:    +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+    |   | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |    +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+    | 0 | h | o | n | k | h | o | n | k | h | o | n | k | h | o | n | k |     +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+    | 1 | h | o | n | k |   |   |   |   |   |   |   |   |   |   |   |   |    +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+    Input: 'hi'Output:     +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+    |   | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |    +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+    | 0 | h | i |   |   |   |   |   |   |   |   |   |   |   |   |   |   |    +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+

I hope this is okay.

Also, here is another version I created for this challenge, which, although is an invalid candidate since it doesnot print out extra empty cells for the last row if its length is not a multiple 16, in my opinion outputs a much better looking page than the one required by OP, mainly because it does not even show empty cells if the last row is not a multiple of 16, but instead just shows filled cells, and that's it:

def f2(r):o=' 0123456789ABCDEF';r=[r[0+i:16+i]for i in range(0,len(r),16)];print('+---'*17+'+\n|',end='');[print(' {} |'.format(h),end='')for h in o];print(''.join([str(e+' | ')if e.isdigit()or e.isalpha()else str(e)for e in''.join([str('\n'+'+---'*17+'+\n| '+x[0]+x[1])for x in zip(o[1::1],r)])]));print('+---'*(len(r[-1])+1)+'+')

Here is a sample input and output for the inapplicable code above:

Input: 'ggreuuobgugoubgoubguorgoruguor'Output:    +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+    |   | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |    +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+    | 0 | g | g | r | e | u | u | o | b | g | u | g | o | u | b | g | o |     +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+    | 1 | u | b | g | u | o | r | g | o | r | u | g | u | o | r |     +---+---+---+---+---+---+---+---+---+---+---+---+---+---+---+    (As you can see, there are no empty cells shown in the entire table. This looks much better to me.)
answeredApr 3, 2016 at 6:47
R. Kap's user avatar
\$\endgroup\$
4
  • \$\begingroup\$"If the input length is not a multiple of 16, you need to start a new row for the remaining characters and leave the unused cells empty (=filled with 3 spaces)."\$\endgroup\$CommentedApr 3, 2016 at 7:24
  • \$\begingroup\$@KennyLau Ah, yes. I did not see that. Dang...edit now in progress. Honestly though, this looks much better than the one OP shows, don't you think?\$\endgroup\$CommentedApr 3, 2016 at 7:25
  • \$\begingroup\$Why the down vote?\$\endgroup\$CommentedApr 5, 2016 at 19:06
  • \$\begingroup\$@R.Kap I couldn't quite tell you, but here, have an upvote\$\endgroup\$CommentedApr 16, 2016 at 15:22
4
\$\begingroup\$

Excel VBA,157 146 Bytes (Cheating?)

Anonymous VBE Immediate Window function that destructively takes input from range[A1] and outputs to theActiveSheet object.

Golfed

[B1:Q1]="=Dec2Hex(Column(B1)-2)":L=[Len(A1)]:For i=0To l:Cells(i\16+2,i Mod 16+2)=Mid([A1],i+1,1):Next:For i=1To l\16+1:Cells(i+1,1)=i:Next:[A1]="

Formatted

[B1:Q1]="=Dec2Hex(Column(B1)-2)"L=[Len(A1)]For i=0To l:Cells(Int(i/16)+2,i Mod 16+2)=Mid([A1],i+1,1):NextFor i=1To Int(l/16)+1:Cells(i+1,1)=i:Next[A1]=""

Input / Output

Given:

[A1]="qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJ"

the generated output is

Table thingy

answeredJul 21, 2017 at 16:39
Taylor Raine's user avatar
\$\endgroup\$
1
  • \$\begingroup\$probs not valid, but cool anyway. To make it more similar (but still invalid?) you should turn on the appropriate cell boarders.\$\endgroup\$CommentedJan 21, 2018 at 14:43
4
\$\begingroup\$

Excel (Version 1911), 247

A1 =DEC2HEX(COLUMN(A1:P1)-1)A2 'Input StringA3 =2+LEN(A2)/16A4 =CONCAT(" ",C2#)A5 =TEXTJOIN(""&REPT("+---",17)&"+",0,,"| "&MID(A4,B2#+1,1)&" | "&MID(TEXTJOIN(" | ",0,A1#,MID(A2&REPT(" ",16),HEX2DEC(C2#&A1#)+1,1)),B2#*64+1,61)&" |",) ' OutputB2 =SEQUENCE(A3)-1C2 =DEC2HEX(SEQUENCE(A3-1)-1)

Sample Test

Sample test

Excel (Joke answer), 88 Bytes

Not a valid solution, but fun

B2 'InputC4 =DEC2HEX(SEQUENCE(,16,0))B5 =DEC2HEX(SEQUENCE(1+LEN(B2)/16)-1)C5 =MID(B2,HEX2DEC(B5#&C4#)+1,1)

Output

enter image description here

answeredFeb 14, 2020 at 21:59
emegolf123's user avatar
\$\endgroup\$
3
\$\begingroup\$

05AB1E,65 63 bytes

Code:

"+---"17×'+J©,žhAu6£¹J16÷)v„| ?N>iðëN<16B}y«ð17×ðñvy„ |ðJ?}¶?®,

Try it online!. UsesCP-1252 encoding.

answeredApr 2, 2016 at 11:26
Adnan's user avatar
\$\endgroup\$
3
\$\begingroup\$

JavaScript (ES6), 148 bytes

s=>(s=' 0123456789ABCDEF'+s+' '.repeat(15)).match(/(?!^).{16}/g).map((t,i)=>d+`+| `+[s[i],...t].join` | `,d=`+---`.repeat(17)).join` |`+` |${d}+`

The' 0123456789ABCDEF' exists to populate the first column, but conveniently also covers the first row. The input string is then padded with spaces to allow it to be split into substrings of length 16, with the(?!^) preventing the leading space from being matched. The rest is just joining up the pieces.

answeredApr 2, 2016 at 10:57
Neil's user avatar
\$\endgroup\$
2
\$\begingroup\$

PowerShell,127126 121 bytes

($d='+---'*17+'+')($n="0123456789ABCDEF$args"+' '*16)|sls '.{16}'-a|% m*|% v*|%{"$($n[$i++-1]+$_|% t*y|%{"| $_"}) |"$d}

Try it online!

answeredFeb 14, 2020 at 14:53
mazzy's user avatar
\$\endgroup\$
2
\$\begingroup\$

Perl 5-pF,134109 106 bytes

@F=(@a=($",0..9,A..F),@F);shift@F;printf+($_="+---"x@a.'+')."|%2s "x@a."|",$a[$i++],splice@F,0,16while@F

Try it online!

answeredFeb 14, 2020 at 20:41
Xcali's user avatar
\$\endgroup\$
1
\$\begingroup\$

Perl 5-pl,127 120 bytes

@.=($",0..9,A..F);$\='+---'x@..'+';$_=join'',@.,s;.{1,16};$.[$.++].sprintf'%-16s',$&;gre;s;.;| $& ;g;s;.{1,68};$\$&|;g

Try it online!

Previous version:

@.=($",0..9,A..F);$~='+'.'---+'x@.;$_=join'',@.,s;.{1,16};$.[$.++].sprintf'%-16s',$&;gre;s;.;| $& ;g;s;.{1,68};$~$&|;g;$_.=$~

Try it online!

answeredNov 30, 2020 at 11:42
Denis Ibaev's user avatar
\$\endgroup\$
1
\$\begingroup\$

05AB1E,47 45 bytes

Thanks toKevin Cruijssen for -2 bytes!

16ôí16jí15ÝhJš¬ðìsøv"+---"69∍=yS3j€ÀõšĆ'|ý,},

Try it online!

Commented:

16ô                     # split the input into groups of (up to) 16   í16jí                # right-pad the groups to 16 with reverse, left-pad, reverse        15Ý             # push [0, 1, ..., 15]           hJ           # convert each number to hexadecimal and join into string                        #  => "0123456789ABCDEF"               š        # prepend this string to the list                ¬       # get the string back from the list                 ðìs    # prepend a space                    ø   # zip the list and the stringv              }        # iterate over the list "+---"                 # push the string "+---"       69∍              # extend it to length 69          =             # print it without removing the string from the stack y                      # push the current entry  S                     # split into a list of characters   3j                   # left-pad each to 3 chars with spaces     €À                 # rotate each string left       õšĆ              # prepend and append the empty string          '|ý           # join by "|"             ,          # print the string,                       # after the loop: print "+---"69∍, which is still on the stack

Try the first part with step-by-step output
Try one loop-iteration with step-by-step output

answeredNov 30, 2020 at 14:22
ovs's user avatar
\$\endgroup\$
3
  • \$\begingroup\$DŠšs can beš¬ for -2\$\endgroup\$CommentedNov 30, 2020 at 15:01
  • \$\begingroup\$PS: It doesn't save bytes here, but just in case you're unaware of it, there is a builtin "surround with". The3j€Àõš could alternatively have beenðδ.øõ.ø.\$\endgroup\$CommentedNov 30, 2020 at 15:20
  • \$\begingroup\$@KevinCruijssen That does look a little cleaner, I did not know about this builtin. I hadõšĆ… | ý¦¨ as a same-length alternative and I guessðì€ĆõšĆ works too.\$\endgroup\$CommentedNov 30, 2020 at 15:33

Your Answer

More generally…

  • …Please make sure to answer the question and provide sufficient detail.

  • …Avoid asking for help, clarification or responding to other answers (use comments instead).

Draft saved
Draft discarded

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.