46
4
Write a program (or function) that takes in a non-empty string of any printable ASCII characters.
Print (or return) a zigzagging chain of the characters in the string with every neighboring pair of characters linked by:
/
if the first character occurs before the second character in normal ASCII order. e.g.B / A
\
if the first character occurs after the second character in normal ASCII order. e.g.B \ A
-
if the first and second characters are the same. e.g.A-A
So the output for Programming Puzzles & Code Golf
would be
o
/ \
r z-z o e G l
/ \ / \ / \ / \ / \
P o r m-m n u l s & C d f
\ / \ / \ / \ / \ / \ / \ /
g a i g P e
\ /
If there is only one character in the input string the output would just be that character.
Your program should treat ,
/
, \
, and -
just the same as all other characters.
e.g. -\//-- \ //-
should produce:
\
/ \
- /-/
/ \
- --- \ /-/
\ / \ / \
-
\
There should be no extraneous newlines in the output except for a single optional trailing newline. (Note that the empty line in the example just above holds the last space in the the string and is therefore not extraneous.) There may be trailing spaces on any lines in any arrangement.
The shortest code in bytes wins.
One more example - Input:
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679
Output:
9 9 8 6 6
/ \ / \ / \ / \ / \
9 6 8 7 3 3 4 2 4 8 9 8-8
/ \ / \ / \ / \ / \ / \ / \
4 5 2 5 5 2 3-3 3 7 5 2 4 9 9 9-9 7
/ \ / \ / \ / \ / \ / \ / \ / \ / \
3 1 1 3 2 0 1 7 6 3 3 5 8 8 6
\ / \ / \ / \ / \ / \
. 1 1 5 2 9 9 3 7 1 4 6 8 9
\ / \ / \ / \ / \ / \ / \ / \ /
0 0 7 9 5 2 0 0 2 6 9-9 8 5 4 7
\ / \ / \ / \ / \ / \ / \ /
4 4-4 2 8 8 4 2 3 2 7 6
\ / \ / \ / \ /
0 6 8 3 1-1 0
\ / \ /
2 0
You can save a byte by replacing
– jrich – 2015-08-30T14:31:29.583'\n'
with a template string like this@UndefinedFunction Yeah, I've used that trick before, but forgot to put it in last night. Thanks for the reminder! – ETHproductions – 2015-08-30T17:46:59.817
Your
for
loops can be squished a lot. Don't waste a whole block of required code withi++
. Instead, run most of yourfor
code in there. Also, you don't need braces around single lines of code. – Not that Charles – 2015-08-31T21:36:59.383It also seems like the only way you use
l
is to calculates.length*2-1
and you do that twice. Why not store that value instead? – Not that Charles – 2015-08-31T21:41:05.027However, it looks like a requirement is to print the string to the screen, and you haven't fulfilled that. Consider using
\n
instead of<br>
and putting to console or alert. – Not that Charles – 2015-08-31T21:43:43.5601@NotthatCharles Thanks for the tips! I'd just tried out the modified algorithm and not bothered to golf it much more yet. The
<br>
is just in there so it shows up in the HTML version; if you look closely, I use a template string instead in the actual entry. Also, it's not a requirement: "Print (or return)..." – ETHproductions – 2015-08-31T21:47:32.987You can save 2 more characters by declaring all the iteration variables separately in that
m=w=n=0
block. You need to add extra variables for the other iterations. So, e.g.,f=j=m=w=n=0
... and then replace thei
s as needed. Then you can drop thei=0
in the laterfor
loops. You may find you save space without using anfor in
loop at the beginning, too. – Not that Charles – 2015-09-04T18:11:03.020