omega-rosetta
aien-dev/omega-rosetta
Brought by drakestapleton
omega-rosetta / exercises / atbash-cipher / .meta / example.om
| 1 | note Exercism-style exercise "atbash-cipher": https://exercism.org/tracks/python/exercises/atbash-cipher |
| 2 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 3 | note encode writes the Atbash cipher of s into o (a to z, b to y, ...; letters lower case, digits |
| 4 | note kept, everything else dropped) in groups of 5 separated by one space; decode writes the plain |
| 5 | note text of s (spaces dropped). Both return the number of bytes written. |
| 6 | note Check it with: sh exercise.sh exercises/atbash-cipher |
| 7 | om2 |
| 8 | fn encode s view uchar * o edit uchar * to ulong |
| 9 | p = ulong 0 |
| 10 | k = ulong 0 |
| 11 | for i ulong len s |
| 12 | c = get s i |
| 13 | if and ge c 65 le c 90 |
| 14 | c = add c 32 |
| 15 | end |
| 16 | keep = false |
| 17 | if and ge c 97 le c 122 |
| 18 | c = sub 219 c |
| 19 | keep = true |
| 20 | else |
| 21 | if and ge c 48 le c 57 |
| 22 | keep = true |
| 23 | end |
| 24 | end |
| 25 | if keep |
| 26 | if and gt k 0 eq rem k 5 0 |
| 27 | put o p 32 |
| 28 | p = add p 1 |
| 29 | end |
| 30 | put o p c |
| 31 | p = add p 1 |
| 32 | k = add k 1 |
| 33 | end |
| 34 | end |
| 35 | ret p |
| 36 | end |
| 37 | fn decode s view uchar * o edit uchar * to ulong |
| 38 | p = ulong 0 |
| 39 | for i ulong len s |
| 40 | c = get s i |
| 41 | if and ge c 65 le c 90 |
| 42 | c = add c 32 |
| 43 | end |
| 44 | if and ge c 97 le c 122 |
| 45 | put o p sub 219 c |
| 46 | p = add p 1 |
| 47 | else |
| 48 | if and ge c 48 le c 57 |
| 49 | put o p c |
| 50 | p = add p 1 |
| 51 | end |
| 52 | end |
| 53 | end |
| 54 | ret p |
| 55 | end |