Sha256: 41aa93b137c1d8e7392008273091591c4c333635cf881a5fc355de1eba33ad15

Contents?: true

Size: 1.05 KB

Versions: 70

Compression:

Stored size: 1.05 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^ =>
    recover
      Iter[U8](input.values())
        .filter({(c: U8): Bool => c != ' '})
        .map[U8](this~_transpose())
        .collect[String ref](String)
    end

Version data entries

70 entries across 70 versions & 1 rubygems

Version Path
trackler-2.1.0.8 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.1.0.7 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.1.0.6 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.1.0.5 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.1.0.4 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.1.0.3 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.1.0.2 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.1.0.1 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.1.0.0 tracks/pony/exercises/atbash-cipher/example.pony
trackler-2.0.8.55 tracks/pony/exercises/atbash-cipher/example.pony