We'll define the ASCII Odd/Even Cipher via the below pseudocode:
Define 'neighbor' as the characters adjacent to the current letter in the string
If the one of the neighbors is out of bounds of the string, treat it as \0 or null
Take an input string
For each letter in the string, do
If the 0-based index of the current letter is even, then
Use the binary-or of the ASCII codes of both its neighbors
Else
If the ASCII code of the current letter is odd, then
Use the binary-or of itself plus the left neighbor
Else
Use the binary-or of itself plus the right neighbor
In all cases,
Convert the result back to ASCII and return it
If this would result in a code point 127 or greater to be converted, then
Instead return a space
Join the results of the For loop back into one string and output it
For example, for input Hello
, the output is emmol
, since
- The
H
turns to\0 | 'e'
which ise
- The
e
turns to'e' | 'l'
, or101 | 108
, which is109
orm
- The first
l
also turns to101 | 108
orm
- The second
l
turns to108 | 111
, which is111
oro
- The
o
turns to108 | \0
, orl
Input
- A sentence composed solely of printable ASCII characters, in any suitable format.
- The sentence may have periods, spaces, and other punctuation, but will only ever be one line.
- The sentence will be at least three characters in length.
Output
- The resulting cipher, based on the rules described above, returned as a string or output.
The Rules
- Either a full program or a function are acceptable.
- Standard loopholes are forbidden.
- This is code-golf so all usual golfing rules apply, and the shortest code (in bytes) wins.
Examples
Input on one line, output on the following. Blank lines separate examples.
Hello
emmol
Hello, World!
emmol, ww~ved
PPCG
PSWG
Programming Puzzles and Code Golf
r wogsmmoonpuu ~ meannncoooeggonl
abcdefghijklmnopqrstuvwxyz
bcfefgnijknmno~qrsvuvw~yzz
!abcdefghijklmnopqrstuvwxyz
aaccgeggoikkomoo qsswuww yy
Test 123 with odd characters. R@*SKA0z8d862
euutu133www|todddchizsscguwssr`jS{SK{z~|v66
o
changes tol
in the first example, I'm pretty sure your specs ensure that the firsto
does not change tol
in the second example. It should change to'l' | ','
, whatever that is, right? \$\endgroup\$'l' | ','
, which is108 | 44 --> 1101111 | 0101100
, which becomes108
, which isl
. The,
happens to line up with thel
, so there's no change when the binary-or takes place. \$\endgroup\$