53
\$\begingroup\$

Identicons are visual depictions of hash values, often made from symmetrical arrangements of geometric shapes. Your default Stack Exchange avatar is an identicon. This challenge is about creating "wordenticons", simple text-based versions of identicons that apply to strings of lowercase letters, i.e. words.

Challenge

Write a program or function that takes in a string S and outputs its wordenticon. S is guaranteed to be nonempty and only contain lowercase English letter characters a-z. You may optionally assume S has a trailing newline.

The wordenticon of S will be a square grid of text with side lengths 2*length(S) composed of spaces ( ), vertical bars, (|), and horizontal bars ().

To generate the wordenticon of S, form a square grid where every column corresponds to a letter of S (in normal left-to-right reading order) and every row corresponds to a letter of S (in normal top-to-bottom reading order).

For example, if S is food our initial grid looks like

 food
f....
o....
o....
d....

where . is just a placeholder.

For every empty point (every .) in the grid:

  1. If the column letter comes before the row letter alphabetically, replace the . with |.
  2. If the column letter comes after the row letter alphabetically, replace the . with .
  3. If the column and row letters are the same, replace the . with (space).

Here is the food example after each of these steps:

  1. Adding |'s:

     food
    f...|
    o|..|
    o|..|
    d....
    
  2. Adding 's:

     food
    f.――|
    o|..|
    o|..|
    d―――.
    
  3. Adding 's:

     food
    f ――|
    o|  |
    o|  |
    d――― 
    

To complete the wordenticon, remove the superfluous row and column containing the words

 ――|
|  |
|  |
――― 

then mirror the entire thing horizontally

 ――||―― 
|  ||  |
|  ||  |
―――  ―――

and finally mirror it again vertically

 ――||―― 
|  ||  |
|  ||  |
―――  ―――
―――  ―――
|  ||  |
|  ||  |
 ――||―― 

resulting in the 2*length(S) side length text grid that is the final wordenticon.

Examples

Here are some additional wordenticon examples. Note that different words can have identical wordenticons and some wordenticons can be completely made of spaces (unfortunately markdown does not want to render those).

food

 ――||―― 
|  ||  |
|  ||  |
―――  ―――
―――  ―――
|  ||  |
|  ||  |
 ――||―― 

mood

 ――||―― 
|  ||  |
|  ||  |
―――  ―――
―――  ―――
|  ||  |
|  ||  |
 ――||―― 

foof

 ――  ―― 
|  ||  |
|  ||  |
 ――  ―― 
 ――  ―― 
|  ||  |
|  ||  |
 ――  ―― 

fool

 ―――――― 
|  ||  |
|  ||  |
|――  ――|
|――  ――|
|  ||  |
|  ||  |
 ―――――― 

a [2*2 grid of spaces]




to

 || 
―  ―
―  ―
 || 

it

 ―― 
|  |
|  |
 ―― 

tt [4*4 grid of spaces]






abc

 ―――― 
| ―― |
||  ||
||  ||
| ―― |
 ―――― 

and

 ―――― 
| || |
|―  ―|
|―  ―|
| || |
 ―――― 

but

 ―――― 
| || |
|―  ―|
|―  ―|
| || |
 ―――― 

you

 |||| 
― ―― ―
―|  |―
―|  |―
― ―― ―
 |||| 

bob

 ―  ― 
| || |
 ―  ― 
 ―  ― 
| || |
 ―  ― 

cat

 |――| 
― ―― ―
||  ||
||  ||
― ―― ―
 |――| 

cart

 |――――| 
― ―――― ―
|| ―― ||
|||  |||
|||  |||
|| ―― ||
― ―――― ―
 |――――| 

todo

 |||||| 
― |  | ―
―― ―― ――
― |  | ―
― |  | ―
―― ―― ――
― |  | ―
 |||||| 

mice

 |||||| 
― |||| ―
―― ―― ――
――|  |――
――|  |――
―― ―― ――
― |||| ―
 |||||| 

zyxw

 |||||| 
― |||| ―
―― || ――
―――  ―――
―――  ―――
―― || ――
― |||| ―
 |||||| 

banana

 |―|―||―|―| 
― ― ―  ― ― ―
|| | || | ||
― ― ―  ― ― ―
|| | || | ||
― ― ―  ― ― ―
― ― ―  ― ― ―
|| | || | ||
― ― ―  ― ― ―
|| | || | ||
― ― ―  ― ― ―
 |―|―||―|―| 

codegolf

 ―――――――――――――― 
| ||| |||| ||| |
|― ―――――――――― ―|
|―| ―――――――― |―|
|―|| ――||―― ||―|
| ||| |||| ||| |
|―|||― || ―|||―|
|―||―――  ―――||―|
|―||―――  ―――||―|
|―|||― || ―|||―|
| ||| |||| ||| |
|―|| ――||―― ||―|
|―| ―――――――― |―|
|― ―――――――――― ―|
| ||| |||| ||| |
 ―――――――――――――― 

programming

 ―||―||||||||||||―||― 
| || |||||||||||| || |
―― |―||||||||||||―| ――
――― ―|――――  ――――|― ―――
| || |||||||||||| || |
――――― ―――――――――― ―――――
―――|―|  |―||―|  |―|―――
―――|―|  |―||―|  |―|―――
―――|―|―― ―||― ――|―|―――
―――|―|||| || ||||―|―――
――― ―|――――  ――――|― ―――
――― ―|――――  ――――|― ―――
―――|―|||| || ||||―|―――
―――|―|―― ―||― ――|―|―――
―――|―|  |―||―|  |―|―――
―――|―|  |―||―|  |―|―――
――――― ―――――――――― ―――――
| || |||||||||||| || |
――― ―|――――  ――――|― ―――
―― |―||||||||||||―| ――
| || |||||||||||| || |
 ―||―||||||||||||―||― 

abcdefghijklm

 ―――――――――――――――――――――――― 
