Generate Wordenticons

53

6

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.

Calvin's Hobbies

Posted 2016-05-26T21:23:19.320

Reputation: 84 000

Can we take the input as an array of characters? – Downgoat – 2016-05-26T22:38:51.527

@Downgoat No, it should be a normal string unless there's absolutely no other way for your language. – Calvin's Hobbies – 2016-05-26T22:39:55.497

2You've got examples for programming, and and codegolf but you forgot puzzles... – Neil – 2016-05-26T23:21:24.723

Could you add a leaderboard? – Leaky Nun – 2016-05-27T11:11:38.193

So we did eventually find out why you were asking about portmanteaus in chat :) – gcampbell – 2016-05-27T13:01:59.170

Answers

21

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

Suever

Posted 2016-05-26T21:23:19.320

Reputation: 10 257

9:O you outgolfed Dennis! – Downgoat – 2016-05-26T23:46:12.963

@Downgoat This is one of those rare occasions that MATL is shorter than Jelly! – Suever – 2016-05-26T23:58:09.247

Good idea to use the sign function! – Luis Mendo – 2016-05-26T23:58:38.237

18

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
 -|||||||||||||| -|||-||||||||||-  -||||||||||-|||- ||||||||||||||- 
| |||||||||||||||||||-|||||||||| || ||||||||||-||||||||||||||||||| |
-- |-|||||-|||||--|||- |||||||||----||||||||| -|||--|||||-|||||-| --
--- -||----|-------| ---|--|-|--------|-|--|--- |-------|----||- ---
--|| ||||| |||||--|||-||||||||||----||||||||||-|||--||||| ||||| ||--
----- |----|------- ----|---- -------- ----|---- -------|----| -----
------ ---- ------------ ------------------ ------------ ---- ------
---|-|| ||-||| |--|||--|| ||-||------||-|| ||--|||--| |||-|| ||-|---
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
---|-||-- -|-------||---|--|-|--------|-|--|---||-------|- --||-|---
--|| ||||| |||||--|||-||||||||||----||||||||||-|||--||||| ||||| ||--
------ ---- ------------ ------------------ ------------ ---- ------
---|-||--|-| ------||---|--|-|--------|-|--|---||------ |-|--||-|---
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
---|-|| ||-||| |--|||--|| ||-||------||-|| ||--|||--| |||-|| ||-|---
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
 -|||||||||||||| -|||-||||||||||-  -||||||||||-|||- ||||||||||||||- 
|-||||||||||||||| |||-||||||||||-||-||||||||||-||| |||||||||||||||-|
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
----- |----|------- ----|---- -------- ----|---- -------|----| -----
--- -||----|-------| ---|--|-|--------|-|--|--- |-------|----||- ---
||||||||||||||||||||| |||||||||||||||||||||||| |||||||||||||||||||||
-- |-|||||-|||||--|||- |||||||||----||||||||| -|||--|||||-|||||-| --
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
------ ---- ------------ ------------------ ------------ ---- ------
---|-|| ||-||| |--|||--|| ||-||------||-|| ||--|||--| |||-|| ||-|---
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
-----||----|-------|----|-- -|--------|- --|----|-------|----||-----
---|-|||||-|||||--|||--||||| || ---- || |||||--|||--|||||-|||||-|---
----- |----|------- ----|---- -------- ----|---- -------|----| -----
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
---|-|||||-|||||--|||--||||| || ---- || |||||--|||--|||||-|||||-|---
| |||||||||||||||||||-|||||||||| || ||||||||||-||||||||||||||||||| |
 -|||||||||||||| -|||-||||||||||-  -||||||||||-|||- ||||||||||||||- 
 -|||||||||||||| -|||-||||||||||-  -||||||||||-|||- ||||||||||||||- 
| |||||||||||||||||||-|||||||||| || ||||||||||-||||||||||||||||||| |
---|-|||||-|||||--|||--||||| || ---- || |||||--|||--|||||-|||||-|---
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
----- |----|------- ----|---- -------- ----|---- -------|----| -----
---|-|||||-|||||--|||--||||| || ---- || |||||--|||--|||||-|||||-|---
-----||----|-------|----|-- -|--------|- --|----|-------|----||-----
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
---|-|| ||-||| |--|||--|| ||-||------||-|| ||--|||--| |||-|| ||-|---
------ ---- ------------ ------------------ ------------ ---- ------
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
-- |-|||||-|||||--|||- |||||||||----||||||||| -|||--|||||-|||||-| --
||||||||||||||||||||| |||||||||||||||||||||||| |||||||||||||||||||||
--- -||----|-------| ---|--|-|--------|-|--|--- |-------|----||- ---
----- |----|------- ----|---- -------- ----|---- -------|----| -----
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
|-||||||||||||||| |||-||||||||||-||-||||||||||-||| |||||||||||||||-|
 -|||||||||||||| -|||-||||||||||-  -||||||||||-|||- ||||||||||||||- 
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
---|-|| ||-||| |--|||--|| ||-||------||-|| ||--|||--| |||-|| ||-|---
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
---|-||--|-| ------||---|--|-|--------|-|--|---||------ |-|--||-|---
------ ---- ------------ ------------------ ------------ ---- ------
--|| ||||| |||||--|||-||||||||||----||||||||||-|||--||||| ||||| ||--
---|-||-- -|-------||---|--|-|--------|-|--|---||-------|- --||-|---
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
---|-|| ||-||| |--|||--|| ||-||------||-|| ||--|||--| |||-|| ||-|---
------ ---- ------------ ------------------ ------------ ---- ------
----- |----|------- ----|---- -------- ----|---- -------|----| -----
--|| ||||| |||||--|||-||||||||||----||||||||||-|||--||||| ||||| ||--
--- -||----|-------| ---|--|-|--------|-|--|--- |-------|----||- ---
-- |-|||||-|||||--|||- |||||||||----||||||||| -|||--|||||-|||||-| --
| |||||||||||||||||||-|||||||||| || ||||||||||-||||||||||||||||||| |
 -|||||||||||||| -|||-||||||||||-  -||||||||||-|||- ||||||||||||||-  

