Sha256: 7d99525680e1ef8948185fc73201270d2390f8591538157d971ff9d813451669

Contents?: true

Size: 1.08 KB

Versions: 46

Compression:

Stored size: 1.08 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)])?
          .values())
    else
      recover String end
    end

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

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
trackler-2.2.1.39 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.38 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.37 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.36 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.35 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.34 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.33 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.32 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.31 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.30 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.29 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.28 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.27 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.26 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.25 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.24 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.23 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.22 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.21 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.20 tracks/pony/exercises/atbash-cipher/example.pony