Skip to content

Simple Cipher #298

Closed
Closed
@rabestro

Description

@rabestro

Port exercise from Bash track:
https://exercism.org/tracks/bash/exercises/simple-cipher

Something like:

#!/usr/bin/env gawk -f
# These variables are initialized on the command line (using '-v'):
# - command
# - key
@load "ordchr"

BEGIN {
    FS = ""
    OFS = ""
}

{
    for (i = 1; i <= NF; i++)
        @command(i)
    print
}

function encode(i) {
    $i = chr((ord($i) - ord("a") + keyShift()) % 26 + ord("a"))
}
function decode(i) {
    $i = chr((ord($i) - ord("a") - keyShift()) % 26 + ord("a"))
}
function keyShift() {
    KeyIndex %= length(key)
    KeyIndex++
    return ord(substr(key, KeyIndex, 1)) - ord("a")
}

Need to generate a key to pass the test:

@test  "Can generate a random key" {
    #[[ $BATS_RUN_SKIPPED == "true" ]] || skip
    run bash simple_cipher.sh key
    assert_success
    key=$output
    assert [ "${#key}" -ge 100 ]     # at least 100 chars
    [[ "$key" =~ ^[[:lower:]]+$ ]]   # only lowercase letters
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions