This task is rather simple, and makes use of three distinct "operator" characters. Your task is, given a simple sequence of letters, perform the following task to encode it using <
,>
,*
. You may choose to use either upper or lowercase letters, you do not have to handle both.
Cipher Explanation
The cipher is simple, you're using increment and decrement operations to traverse from letter 1 to the end letter, with *
being your "submit" function. The operator for "increment" will be >
and "decrement" will be <
.
An example using the word adbc
:
- Start with the first letter of the word, output that letter.
a
- Next, use
>
and<
(like brainfuck) to "navigate" the current letter to the next one.a>
would result in 'raising'a
by 1 to the letterb
.a<
would result inz
because you're lowering the letter (it wraps, you must always choose the direction resulting in the LEAST number of operations). - After outputting the correct minimalized combination of
<
and>
output a*
to denote that we've reached the next letter.
The steps to encode adbc
would be:
a # a
a>>>* # ad
a>>>*<<* # adb
a>>>*<<*>* # adbc
Examples
The steps to encode aza
would be:
a # a
a<* # az
a<*>* # aza
More examples:
"abcdef" = "a>*>*>*>*>*"
"zyaf" = "z<*>>*>>>>>*"
"zzzzzz" = "z*****"
"z" = "z"
"zm" = "z<<<<<<<<<<<<<*" or "z>>>>>>>>>>>>>*" (equidistant)
"zl" = "z>>>>>>>>>>>>*"
"alphabet" = "a>>>>>>>>>>>*>>>>*<<<<<<<<*<<<<<<<*>*>>>*<<<<<<<<<<<*"
"banana" = "b<*>>>>>>>>>>>>>*<<<<<<<<<<<<<*>>>>>>>>>>>>>*<<<<<<<<<<<<<*" OR "b<*<<<<<<<<<<<<<*>>>>>>>>>>>>>*<<<<<<<<<<<<<*>>>>>>>>>>>>>*"
"abcdefghijklmnopqrstuvwxyz" = "a>*>*>*>*>*>*>*>*>*>*>*>*>*>*>*>*>*>*>*>*>*>*>*>*>*"
"abcdefz" = "a>*>*>*>*>*<<<<<<*"
Rules
- We are encoding not decoding, so don't mess that up.
- You may assume the message will contain letters
[A-Z]
or[a-z]
, your choice. - You may use any non-letter/numeric/reserved character to denote
*
(E.G.$
). - You must have the ending
*
, it isn't implicit on repeats. - You may assume no empty strings, but a single character is possible.
- If it is equidistant either way to the next letter, you may choose a direction.
- This is code-golf, lowest byte-count wins.
Please explain your answer, it helps others learn this way.
abcdefghijklmnopqrstuvwxyz
and is not its own input? \$\endgroup\$zl
should use>
. \$\endgroup\$alphabet
is in my opiniona>>>>>>>>>>>*>>>>*<<<<<<<<*<<<<<<<*>*>>>*<<<<<<<<<<<*
andzl
should bez>>>>>>>>>>>>*
and forbanana
should a second solution existsb<*<<<<<<<<<<<<<*>>>>>>>>>>>>>*<<<<<<<<<<<<<*>>>>>>>>>>>>>*
\$\endgroup\$zm
. @jorg good catches, fixed all of them, was a manual effort. \$\endgroup\$