| ―――――――――――――――――――――― |
|| ―――――――――――――――――――― ||
||| ―――――――――――――――――― |||
|||| ―――――――――――――――― ||||
||||| ―――――――――――――― |||||
|||||| ―――――――――――― ||||||
||||||| ―――――――――― |||||||
|||||||| ―――――――― ||||||||
||||||||| ―――――― |||||||||
|||||||||| ―――― ||||||||||
||||||||||| ―― |||||||||||
||||||||||||  ||||||||||||
||||||||||||  ||||||||||||
||||||||||| ―― |||||||||||
|||||||||| ―――― ||||||||||
||||||||| ―――――― |||||||||
|||||||| ―――――――― ||||||||
||||||| ―――――――――― |||||||
|||||| ―――――――――――― ||||||
||||| ―――――――――――――― |||||
|||| ―――――――――――――――― ||||
||| ―――――――――――――――――― |||
|| ―――――――――――――――――――― ||
| ―――――――――――――――――――――― |
 ―――――――――――――――――――――――― 

Scoring

This is , the shortest code in bytes wins. Tiebreaker goes to the earlier answer.

Notes

  • Any instance of horizontal bar () in your code may be counted as 1 byte instead of the 3 UTF-8 bytes it actually takes up. (Up to ten instances.)
  • If desired you may use regular dashes (-) in place of horizontal bars ().
  • Removing or adding trailing spaces in the lines of a wordenticon is not allowed (even if the shape remains unchanged). It should be an exact 2*length(S) side length text square.
  • The output wordenticon may optionally have a single trailing newline.
\$\endgroup\$
5
  • \$\begingroup\$ Can we take the input as an array of characters? \$\endgroup\$
    – Downgoat
    May 26, 2016 at 22:38
  • \$\begingroup\$ @Downgoat No, it should be a normal string unless there's absolutely no other way for your language. \$\endgroup\$ May 26, 2016 at 22:39
  • 2
    \$\begingroup\$ You've got examples for programming, and and codegolf but you forgot puzzles... \$\endgroup\$
    – Neil
    May 26, 2016 at 23:21
  • \$\begingroup\$ Could you add a leaderboard? \$\endgroup\$
    – Leaky Nun
    May 27, 2016 at 11:11
  • \$\begingroup\$ So we did eventually find out why you were asking about portmanteaus in chat :) \$\endgroup\$
    – gcampbell
    May 27, 2016 at 13:01

34 Answers 34

21
\$\begingroup\$

MATL, 20 15 bytes

'-| 'jtPht!-ZS)

Try it at MATL Online

Explanation

'-| '       % String literal defining the replacement characters
j           % Explicitly grab the input as a string
tP          % Duplicate and reverse the input string (row vector of chars)
h           % Horizontally concatenate the input and it's inverse
t!          % Duplicate and turn into a column vector
-           % Subtract the two vectors (converts to ASCII codes) and we automatically
            % broadcast to create a (2N x 2N) matrix where if the column is
            % later in the alphabet (higher ASCII) we get a positive number, if the 
            % column was earlier (lower ASCII) we get a negative number, and if they are
            % the same letter (same ASCII) we get a 0.
ZS          % sign function which yields -1 for negative, 1 for positive, and 0 for 0;
)           % Use this to index (modulus) into the string literal '-| '. MATL uses 1-based
            % indexing so 0 yields ' ', -1 replaced by '|', and 1 replaced by '-'
            % Implicitly display the result
\$\endgroup\$
3
  • 10
    \$\begingroup\$ :O you outgolfed Dennis! \$\endgroup\$
    – Downgoat
    May 26, 2016 at 23:46
  • \$\begingroup\$ @Downgoat This is one of those rare occasions that MATL is shorter than Jelly! \$\endgroup\$
    – Suever
    May 26, 2016 at 23:58
  • \$\begingroup\$ Good idea to use the sign function! \$\endgroup\$
    – Luis Mendo
    May 26, 2016 at 23:58
18
\$\begingroup\$

Java, 329 305 264 259 192 bytes

Thanks to:

  • @Bálint for suggesting to use ternary operators.
  • @user902383 for suggesting to reverse the string myself
  • @Frozn and @user902383 for suggesting to replace StringBuilder with String.

Golfed:

String g(String w){char[]a=w.toCharArray();String s="";for(int i=a.length-1;i>=0;s=s+a[i--]);w+=s;a=w.toCharArray();s="";for(char x:a){for(char y:a)s+=(x>y?'|':x<y?'-':' ');s+='\n';}return s;}

Ungolfed:

String g(String w) {
    char[] a = w.toCharArray();
    String s = "";
    for (int i = a.length - 1; i >= 0; s = s + a[i--]);
    w += s;
    a = w.toCharArray();
    s = "";// To keep the output pure (ie. without the input string as well)
    for (char x : a) {
        for (char y : a)
            s += (x > y ? '|' : x < y ? '-' : ' ');
        s += '\n';
    }
    return s;
}

Definitely a fun one. First attempt was a function that was O(n) but ended up getting replaced with this simpler form after I got too frustrated.

And, to test:

supercalifragilisticexpialidocious
 -|||||||||||||| -|||-||||||||||-  -||||||||||-|||- ||||||||||||||- 
| |||||||||||||||||||-|||||||||| || ||||||||||-||||||||||||||||||| |
-- |-|||||-|||||--|||- |||||||||----||||||||| -|||--|||||-|||||-| --
--- -||----|-------| ---|--|-|--------|-|--|--- |-------|----||- ---
--|| ||||| |||||--|||-||||||||||----||||||||||-|||--||||| ||||| ||--
----- |----|------- ----|---- -------- ----|---- -------|----| -----
------ ---- ------------ ------------------ ------------ ---- ------
---|-|| ||-||| |--|||--|| ||-||------||-|| ||--|||--| |||-|| ||-|---
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
---|-||-- -|-------||---|--|-|--------|-|--|---||-------|- --||-|---
--|| ||||| |||||--|||-||||||||||----||||||||||-|||--||||| ||||| ||--
------ ---- ------------ ------------------ ------------ ---- ------
---|-||--|-| ------||---|--|-|--------|-|--|---||------ |-|--||-|---
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
---|-|| ||-||| |--|||--|| ||-||------||-|| ||--|||--| |||-|| ||-|---
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
 -|||||||||||||| -|||-||||||||||-  -||||||||||-|||- ||||||||||||||- 
