Sha256: 7fffc1a624a14ad8942ad89deaa24cfe0ac20844c785831022643835d7f44363

Contents?: true

Size: 1.04 KB

Versions: 65

Compression:

Stored size: 1.04 KB

Contents

use "itertools"

primitive Atbash
  fun _transpose(c: U8): U8 =>
    if (('a' <= c) and (c <= 'z')) then
      ('z' - c) + 'a'
    else
      c
    end

  fun _transposable(c: U8): Bool =>
    (('a' <= c) and (c <= 'z')) or (('0' <= c) and (c <= '9'))

  fun _group(groups: Array[String ref], c: U8): Array[String ref]^ =>
    let last_idx = groups.size() - 1
    let last = try groups(last_idx) else String end
    if last.size() < 5 then
      last.push(c)
    else
      last.push(' ')
      groups.push(String(6).>push(c))
    end
    groups

  fun encode(input: String): String iso^ =>
    try
      String.join(
        Iter[U8](input.lower().values())
          .filter(this~_transposable())
          .map[U8]({(c: U8): U8 => Atbash._transpose(c)})
          .fold[Array[String ref]](this~_group(), [String(6)]))
    else
      recover String end
    end

  fun decode(input: String): String iso^ =>
    Iter[U8](input.values())
      .filter({(c: U8): Bool => c != ' '})
      .map[U8](this~_transpose())
      .collect[String iso](recover String end)

Version data entries

65 entries across 65 versions & 1 rubygems

Version Path
trackler-2.0.8.34 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.0.8.33 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.0.8.32 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.0.8.31 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.0.8.30 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.0.8.29 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.0.8.28 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.0.8.27 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.0.8.26 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.0.8.24 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.0.8.23 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.0.8.22 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.0.8.21 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.0.8.20 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.0.8.19 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.0.8.18 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.0.8.17 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.0.8.16 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.0.8.15 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.0.8.14 tracks/pony/exercises/atbash-cipher/example.pony