31
\$\begingroup\$

Given a list of at least two words (made only of lowercase letters), construct and display an ASCII ladder of the words by alternating the direction of writing first to the right, then to the left, relatively to the initial direction from left to right.

When you finish writing a word, change the direction and only then start writing the next word.

If your language doesn't support lists of words, or it's more convenient for you, you can take the input as a string of words, separated by a single space.

Leading and trailing whitespaces are allowed.

["hello", "world"] or "hello world"

hello
    w
    o
    r
    l 
    d

Here we start by writing hello and when we come to the next word (or in the case of the input as a string - a space is found), we change the relative direction to the right and continue to write world

Test cases:

["another", "test", "string"] or "another test string" ->   

another
      t
      e
      s
      tstring


["programming", "puzzles", "and", "code", "golf"] or "programming puzzles and code golf" ->

programming
          p
          u
          z
          z
          l
          e
          sand
             c
             o
             d
             egolf

["a", "single", "a"] or "a single a" ->

a
s
i
n
g
l
ea

Wining criteria

The shortest code in bytes in every language wins. Don't let be discouraged by the golfing languages!

Sandbox

\$\endgroup\$
2

32 Answers 32

1
2
1
\$\begingroup\$

[ bash + stty ], 155 bytes

This does not trump @Rich solution but with more work it might be good. I realise it has an error atm (direction of new word) but since I don't have the reputation to comment on Rich's solution I have added this new answer even with an error to show another approach. It also does not suffer from a problem with whether at bottom of screen or not.

It wont work with TIO either ("not a possible ioctl" error).

j=0;for s;do
l=${#s};stty -onlcr
if((j%2==0));then
printf "%s\n\b" $s;else
for((i=0;i<$l;i++));do
printf "%s\n\b" ${s:$i:1}
done;fi;((j+=1));done;stty onlcr

Code that does work is:

j=0;for s;do l=${#s};stty -onlcr
if !((j%2));then
F=%s
f=$s
else
for((i=0;i<$l;i++));do
F=$F\\n\\b%s
f=$f\ ${s:$i:1}
done
fi
printf $F $f
f=;F=
((j+=1))
done
stty onlcr

Not so good as @Rich but 168 bytes. Could be cheeky and remove initialisation to save 4 bytes but that I feel is cheating.

\$\endgroup\$
1
  • \$\begingroup\$ AH-HA there is ANOTHER way that avoids stty. There is a VT character (Vertical Tab) that effectively moves down one line on a screen. Removing the stty code and changing \\n to \\v works for 127 bytes. \$\endgroup\$ – PJF May 28 '19 at 16:56
1
\$\begingroup\$

Python 3, 218 bytes


words=input().split();space=-1;
for i in range(len(words)):
 if i%2==0:
  print(words[i]);space+=len(words[i])
 else:
  for ch in words[i][:-1]:        
   print(" "*(space)+ch)
  print(" "*(space)+words[i][-1],end="")

Try it online!

218 bytes i need to learn to reduce it ...

\$\endgroup\$
2
  • \$\begingroup\$ Tips for golfing in Python may help you with that... \$\endgroup\$ – manatwork May 28 '19 at 10:53
  • \$\begingroup\$ Welcome to PPCG! I formatted the submission for you. You can easily trim the leading and trailing whitespaces and use tabs: TIO \$\endgroup\$ – Galen Ivanov May 28 '19 at 10:57
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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