Python 2Python 3, 9287 bytes
s=inputr,*s=input();p=r=s[0];p=r
for c in s[1:]s:d=(ord(cp)-ord(pc)-13)%26-13;r+='<'*13;r+='<'*d+'>'*-d+'>'*d+'*';p=cd+'*';p=c
print (r)
Works with either caselowercase or uppercase.
The program builds the output string r
as it iterates over the characters in the input string. It stores the previous character as p
, and computes the incrementing operation to get from p
to the new character c
.
The interval between the characters is ord(c)-ord(p)
, and (ord(c)-ord(p)-13)%26-13
takes it modulo 26 to the interval [-13..12]
. A negative result means it's shorter to step down, and a positive result means to step up.
This needs to be converted to a string of >
or <
depending on the sign. Rather than using abs
or a conditional, we take advantage of Python's string multiplication s*n
giving the empty string when n
is negative. In the expression '<'*-d+'>'*d
, the wrong-signed part does not contribute.
The initial state is a handled a bit clumsily. The output string starts withby splitting the input into its first character and the rest with Python 3's unpacking s[0]r,*s=input()
. We start the iteration at the 2ndThe initial character ofis used to start building the input s
string, putting the first character s[0]
as well as the previous valueinitial "previous" char. These give even more trouble in attempts at a recursive function solution
Thanks to ovs for suggesting switching to Python 3 to do this unpacking.