Sha256: 7c08329b4f847aace099dd7f702bc31e3c2893a2810a5d00572bb8453bd73ab1

Contents?: true

Size: 1.01 KB

Versions: 119

Compression:

Stored size: 1.01 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^ =>
    String.join(
      Iter[U8](input.lower().values())
        .filter(this~_transposable())
        .map[U8]({(c: U8): U8 => Atbash._transpose(c) })
        .fold[Array[String ref]]([String(6)], this~_group())
        .values())

  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

119 entries across 119 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.179 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.178 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.177 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.176 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.175 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.174 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.173 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.172 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.171 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.170 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.169 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.167 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.166 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.165 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.164 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.163 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.162 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.161 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.2.1.160 tracks/pony/exercises/atbash-cipher/example.pony