user48256

Posted 2016-05-26T21:23:19.320

Reputation:

2Welcome to Programming Puzzles & Code Golf! This is a very nice first answer. :) – Alex A. – 2016-05-26T23:30:16.587

1Note that you don't have to use \u2015. Using horizontal bar (or simply dash) in the raw code is fine. – Calvin's Hobbies – 2016-05-26T23:32:06.537

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) – Leaky Nun – 2016-05-27T12:22:14.253

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++){... – Aaron – 2016-05-27T12:31:43.673

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. – Frozn – 2016-05-28T15:11:51.563

you could rewrite your for loops as for-each loop, all what you need to do is convert string to char[] and itterate through it, this could give you at least 10 bytes, you dont need to declare variable m you could reuse w and i'm not sure but i think if you implement own reverse method, replacing append with + could save you a lot – user902383 – 2016-05-28T20:41:19.797

@user902383 I've updated with your code. I had to clean it up just a smidge to keep the output as a square. Also, I removed the input string being appended to the output first. – None – 2016-05-29T04:26:16.127

Maybe a bit late, but you can replace s = s + a[i--] with s += a[i--] also you can but the assigment of a into the first foreach loop. It then looks like this: char x : a=w.toCharArray(). Also the parenthesis around the ternaries isn't necessary – Frozn – 2016-06-14T14:22:07.703

11

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.

nimi

Posted 2016-05-26T21:23:19.320

Reputation: 34 639

8

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.

Dennis

Posted 2016-05-26T21:23:19.320

Reputation: 196 637

8

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.

Neil

Posted 2016-05-26T21:23:19.320

Reputation: 95 035

Brilliant idea with the [...s,s].reverse() +1 – Downgoat – 2016-05-27T00:30:16.060

5

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.

FryAmTheEggman

Posted 2016-05-26T21:23:19.320

Reputation: 16 206

darn it, 2 secs before I posted my 31 char solution ;P – Maltysen – 2016-05-26T21:43:38.610

1@Maltysen I got tired of being out FGITWed ;) Anyway I'm sure it can be shorter... – FryAmTheEggman – 2016-05-26T21:45:29.720

5

Haskell, 66 bytes

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

Lynn

Posted 2016-05-26T21:23:19.320

Reputation: 55 648

4

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

Downgoat

Posted 2016-05-26T21:23:19.320

Reputation: 27 116

4

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))

Leaky Nun

Posted 2016-05-26T21:23:19.320

Reputation: 45 011

1With some restructuring, you can get to 20 bytes: ' |-'{~3*@-/~@u:[,|. – Zgarb – 2016-05-27T16:37:35.893

That's a nice use of forks... – Leaky Nun – 2016-05-27T16:41:22.497

3

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!

Bálint

Posted 2016-05-26T21:23:19.320

Reputation: 1 847

1you can usually replace =>{...} with =>(...) and replace all the semicolons within that time commas – Downgoat – 2016-05-26T22:13:46.800

This answer is almost identical to Downgoat's, I swear, I didn't looked at his one. – Bálint – 2016-05-26T23:37:57.877

3

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].

LegionMammal978

Posted 2016-05-26T21:23:19.320

Reputation: 15 731

3

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

 |||||||||||||||||||||| 
- -|||-|| |--| ||-|||- -
-| |||-||||--||||-||| |-
--- ----|------|---- ---
---| ---|------|--- |---
---|| -||- -- -||- ||---
-||||| |||||||||| |||||-
---||-- |------| --||---
-------- ------ --------
- -|||-|| |--| ||-|||- -
---|| -||- -- -||- ||---
-|||||-||||  ||||-|||||-
-|||||-||||  ||||-|||||-
---|| -||- -- -||- ||---
- -|||-|| |--| ||-|||- -
-------- ------ --------
---||-- |------| --||---
-||||| |||||||||| |||||-
---|| -||- -- -||- ||---
---| ---|------|--- |---
--- ----|------|---- ---
-| |||-||||--||||-||| |-
- -|||-|| |--| ||-|||- -
 |||||||||||||||||||||| 

user19547

Posted 2016-05-26T21:23:19.320

Reputation:

I wasn't sure if method only was acceptable, if not let me know and I will adjust my answer accordingly – None – 2016-05-27T00:13:30.983

@Downgoat thanks for the edit, I wasn't sure the proper method to escape the #. – None – 2016-05-27T00:28:19.900

Which apparently only messed up in the preview, good to know – None – 2016-05-27T00:28:56.513

since string implements IEnumerable<char> you can save some bytes by using .Reverse() directly on the string skipping the .ToCharArray() – grabthefish – 2016-05-27T07:16:12.530

you could also change var a = new[] { '-', ' ', '|' }; to var a = "- |"; because you can use indexing on strings – grabthefish – 2016-05-27T07:26:12.763

and you could save another couple of bytes by at the end merging those 2 lines return r + new string(...); – grabthefish – 2016-05-27T07:30:06.207

@Gunther34567 Thanks for those tips, I was aware of the last one after I had left too, an unfortunate artifact of testing the first draft. – None – 2016-05-27T15:45:43.170

3

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)

