| Bytes | Lang | Time | Link |
|---|---|---|---|
| 021 | Pip l | 210608T191749Z | DLosc |
| 016 | Japt R | 210608T120946Z | Shaggy |
| 020 | Uiua | 240201T124444Z | Joao-3 |
| 076 | Perl 5 F | 240131T221353Z | Xcali |
| 014 | Vyxal | 240131T205214Z | emanresu |
| 015 | MATL | 160526T232144Z | Suever |
| 023 | Vyxal j | 210608T103600Z | emanresu |
| 024 | APL Dyalog Extended | 200929T060603Z | Razetime |
| 013 | 05AB1E | 190312T163537Z | Kevin Cr |
| 202 | C gcc | 170923T024514Z | Conor O& |
| 101 | R | 170922T172625Z | Giuseppe |
| nan | C# 143 bytes | 160527T001240Z | user1954 |
| 070 | Julia | 160529T170333Z | Tim B. |
| 039 | Octave | 160529T042838Z | Suever |
| 192 | Java | 160526T232359Z | user4825 |
| 066 | Haskell | 160527T221612Z | lynn |
| 070 | J | 160527T010946Z | Conor O& |
| 020 | J | 160527T111446Z | Leaky Nu |
| 171 | Clojure | 160527T155804Z | mark |
| 150 | C# | 160527T125853Z | downrep_ |
| 109 | ><> | 160527T105128Z | Sok |
| 175 | Python 3.5 | 160527T060201Z | R. Kap |
| 020 | CJam | 160527T063851Z | Martin E |
| 126 | Python 2 | 160527T062701Z | user4594 |
| 053 | Actually | 160527T053052Z | user4594 |
| 303 | Javascript | 160527T022930Z | Chris O& |
| 042 | Jolf | 160527T004713Z | Conor O& |
| 3130 | Pyth | 160526T213706Z | FryAmThe |
| 094 | JavaScript ES6 | 160526T232841Z | Neil |
| 124 | Javascript | 160526T215559Z | Bál |
| 102 | Mathematica | 160526T224238Z | LegionMa |
| 123 | JavaScript ES6 | 160526T223105Z | Downgoat |
| 093 | Haskell | 160526T223021Z | nimi |
| 016 | Jelly | 160526T221658Z | Dennis |
Pip -l, 22 21 "bytes"
QR(" |―"$CM Aa@GMC#a)
Explanation
QR(" |―"$CM Aa@GMC#a)
a is first command-line argument (implicit)
MC#a Map this function to each r,c pair where 0 <= r,c < len(a):
a@G Get the chars in a at indices r and c
A ASCII value of each char
$CM Fold on numeric comparison (returns -1, 0, or 1)
(" |―" ) Use each number in the resulting nested list to index into " |―"
(0-based cyclic indexing: 0 -> " ", 1 -> "|", -1 -> "―")
QR Quad-reflect
Autoprint with each sublist on a separate line (-l flag)
Japt -R, 20 16 bytes
pÔ
£c_gXc)g" -|
pÔ\n¬£c_gXc)g" -| :Implicit input of string U
p :Append
Ô : Reverse
\n :Reassign to variable U
¬ :Split
£ :Map each X
c : Map the character codes of U
_ : By passing them through the following function
g : Sign of difference with
Xc : character code of X
) : End sign
g" -| : 0-based index into " -|"
:Implicit output joined with newlines
Uiua, 28 20 bytes
∵(⊡:"| -")+1±⊞-.⊂:⇌.
My older 28-byte solution had it mirroring at the end, but looking at some answers, I decided to mirror in the beginning.
Perl 5 -F, 76 bytes
say@$_,reverse@$_ for(@;=map{//;[map{($",'-','|')[$_ cmp$']}@F]}@F),reverse@
Vyxal, 14 bytes
mC:ǒ-±` |-`İṠ⁋
m # Mirror
C # Get charcodes
:ǒ- # Outer product by subtraction
İ # Index
± # Signs of those differences
` |-` # Into " |-"
Ṡ⁋ # Concatenate and join on newlines
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
APL (Dyalog Extended), 24 bytes
⍪∘⊖⍨⍤,∘⌽⍨' |―'⊇⍨∘.(≠+<)⍨
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
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)
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("");}
Works by looping through each character, then updating the resultant character (and the reflections thereof).
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),,"")
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
}
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
||||||||||||||||||||||
- -|||-|| |--| ||-|||- -
-| |||-||||--||||-||| |-
--- ----|------|---- ---
---| ---|------|--- |---
---|| -||- -- -||- ||---
-||||| |||||||||| |||||-
---||-- |------| --||---
-------- ------ --------
- -|||-|| |--| ||-|||- -
---|| -||- -- -||- ||---
-|||||-|||| ||||-|||||-
-|||||-|||| ||||-|||||-
---|| -||- -- -||- ||---
- -|||-|| |--| ||-|||- -
-------- ------ --------
---||-- |------| --||---
-||||| |||||||||| |||||-
---|| -||- -- -||- ||---
---| ---|------|--- |---
--- ----|------|---- ---
-| |||-||||--||||-||| |-
- -|||-|| |--| ||-|||- -
||||||||||||||||||||||
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)],"
")
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.
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').
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 '| -'.
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
StringBuilderwithString.
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
-|||||||||||||| -|||-||||||||||- -||||||||||-|||- ||||||||||||||-
| |||||||||||||||||||-|||||||||| || ||||||||||-||||||||||||||||||| |
-- |-|||||-|||||--|||- |||||||||----||||||||| -|||--|||||-|||||-| --
--- -||----|-------| ---|--|-|--------|-|--|--- |-------|----||- ---
--|| ||||| |||||--|||-||||||||||----||||||||||-|||--||||| ||||| ||--
----- |----|------- ----|---- -------- ----|---- -------|----| -----
------ ---- ------------ ------------------ ------------ ---- ------
---|-|| ||-||| |--|||--|| ||-||------||-|| ||--|||--| |||-|| ||-|---
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
---|-||-- -|-------||---|--|-|--------|-|--|---||-------|- --||-|---
--|| ||||| |||||--|||-||||||||||----||||||||||-|||--||||| ||||| ||--
------ ---- ------------ ------------------ ------------ ---- ------
---|-||--|-| ------||---|--|-|--------|-|--|---||------ |-|--||-|---
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
---|-|| ||-||| |--|||--|| ||-||------||-|| ||--|||--| |||-|| ||-|---
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
-|||||||||||||| -|||-||||||||||- -||||||||||-|||- ||||||||||||||-
|-||||||||||||||| |||-||||||||||-||-||||||||||-||| |||||||||||||||-|
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
----- |----|------- ----|---- -------- ----|---- -------|----| -----
--- -||----|-------| ---|--|-|--------|-|--|--- |-------|----||- ---
||||||||||||||||||||| |||||||||||||||||||||||| |||||||||||||||||||||
-- |-|||||-|||||--|||- |||||||||----||||||||| -|||--|||||-|||||-| --
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
------ ---- ------------ ------------------ ------------ ---- ------
---|-|| ||-||| |--|||--|| ||-||------||-|| ||--|||--| |||-|| ||-|---
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
-----||----|-------|----|-- -|--------|- --|----|-------|----||-----
---|-|||||-|||||--|||--||||| || ---- || |||||--|||--|||||-|||||-|---
----- |----|------- ----|---- -------- ----|---- -------|----| -----
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
---|-|||||-|||||--|||--||||| || ---- || |||||--|||--|||||-|||||-|---
| |||||||||||||||||||-|||||||||| || ||||||||||-||||||||||||||||||| |
-|||||||||||||| -|||-||||||||||- -||||||||||-|||- ||||||||||||||-
-|||||||||||||| -|||-||||||||||- -||||||||||-|||- ||||||||||||||-
| |||||||||||||||||||-|||||||||| || ||||||||||-||||||||||||||||||| |
---|-|||||-|||||--|||--||||| || ---- || |||||--|||--|||||-|||||-|---
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
----- |----|------- ----|---- -------- ----|---- -------|----| -----
---|-|||||-|||||--|||--||||| || ---- || |||||--|||--|||||-|||||-|---
-----||----|-------|----|-- -|--------|- --|----|-------|----||-----
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
---|-|| ||-||| |--|||--|| ||-||------||-|| ||--|||--| |||-|| ||-|---
------ ---- ------------ ------------------ ------------ ---- ------
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
-- |-|||||-|||||--|||- |||||||||----||||||||| -|||--|||||-|||||-| --
||||||||||||||||||||| |||||||||||||||||||||||| |||||||||||||||||||||
--- -||----|-------| ---|--|-|--------|-|--|--- |-------|----||- ---
----- |----|------- ----|---- -------- ----|---- -------|----| -----
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
|-||||||||||||||| |||-||||||||||-||-||||||||||-||| |||||||||||||||-|
-|||||||||||||| -|||-||||||||||- -||||||||||-|||- ||||||||||||||-
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
---|-|| ||-||| |--|||--|| ||-||------||-|| ||--|||--| |||-|| ||-|---
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
---|-||--|-| ------||---|--|-|--------|-|--|---||------ |-|--||-|---
------ ---- ------------ ------------------ ------------ ---- ------
--|| ||||| |||||--|||-||||||||||----||||||||||-|||--||||| ||||| ||--
---|-||-- -|-------||---|--|-|--------|-|--|---||-------|- --||-|---
---|-||- |-|| - -- ||-- |- |-| ------ |-| -| --|| -- - ||-| -||-|---
---|-|| ||-||| |--|||--|| ||-||------||-|| ||--|||--| |||-|| ||-|---
------ ---- ------------ ------------------ ------------ ---- ------
----- |----|------- ----|---- -------- ----|---- -------|----| -----
--|| ||||| |||||--|||-||||||||||----||||||||||-|||--||||| ||||| ||--
--- -||----|-------| ---|--|-|--------|-|--|--- |-------|----||- ---
-- |-|||||-|||||--|||- |||||||||----||||||||| -|||--|||||-|||||-| --
| |||||||||||||||||||-|||||||||| || ||||||||||-||||||||||||||||||| |
-|||||||||||||| -|||-||||||||||- -||||||||||-|||- ||||||||||||||-
Haskell, 66 bytes
u s|e<-s++reverse s=unlines[["- |"!!min(length[a..b])2|a<-e]|b<-e]
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.
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))
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))))
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
><>, 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).
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])
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 aif both values are equal.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 listpby 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 beingfood, this would return:――||―― | || | | || | ――― ―――print(u+'\n'+u[::-1])Finally, output
ufollowed by a newline and thenureversed to vertically mirror the first half for the second half. This is your completed wordenticon, which for the test casefoodwould finally be:――||―― | || | | || | ――― ――― ――― ――― | || | | || | ――||――
CJam, 20 bytes
l_W%+_ff{-g" |―"=}N*
Uses the obvious approach of computing an outer product and using difference and sgn to compute the character in each cell.
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.
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
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.
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)
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
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.
Pyth, 31 30
js_BsM_BMclQsm@" |―"._-FCMd*QQ
Sadly can't drop the Qs because of the several bifurcates. Pretty basic algorithm so far, count treats the horizontal bar as 1 byte.
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.
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!
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].
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
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.
Jelly, 16 bytes
Om©0_'®Ṡị“-| ”j⁷
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.