|-||||||||||||||| |||-||||||||||-||-||||||||||-||| |||||||||||||||-|
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
----- |----|------- ----|---- -------- ----|---- -------|----| -----
--- -||----|-------| ---|--|-|--------|-|--|--- |-------|----||- ---
||||||||||||||||||||| |||||||||||||||||||||||| |||||||||||||||||||||
-- |-|||||-|||||--|||- |||||||||----||||||||| -|||--|||||-|||||-| --
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
------ ---- ------------ ------------------ ------------ ---- ------
---|-|| ||-||| |--|||--|| ||-||------||-|| ||--|||--| |||-|| ||-|---
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
-----||----|-------|----|-- -|--------|- --|----|-------|----||-----
---|-|||||-|||||--|||--||||| || ---- || |||||--|||--|||||-|||||-|---
----- |----|------- ----|---- -------- ----|---- -------|----| -----
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
---|-|||||-|||||--|||--||||| || ---- || |||||--|||--|||||-|||||-|---
| |||||||||||||||||||-|||||||||| || ||||||||||-||||||||||||||||||| |
 -|||||||||||||| -|||-||||||||||-  -||||||||||-|||- ||||||||||||||- 
 -|||||||||||||| -|||-||||||||||-  -||||||||||-|||- ||||||||||||||- 
| |||||||||||||||||||-|||||||||| || ||||||||||-||||||||||||||||||| |
---|-|||||-|||||--|||--||||| || ---- || |||||--|||--|||||-|||||-|---
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
----- |----|------- ----|---- -------- ----|---- -------|----| -----
---|-|||||-|||||--|||--||||| || ---- || |||||--|||--|||||-|||||-|---
-----||----|-------|----|-- -|--------|- --|----|-------|----||-----
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
---|-|| ||-||| |--|||--|| ||-||------||-|| ||--|||--| |||-|| ||-|---
------ ---- ------------ ------------------ ------------ ---- ------
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
-- |-|||||-|||||--|||- |||||||||----||||||||| -|||--|||||-|||||-| --
||||||||||||||||||||| |||||||||||||||||||||||| |||||||||||||||||||||
--- -||----|-------| ---|--|-|--------|-|--|--- |-------|----||- ---
----- |----|------- ----|---- -------- ----|---- -------|----| -----
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
|-||||||||||||||| |||-||||||||||-||-||||||||||-||| |||||||||||||||-|
 -|||||||||||||| -|||-||||||||||-  -||||||||||-|||- ||||||||||||||- 
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
---|-|| ||-||| |--|||--|| ||-||------||-|| ||--|||--| |||-|| ||-|---
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
---|-||--|-| ------||---|--|-|--------|-|--|---||------ |-|--||-|---
------ ---- ------------ ------------------ ------------ ---- ------
--|| ||||| |||||--|||-||||||||||----||||||||||-|||--||||| ||||| ||--
---|-||-- -|-------||---|--|-|--------|-|--|---||-------|- --||-|---
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
---|-|| ||-||| |--|||--|| ||-||------||-|| ||--|||--| |||-|| ||-|---
------ ---- ------------ ------------------ ------------ ---- ------
----- |----|------- ----|---- -------- ----|---- -------|----| -----
--|| ||||| |||||--|||-||||||||||----||||||||||-|||--||||| ||||| ||--
--- -||----|-------| ---|--|-|--------|-|--|--- |-------|----||- ---
-- |-|||||-|||||--|||- |||||||||----||||||||| -|||--|||||-|||||-| --
| |||||||||||||||||||-|||||||||| || ||||||||||-||||||||||||||||||| |
 -|||||||||||||| -|||-||||||||||-  -||||||||||-|||- ||||||||||||||-  