Mego

Posted 2016-05-26T21:23:19.320

Reputation: 32 998

3

><>, 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).

Sok

Posted 2016-05-26T21:23:19.320

Reputation: 5 592

2

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.

Conor O'Brien

Posted 2016-05-26T21:23:19.320

Reputation: 36 228

2

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.

Martin Ender

Posted 2016-05-26T21:23:19.320

Reputation: 184 808

2

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

downrep_nation

Posted 2016-05-26T21:23:19.320

Reputation: 1 152

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. – FryAmTheEggman – 2016-05-27T13:41:08.763

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. – None – 2016-06-22T19:33:10.093

2

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))))

mark

Posted 2016-05-26T21:23:19.320

Reputation: 251

2

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 '| -'.

Suever

Posted 2016-05-26T21:23:19.320

Reputation: 10 257

2

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.

Tim B.

Posted 2016-05-26T21:23:19.320

Reputation: 21

Hello, and welcome to PPCG! Thanks for joining us! – NoOneIsHere – 2016-05-29T17:08:55.707

1

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.

Conor O'Brien

Posted 2016-05-26T21:23:19.320

Reputation: 36 228

1

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

Chris O'Kelly

Posted 2016-05-26T21:23:19.320

Reputation: 121

You can check, if a letter comes before in alphahet, by simply doing "a"<"b" – Bálint – 2016-05-27T06:52:34.980

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. :) – gcampbell – 2016-05-27T13:08:51.033

1

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:

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

R. Kap

Posted 2016-05-26T21:23:19.320

Reputation: 4 730

1

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

Mego

Posted 2016-05-26T21:23:19.320

Reputation: 32 998

0

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
}

Giuseppe

Posted 2016-05-26T21:23:19.320

Reputation: 21 077

0

C (gcc), 202 bytes

f(c,i,j,s,t,a,v)char*c,*v;{v=malloc((a=strlen(c)*2)*a);for(j=0;t=c[j];j++)for(i=0;s=c[i];i++)v[j*a+i]=v[j*a+a+~i]=v[a*(a+~j)+i]=v[a*(a+~j)+a+~i]="- |"[(s<t)-(s>t)+1];for(;*v;v+=a)write(1,v,a),puts("");}

Try it online!

Works by looping through each character, then updating the resultant character (and the reflections thereof).

Conor O'Brien

Posted 2016-05-26T21:23:19.320

Reputation: 36 228

0

05AB1E (legacy), 20 22 21 bytes

Ǹ˜ãε… |-s`.Sè}sgôJ∞∊

+2 bytes as bug-fix for single-char inputs..
-1 byte by using regular dashes - instead of , since we can then use … |- instead of "… |―" (since … |― would incorrectly act as a dictionary string)

Uses the legacy version of 05AB1E, since it implicitly joins by newlines when mirroring, which requires an additional explicit » in the new version.

Try it online or verify all test cases.

Explanation:

Ç             # Push the unicode values of the characters of the (implicit) input-string
 ¸˜           # Wrap this into a list and flatten (workaround for single-char inputs,
              #  since `Ç` will then result in a single value instead of a list of values..)
   ã          # Create each possible pair
ε             # Map each pair to:
 … |-         #  Push string " |-" (note: `… |―` cannot be used here, since it will
              #   incorrectly act as a dictionary string)
 s            #  Swap to take the current map-pair
  `           #  Push both values seperated to the stack
   .S         #  Compare them with each other (-1 if a<b; 0 if a==b; 1 if a>b)
 è            #  Use it to index into the string " |―" (-1 will wraparound to the tail)
      }s      # After the map: swap to get the (implicit) input-string again
        gô    # Get its length, and split the mapped list into parts of that size
          J   # Join each character in the inner lists together to a string
           ∞∊ # Mirror both horizontally and vertically (which implicitly joins by newlines
              #  in the legacy version of 05AB1E)
              # (and output the result implicitly)

Kevin Cruijssen

Posted 2016-05-26T21:23:19.320

Reputation: 67 575