19
1
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'aby 1 to the letterb.a<would result inzbecause 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.
Just to be clear, the last test case represents
abcdefghijklmnopqrstuvwxyzand is not its own input? – Nick Clifford – 2017-04-17T19:55:25.4171@NickClifford yes. – Magic Octopus Urn – 2017-04-17T19:57:05.187
I think
zlshould use>. – xnor – 2017-04-17T21:11:27.2434Could you please check the examples?
alphabetis in my opiniona>>>>>>>>>>>*>>>>*<<<<<<<<*<<<<<<<*>*>>>*<<<<<<<<<<<*andzlshould bez>>>>>>>>>>>>*and forbananashould a second solution existsb<*<<<<<<<<<<<<<*>>>>>>>>>>>>>*<<<<<<<<<<<<<*>>>>>>>>>>>>>*– Jörg Hülsermann – 2017-04-17T21:41:26.967@xnor correct, was a manual typo from
zm. @jorg good catches, fixed all of them, was a manual effort. – Magic Octopus Urn – 2017-04-18T13:19:43.923