Skip to main content
added 5 characters in body
Source Link
xnor
  • 146.6k
  • 26
  • 279
  • 652

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)

Try it online!Try it online!

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

Python 2, 92 bytes

s=input();p=r=s[0]
for c in s[1:]:d=(ord(c)-ord(p)-13)%26-13;r+='<'*-d+'>'*d+'*';p=c
print r

Try it online!

Works with either case.

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 with the first character s[0]. We start the iteration at the 2nd character of the input s, putting the first character s[0] as the previous value. These give even more trouble in attempts at a recursive function solution.

Python 3, 87 bytes

r,*s=input();p=r
for c in s:d=(ord(p)-ord(c)-13)%26-13;r+='<'*d+'>'*-d+'*';p=c
print(r)

Try it online!

Works with either lowercase 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 handled by splitting the input into its first character and the rest with Python 3's unpacking r,*s=input(). The initial character is used to start building the string, as well as the initial "previous" char.

Thanks to ovs for suggesting switching to Python 3 to do this unpacking.

Source Link
xnor
  • 146.6k
  • 26
  • 279
  • 652

Python 2, 92 bytes

s=input();p=r=s[0]
for c in s[1:]:d=(ord(c)-ord(p)-13)%26-13;r+='<'*-d+'>'*d+'*';p=c
print r

Try it online!

Works with either case.

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 with the first character s[0]. We start the iteration at the 2nd character of the input s, putting the first character s[0] as the previous value. These give even more trouble in attempts at a recursive function solution.