\$\endgroup\$
8
  • 2
    \$\begingroup\$ Welcome to Programming Puzzles & Code Golf! This is a very nice first answer. :) \$\endgroup\$
    – Alex A.
    May 26, 2016 at 23:30
  • 1
    \$\begingroup\$ Note that you don't have to use \u2015. Using horizontal bar (or simply dash) in the raw code is fine. \$\endgroup\$ May 26, 2016 at 23:32
  • \$\begingroup\$ I'm pretty sure that if you used normal String instead of StringBuilder the answer could be much shorter... (even though it may take up a lot more memory) \$\endgroup\$
    – Leaky Nun
    May 27, 2016 at 12:22
  • \$\begingroup\$ You could win some bytes by declaring all the ints on the same line : int i,j,l=m.length();for(i=0;i<l;i++){... \$\endgroup\$
    – Aaron
    May 27, 2016 at 12:31
  • \$\begingroup\$ I think you could save a lot if you change the for loops to for each loops. You don't need the indices for anything else than getting the char. \$\endgroup\$
    – Frozn
    May 28, 2016 at 15:11
11
\$\begingroup\$

Haskell, 93 bytes

r=reverse
h x=unlines$(++)<*>r$zipWith(++)<*>map r$(<$>x).((("- |"!!).fromEnum).).compare<$>x

Usage example:

*Main> putStr $ h "food"
 --||-- 
|  ||  |
|  ||  |
---  ---
---  ---
|  ||  |
|  ||  |
 --||-- 

How it works (note: (f <*> g) x is defined as f x (g x)):

((("- |"!!).fromEnum).).compare       -- a function that finds the replacement char
                                      -- for two given chars
   (<$>x).(    )<$>x                  -- map this function for every char in the
                                      -- input over each char. Now we have the
                                      -- first quadrant as a list of strings
zipWith(++) <*> map r                 -- append to each line a reversed copy of itself
(++) <*> r                            -- append a reversed copy of the whole list
unlines                               -- turn into a single string

Alternative version: the "find replacement" function ((("- |"!!).fromEnum).).compare can also be written as a#b|a<b='-'|a>b='|'|1<2=' ' and called via (#) for the same byte count.

\$\endgroup\$
8
\$\begingroup\$

Jelly, 16 bytes

Om©0_'®Ṡị“-| ”j⁷

Try it online!

How it works

Om©0_'®Ṡị“-| ”j⁷  Main link. Argument: s (string)

O                 Ordinal; replace the characters of s with their code points.
 m 0              Concatenate the result with a reversed copy.
  ©               Copy the result to the register.
      ®           Yield the list in the register.
    _'            Perform spawned difference of the character codes.
       Ṡ          Apply the sign function.
        ị“-| ”    Index into that string (indices 1, -1, 0).
              j⁷  Join, separating by linefeeds.
\$\endgroup\$
8
\$\begingroup\$

JavaScript (ES6), 94 bytes

s=>[...s,s].reverse().join``.replace(/./g,(c,_,t)=>t.replace(/./g,d=>d<c?`|`:d>c?`-`:` `)+`
`)

Using a dash because I usually run the SpiderMonkey JS shell on Windows and Unicode doesn't work if I do that.

\$\endgroup\$
1
  • \$\begingroup\$ Brilliant idea with the [...s,s].reverse() +1 \$\endgroup\$
    – Downgoat
    May 27, 2016 at 0:30
5
\$\begingroup\$

Pyth, 31 30

js_BsM_BMclQsm@" |―"._-FCMd*QQ

Test Suite

Sadly can't drop the Qs because of the several bifurcates. Pretty basic algorithm so far, count treats the horizontal bar as 1 byte.

\$\endgroup\$
2
  • \$\begingroup\$ darn it, 2 secs before I posted my 31 char solution ;P \$\endgroup\$
    – Maltysen
    May 26, 2016 at 21:43
  • 1
    \$\begingroup\$ @Maltysen I got tired of being out FGITWed ;) Anyway I'm sure it can be shorter... \$\endgroup\$ May 26, 2016 at 21:45
5
\$\begingroup\$

Haskell, 66 bytes

u s|e<-s++reverse s=unlines[["- |"!!min(length[a..b])2|a<-e]|b<-e]
\$\endgroup\$
4
\$\begingroup\$

JavaScript ES6, 138 126 123 bytes

s=>(a=(p=[...s]).map(l=>(b=p.map(i=>i<l?"|":i>l?"-":" ").join``)+[...b].reverse().join``)).concat([...a].reverse()).join`
`

most of the code is the reflecting / flipping

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

J, 26 20 bytes

6 bytes thanks to @Zgarb.

' |-'{~3*@-/~@u:[,|.

Previous 26-byte answer

({&' |-')@*@-/~@(3&u:)@,|.

Uses the same algorithm as Dennis' answer.

Usage:

>> f =: ' |-'{~3*@-/~@u:[,|.
>> f 'food'
<<  --||-- 
   |  ||  |
   |  ||  |
   ---  ---
   ---  ---
   |  ||  |
   |  ||  |
    --||-- 

>> f 'supercalifragilisticexpialidocious'
<<  -|||||||||||||| -|||-||||||||||-  -||||||||||-|||- ||||||||||||||- 
   | |||||||||||||||||||-|||||||||| || ||||||||||-||||||||||||||||||| |
   -- |-|||||-|||||--|||- |||||||||----||||||||| -|||--|||||-|||||-| --
   --- -||----|-------| ---|--|-|--------|-|--|--- |-------|----||- ---
   --|| ||||| |||||--|||-||||||||||----||||||||||-|||--||||| ||||| ||--
   ----- |----|------- ----|---- -------- ----|---- -------|----| -----
   ------ ---- ------------ ------------------ ------------ ---- ------
   ---|-|| ||-||| |--|||--|| ||-||------||-|| ||--|||--| |||-|| ||-|---
   ---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
   ---|-||-- -|-------||---|--|-|--------|-|--|---||-------|- --||-|---
   --|| ||||| |||||--|||-||||||||||----||||||||||-|||--||||| ||||| ||--
   ------ ---- ------------ ------------------ ------------ ---- ------
   ---|-||--|-| ------||---|--|-|--------|-|--|---||------ |-|--||-|---
   ---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
   ---|-|| ||-||| |--|||--|| ||-||------||-|| ||--|||--| |||-|| ||-|---
   ---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
    -|||||||||||||| -|||-||||||||||-  -||||||||||-|||- ||||||||||||||- 
   |-||||||||||||||| |||-||||||||||-||-||||||||||-||| |||||||||||||||-|
   ---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
   ----- |----|------- ----|---- -------- ----|---- -------|----| -----
   --- -||----|-------| ---|--|-|--------|-|--|--- |-------|----||- ---
   ||||||||||||||||||||| |||||||||||||||||||||||| |||||||||||||||||||||
   -- |-|||||-|||||--|||- |||||||||----||||||||| -|||--|||||-|||||-| --
   ---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
   ------ ---- ------------ ------------------ ------------ ---- ------
   ---|-|| ||-||| |--|||--|| ||-||------||-|| ||--|||--| |||-|| ||-|---
   ---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
   -----||----|-------|----|-- -|--------|- --|----|-------|----||-----
   ---|-|||||-|||||--|||--||||| || ---- || |||||--|||--|||||-|||||-|---
   ----- |----|------- ----|---- -------- ----|---- -------|----| -----
   ---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
   ---|-|||||-|||||--|||--||||| || ---- || |||||--|||--|||||-|||||-|---
   | |||||||||||||||||||-|||||||||| || ||||||||||-||||||||||||||||||| |
    -|||||||||||||| -|||-||||||||||-  -||||||||||-|||- ||||||||||||||- 
    -|||||||||||||| -|||-||||||||||-  -||||||||||-|||- ||||||||||||||- 
   | |||||||||||||||||||-|||||||||| || ||||||||||-||||||||||||||||||| |
   ---|-|||||-|||||--|||--||||| || ---- || |||||--|||--|||||-|||||-|---
   ---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
   ----- |----|------- ----|---- -------- ----|---- -------|----| -----
   ---|-|||||-|||||--|||--||||| || ---- || |||||--|||--|||||-|||||-|---
   -----||----|-------|----|-- -|--------|- --|----|-------|----||-----
   ---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
   ---|-|| ||-||| |--|||--|| ||-||------||-|| ||--|||--| |||-|| ||-|---
   ------ ---- ------------ ------------------ ------------ ---- ------
   ---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
   -- |-|||||-|||||--|||- |||||||||----||||||||| -|||--|||||-|||||-| --
   ||||||||||||||||||||| |||||||||||||||||||||||| |||||||||||||||||||||
   --- -||----|-------| ---|--|-|--------|-|--|--- |-------|----||- ---
   ----- |----|------- ----|---- -------- ----|---- -------|----| -----
   ---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
   |-||||||||||||||| |||-||||||||||-||-||||||||||-||| |||||||||||||||-|
    -|||||||||||||| -|||-||||||||||-  -||||||||||-|||- ||||||||||||||- 
   ---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
   ---|-|| ||-||| |--|||--|| ||-||------||-|| ||--|||--| |||-|| ||-|---
   ---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
   ---|-||--|-| ------||---|--|-|--------|-|--|---||------ |-|--||-|---
   ------ ---- ------------ ------------------ ------------ ---- ------
   --|| ||||| |||||--|||-||||||||||----||||||||||-|||--||||| ||||| ||--
   ---|-||-- -|-------||---|--|-|--------|-|--|---||-------|- --||-|---
   ---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
   ---|-|| ||-||| |--|||--|| ||-||------||-|| ||--|||--| |||-|| ||-|---
   ------ ---- ------------ ------------------ ------------ ---- ------
   ----- |----|------- ----|---- -------- ----|---- -------|----| -----
   --|| ||||| |||||--|||-||||||||||----||||||||||-|||--||||| ||||| ||--
   --- -||----|-------| ---|--|-|--------|-|--|--- |-------|----||- ---
   -- |-|||||-|||||--|||- |||||||||----||||||||| -|||--|||||-|||||-| --
   | |||||||||||||||||||-|||||||||| || ||||||||||-||||||||||||||||||| |
    -|||||||||||||| -|||-||||||||||-  -||||||||||-|||- ||||||||||||||- 

(>> meaning input (STDIN), << meaning output (STDOUT))

\$\endgroup\$
2
  • 1
    \$\begingroup\$ With some restructuring, you can get to 20 bytes: ' |-'{~3*@-/~@u:[,|. \$\endgroup\$
    – Zgarb
    May 27, 2016 at 16:37
  • \$\begingroup\$ That's a nice use of forks... \$\endgroup\$
    – Leaky Nun
    May 27, 2016 at 16:41
3
\$\begingroup\$

Mathematica, 124 110 104 102 bytes

a=Join[#,Reverse@#]&;#<>"
"&/@a@a@Table[{"|"," ","-"}[[c~Order~d+2]],{c,b=Characters@#},{d,b}]<>""&

Anonymous function. The Unicode character is U+F3C7 for \[Transpose].

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

Javascript 146 142 132 130 124 bytes

n=>(e=(a=[...n]).map(b=>(d=a.map(c=>c<b?"|":c>b?"-":" ")).concat([...d].reverse()).join``)).concat([...e].reverse()).join`
`

Test suite:

f=n=>{a=n.split``;e=a.map(b=>a.map(c=>c<b?"|":c>b?"-":" ")).map(d=>d.concat([...d].reverse()).join``);alert(e.concat([...e].reverse()).join`
`)}

f(prompt("Enter string!"));

Thanks for @HelkaHomba, for helping to remove at least 50 bytes, and to @Downgoat for 3 bytes!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ you can usually replace =>{...} with =>(...) and replace all the semicolons within that time commas \$\endgroup\$
    – Downgoat
    May 26, 2016 at 22:13
  • \$\begingroup\$ This answer is almost identical to Downgoat's, I swear, I didn't looked at his one. \$\endgroup\$
    – Bálint
    May 26, 2016 at 23:37
3
\$\begingroup\$

Actually, 53 bytes

;l╗;∙`♂O♂ii-s3@%" |-"E`MW╜`d@`nkd@Σ'.o@WX'.@s;R+;♂R¥i

Once again, Actually's poor string-processing abilities are its kryptonite. It's still shorter than Java, so I have that going for me, which is nice.

Try it online!

Explanation:

The code can be separated into 3 distinct portions: the translation code, the processing code, and the mirroring code. For readability, I'm going to explain each section separately.

Translation code (starts with the input string, s, on the stack):

;l╗;∙`♂O♂ii-s3@%" |-"E`M
;l╗                       push len(s) to reg0 (needed for processing step; we'll call this n)
   ;∙                     cartesian product of s with itself
     `♂O♂ii-s3@%" |-"E`M  map:
      ♂O♂ii                 get a pair of ordinals for the characters
           -s               subtract, signum
             3@%            mod by 3 because element access with negative indices isn't working
                " |-"E      get corresponding string

Processing code (starts with a list of n**2 characters, corresponding to the bottom-right corner):

W╜`d@`nkd@Σ'.o@WX
W╜`d@`nkd@Σ'.o@W   while loop (while top of stack is truthy):
 ╜`d@`n              remove n characters from the list
       kd@Σ'.o       concatenate those n characters, and append a period
                X  discard the empty list

Mirroring code (starts with a n**2+n-length string, with periods acting as newlines)

'.@s;R+;♂R¥i
'.@s          split on periods
    ;R+       add the reverse list (vertical mirror)
       ;♂R    make a copy of the list with each string reversed (horizontal mirror)
          ¥   concatenate each pair of strings in the two lists (zip-concat)
           i  flatten list
              (implicitly print each stack item, separated by newlines)
\$\endgroup\$
3
\$\begingroup\$

><>, 109 bytes

i:0(?\:}
,[r]l\~l2,[r]rl2
1-:?!\$:}l1-[}
~]\  \
&r\l:?!;1-
?!\$:@@:@$:@@:@)}(}"- |"{?$@{?$o~~$}&1-:&
4.>~ao]2

Input is via STDIN. Try it online!

Explaination:

The input it read and mirrored on the first line. For input abcd, this leaves dcbaabcd on the stack. Each half is then mirrored to give abcddcba (line 2). Then, each element is duplicated and left on its own stack in turn (lines 3 and 4). After this process, the stack of stacks looks a little like this:

aabcddcba  <-- top of the stack of stacks
b
c
d
d
c
b
a          <-- bottom of the stack of stacks

For each stack in turn, the row value (the bottom of the stack) is compared to the column value (the top of the stack). The appropriate character is selected from - | and written to STDOUT. The column values are then rotated so that the next column is at the top of the stack (line 6).

Once all the columns have been considered, the row value is discarded, a newline printed, and the column values put onto the previous stack (line 7), for the output process to start again.

The ] command, in addition to popping from the stack of stacks, empties the current stack if it is the only one left. The program's end condition is if the stack is empty, as all rows have been processed (line 5).

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

C# 166 143 bytes,

using System.Linq;s=>string.Join("\n",(s+=string.Concat(s.Reverse())).Select(x=>s.Aggregate("",(c, y)=>c+"- |"[Math.Sign(x.CompareTo(y))+1])));

Explanation:

using System.Linq;

s=>                                     // Expression bodied member allows for implicit return
  string.Join("\n",                     // Join the generate lines into the final output
    (s+=string.Concat(s.Reverse()))     // Combine s and its reverse inline so aggregate has the whole line
        .Select(x=>                     // For each character in the line run the aggregate to generate its row
            s.Aggregate("",             // Empty string is required to cooerce the output type from char
                (c, y)=>                // c is the generated string so far, y is the next character
                        c+
                                        // Compare the two letters here (row to column)
                                        // Then take the sign of the result to collapse to -1, 0, or 1
                                        // Finally add 1 to line it up with the indexes of the constant string;                                             
                        "- |"[Math.Sign(x.CompareTo(y))+1]
)));

Test:

Wordenticons

 |||||||||||||||||||||| 
- -|||-|| |--| ||-|||- -
-| |||-||||--||||-||| |-
--- ----|------|---- ---
---| ---|------|--- |---
---|| -||- -- -||- ||---
-||||| |||||||||| |||||-
---||-- |------| --||---
-------- ------ --------
- -|||-|| |--| ||-|||- -
---|| -||- -- -||- ||---
-|||||-||||  ||||-|||||-
-|||||-||||  ||||-|||||-
---|| -||- -- -||- ||---
- -|||-|| |--| ||-|||- -
-------- ------ --------
---||-- |------| --||---
-||||| |||||||||| |||||-
---|| -||- -- -||- ||---
---| ---|------|--- |---
--- ----|------|---- ---
-| |||-||||--||||-||| |-
- -|||-|| |--| ||-|||- -
 |||||||||||||||||||||| 
\$\endgroup\$
7
  • \$\begingroup\$ I wasn't sure if method only was acceptable, if not let me know and I will adjust my answer accordingly \$\endgroup\$
    – user19547
    May 27, 2016 at 0:13
  • \$\begingroup\$ @Downgoat thanks for the edit, I wasn't sure the proper method to escape the #. \$\endgroup\$
    – user19547
    May 27, 2016 at 0:28
  • \$\begingroup\$ Which apparently only messed up in the preview, good to know \$\endgroup\$
    – user19547
    May 27, 2016 at 0:28
  • \$\begingroup\$ since string implements IEnumerable<char> you can save some bytes by using .Reverse() directly on the string skipping the .ToCharArray() \$\endgroup\$ May 27, 2016 at 7:16
  • \$\begingroup\$ you could also change var a = new[] { '-', ' ', '|' }; to var a = "- |"; because you can use indexing on strings \$\endgroup\$ May 27, 2016 at 7:26
2
\$\begingroup\$

CJam, 20 bytes

l_W%+_ff{-g" |―"=}N*

Test it here.

Uses the obvious approach of computing an outer product and using difference and sgn to compute the character in each cell.

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

C#, 169 150 bytes

thanks FryAmTheEggman

void f(string s){s+=new string(s.Reverse().ToArray());foreach(char c in s){var t="";foreach(char k in s)t+=c==k?" ":c>k?"|":"-";Console.WriteLine(t);}

ungolfed:

    public static void f(string s)
    {
        s += new string(s.Reverse().ToArray());
        foreach (char c in s)
        {
            var t="";
            foreach (char k in s)
            t+=c==k?" ":c>k?"|":"-";

            Console.WriteLine(t);
        }

    }

more golfing advice appreciated

\$\endgroup\$
2
  • \$\begingroup\$ t+=c==k?" ":c>k?"|":"-"; should work. I haven't golfed C# much, but it's quite possible that using regular for loops will be shorter. \$\endgroup\$ May 27, 2016 at 13:41
  • \$\begingroup\$ This answer has the same problem my original C# answer did in that both Reverse() and ToArray() are part of System.Linq so a using statement is required. \$\endgroup\$
    – user19547
    Jun 22, 2016 at 19:33
2
\$\begingroup\$

Clojure, 171 bytes

(fn[w](let[f concat r reverse p(map #(f %(r %))(partition(count w)(for[x w y w :let[c(compare x y)]](if(neg? c)\-(if(pos? c)\|\ )))))](run! #(apply println %)(f p(r p)))))

ungolfed:

(fn [w]
  (let [n (count w)
        a (for [x w y w
                :let [c (compare x y)]]
            (if (neg? c)
              \-
              (if (pos? c)
                \|
                \ )))
        p (map #(concat % (reverse %))(partition n a))
        p (concat p (reverse p))]
    (run! #(apply println %) p))))
\$\endgroup\$
2
\$\begingroup\$

J, 75 70 bytes

5 bytes saved thanks to Dennis.

3 :'(],.|:@|.@|:)(],|.)''- |''{~]([:-.@*(,~@#$])-(,~@#$(##])@]))3 u:y'

I'll work on converting it into a tacit verb later.

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

Octave, 39 bytes

@(x)'| -'(sign([x,y=flip(x)]-[x y]')+2)

Creates an anonymous function that is able to be run using ans('string').

Demo

Explanation

This solution combines the input string (x) and it's inverse (flip(x)) using [x, flip(x)]. The inverse is assigned to y to shorten the answer, [x, y = flip(x)]. We then create a column vector of the same thing by combining x and y and taking the transpose: [x,y]'. Then we take the difference which will automatically broadcast to create a 2D array of differences between any ASCII representations of letters in the strings. We use sign to make these either -1, 0, or 1 and then add 2 to get valid 1-based index values. We then use these to index into the initial string '| -'.

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

Julia, 70 bytes

This is my first attempt at code golf and I have not used Julia before, so tell me what you think:

f(s)=join([join([r>c?'|':r<c?'―':' 'for c=s])for r=s*=reverse(s)],"
")

Try it online!

Ungolfed:

function wordenticon(word::AbstractString)
    word*=reverse(word)
    join([
        join([
            if r>c
                '|'
            elseif r<c
                '―'
            else
                ' '
            end
            for c in word
        ])
        for r in word]
        ,"\n"
    )
end

I think it could probably be made shorter. This code stores the characters of the wordicon in a matrix:

f(s)=[r>c?'|':r<c?'―':' 'for r=s*=reverse(s),c=s]

Unfortunately, I couldn't manage to produce the desired output using the matrix.

\$\endgroup\$
1
  • \$\begingroup\$ Hello, and welcome to PPCG! Thanks for joining us! \$\endgroup\$ May 29, 2016 at 17:08
2
\$\begingroup\$

R, 101 bytes

101 bytes since I'm using (which I think looks better than -).

function(s)write(c("|"," ","―")[sign(outer(g<-c(r<-utf8ToInt(s),rev(r)),g,"-"))+2],"",2*nchar(s),,"")

Try it online!

I was surprised there wasn't an R answer before since we can exploit the symmetry and R's matrices to get a quite competitive answer, despite this being a string problem.

Ungolfed Explanation:

function(s){
 r <- utf8ToInt(s)               # turn to vector of ints (charcodes)
 g <- c(r, rev(r))               # concatenate r and its reverse
 idx <- sign(outer(g,g,"-")) + 2 # compute all differences and their signs.
                                 # -1=>less than, 0=>equal, +1=>greater than
                                 # add 2 to make them 1-based indices into the vector
 write(c("|"," ","―")[idx],"",2*nchar(s),,"")
     # write the vector of characters to stdout "" with line width 2*nchar(s)
     # and no separator
}
\$\endgroup\$
2
\$\begingroup\$

Vyxal j, 23 bytes

Ẋƛḣ>[\||ḣ=‛- $i];?Lẇvmm

Try it Online!

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

Jolf, 42 bytes

Hardly golfed. I'm probably forgetting about a matrix builtin that Jolf has.

ΆΖR~mGiEd+γR~mGiEΨ."| -"hmA-~@ά~@HE_γSSZiζ

Try it here! This code uses an arrow function (Ψ) for the matrix map.

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

Javascript, 303 Bytes

function w(o){function r(o){var r=Array.prototype.slice.call(o).reverse();console.log(o.join("")+r.join(""))}var e,n,c,h=[],s=o.length;for(e=0;s>e;e++){for(h.push([]),n=0;s>n;n++)c=o.charCodeAt(n)-o.charCodeAt(e),0===c?h[e].push(" "):0>c?h[e].push("|"):h[e].push("-");r(h[e])}for(e=s-1;e>=0;e--)r(h[e])}

Ungolfed

function w(s) {
    var arr = [],
        l = s.length, r, c, x;
    for (r = 0; r < l; r++) {
        arr.push([]);
        for (c = 0; c < l; c++) {
            x = s.charCodeAt(c) - s.charCodeAt(r);
            if (0 === x) {
                arr[r].push(' ');
            } else if (x<0) {
                arr[r].push('|');
            } else {
                arr[r].push('-');
            }
        }
        out(arr[r]);
    }
    for (r = l - 1; r>=0; r--) {
        out(arr[r]);
    }
    function out(r){
        var rev = Array.prototype.slice.call(r).reverse();
        console.log(r.join('') + rev.join(''));
    }
}

No ecma 2015 fanciness here

\$\endgroup\$
2
  • \$\begingroup\$ You can check, if a letter comes before in alphahet, by simply doing "a"<"b" \$\endgroup\$
    – Bálint
    May 27, 2016 at 6:52
  • \$\begingroup\$ You don't need to bother with var x = 1, just do x = 1. In code golf no-one cares about adhering to best practices. :) \$\endgroup\$
    – gcampbell
    May 27, 2016 at 13:08
1
\$\begingroup\$

Python 2, 126 bytes

def f(s):x=[''.join(" -|"[cmp(ord(a),ord(b))]for a in s)for b in s];y=[a+b[::-1]for a,b in zip(x,x)];print'\n'.join(y+y[::-1])

This is essentially a port of my Actually solution.

Try it online

Explanation:

x=[''.join(" -|"[cmp(ord(a),ord(b))]for a in s)for b in s] # get the correct character for each pair of characters in the Cartesian product of s with itself, and concatenate the characters in each line
y=[a+b[::-1]for a,b in zip(x,x)] # mirror each line horizontally
print'\n'.join(y+y[::-1]) # mirror vertically and print
\$\endgroup\$
1
\$\begingroup\$

Python 3.5, 250 223 175 bytes:

def H(o):O=ord;G=len(o);p=[[' ―'[O(i)<O(g)],'|'][O(i)>O(g)]for i in o for g in o];u='\n'.join([''.join(p[i:G+i]+p[i:G+i][::-1])for i in range(0,len(p),G)]);print(u+'\n'+u[::-1])

Try It Online! (Ideone) (The last two test cases won't show up in the output since they are just blank lines. My program is processing them though, which is confirmed the fact that there are 10 cases input, but only 8 outputs appear.)

Ungolfed followed by an Explanation:

def H(o):
    O=ord
    G=len(o)
    p=[[' ―'[O(i)<O(g)],'|'][O(i)>O(g)]for i in o for g in o]
    u='\n'.join([''.join(p[i:G+i]+p[i:G+i][::-1])for i in range(0,len(p),G)])
    print(u+'\n'+u[::-1])
  1. p=[[' ―'[O(i)<O(g)],'|'][O(i)>O(g)]for i in o for g in o]

    Create a list, p, where a | is added if the Unicode Point Value of the column letter is less than the row letter's value, a is added if the Unicode Point Value of the column letter is more than the row letter's value, or a if both values are equal.

  2. u='\n'.join([''.join(p[i:G+i]+p[i:G+i][::-1])for i in range(0,len(p),G)])

    Create a newline joined string, u, from list p by splitting it into joined string segments each consisting of input length number of characters both forwards and backwards, resulting in each one having the length of 2 times how ever many characters there are in the input. This is the top half of your wordenticon. So, in the case of your input being food, this would return:

     ――||―― 
    |  ||  |
    |  ||  |
    ―――  ―――
    
  3. print(u+'\n'+u[::-1])

    Finally, output u followed by a newline and then u reversed to vertically mirror the first half for the second half. This is your completed wordenticon, which for the test case food would finally be:

     ――||―― 
    |  ||  |
    |  ||  |
    ―――  ―――
    ―――  ―――
    |  ||  |
    |  ||  |
     ――||―― 
    
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 20 22 21 19 13 bytes

δ.S… |—sèJ»º∊

+2 bytes as bug-fix for single-char inputs (but later on -2 again by putting the fix at a different position in the code)
-7 bytes thanks to @ovs.

Takes the input as a list of characters.

Try it online or verify all test cases.

Explanation:

δ              # Apply double-vectorized on the (implicit) input-list:
 .S            #  Compare the characters (-1 if a<b; 0 if a==b; 1 if a>b)
   … |—        # Push string " |—"
       sè      # Index the -1,0,1 of the matrix into this string
         J     # Join each inner list together to a string
          »    # Join this list of strings by newlines
           º   # Mirror each line left to right
            ∊  # And mirror the entire block top to bottom
               # (and output the result implicitly)
\$\endgroup\$
2
  • \$\begingroup\$ 15 bytes in the Elixir rewrite with δ instead of a loop. \$\endgroup\$
    – ovs
    Jun 8, 2021 at 10:59
  • \$\begingroup\$ @ovs Thanks! I didn't knew .S works on characters in the new version. (I also didn't knew a matrix [[0,1,-1],...] can't index into a string in the legacy version.. -_- Also, no need for the leading SD if we take the input as a list of characters. :) \$\endgroup\$ Jun 8, 2021 at 11:59
1
\$\begingroup\$

APL (Dyalog Extended), 24 bytes

⍪∘⊖⍨⍤,∘⌽⍨' |―'⊇⍨∘.(≠+<)⍨

Try it online!

Uses Extended for character comparison.

minus a lot of bytes from Adám.

uses ⎕IO←0 (0-indexing)

Explanation(old)

{(⊢⍪⊖)(⊢,⌽)' |―'[∘.{1+⍺(≠+<)⍵}⍨⍵]} ⍵ → input.
                 ∘.           ⍨⍵   cartesian product of ⍵, with the following function:
                   {1+⍺(≠+<)⍵}     return 1 if equal, 2 if greater, 3 if lesser.
           ' |―'[               ]  index into the required characters
      (⊢,⌽)                        append the reverse of the pattern
 (⊢⍪⊖)                             append the y-axis reverse of the pattern
\$\endgroup\$
2
  • \$\begingroup\$ 26: ⍪∘⊖⍨⍤,∘⌽⍨' |―'⊇⍨1+∘.(≠+<)⍨ Try it online! \$\endgroup\$
    – Adám
    Jun 8, 2021 at 13:45
  • \$\begingroup\$ Get rid of 1+ by going ⎕IO←0 \$\endgroup\$
    – Adám
    Jun 8, 2021 at 14:55
1
\$\begingroup\$

Vyxal, 14 bytes

mC:ǒ-±` |-`İṠ⁋

Try it Online!

m              # Mirror
 C             # Get charcodes
  :ǒ-          # Outer product by subtraction
           İ   # Index
     ±         # Signs of those differences
      ` |-`    # Into " |-"
            Ṡ⁋ # Concatenate and join on newlines
\$\endgroup\$
1
\$\begingroup\$

Perl 5 -F, 76 bytes

say@$_,reverse@$_ for(@;=map{//;[map{($",'-','|')[$_ cmp$']}@F]}@F),reverse@

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.