Skip to content

Commit

Permalink
awk/atbash-cipher: 3rd iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
vpayno committed Sep 2, 2023
1 parent 10c612c commit d1bf24b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 23 deletions.
16 changes: 5 additions & 11 deletions awk/atbash-cipher/atbash-cipher.awk
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,20 @@ function atbash_cipher(input) {
return "error: plain-text can't be empty"
}

spacer = "" # is this needed here?

for (i = 1; i <= length(filtered); i++) {
letter = tolower(letters[i])
#print "plain letter: [" letter "]"
shifted_letter = shift_letter(letter)
#print "shifted letter: [" shifted_letter "]"

if (direction == "encode") {
if ((i - 1) % 5 == 0) {
spacer = " "
} else {
spacer = ""
}
}

if (length(cipher_text) == 0) {
cipher_text = shifted_letter
} else {
cipher_text = cipher_text spacer shifted_letter
cipher_text = cipher_text shifted_letter
}

if (direction == "encode") {
cipher_text = gensub(/[a-z0-9]{5}\B/, "& ", "g", cipher_text)
}
}

Expand Down
25 changes: 13 additions & 12 deletions awk/atbash-cipher/run-tests-awk.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ awkunit.so

gawk --lint --file=./atbash-cipher.awk < /dev/null > /dev/null
gawk: ./atbash-cipher.awk:3: warning: `load' is a gawk extension
gawk: ./atbash-cipher.awk:50: warning: `gensub' is a gawk extension

real 0m0.005s
user 0m0.002s
Expand All @@ -37,16 +38,16 @@ gawk: warning: function `assert' defined but never called directly
gawk: ./awkunit.awk:26: warning: reference to uninitialized variable `_assert_exit'

real 0m0.006s
user 0m0.003s
sys 0m0.002s
user 0m0.002s
sys 0m0.004s

gawk --lint --file=./test-cases.awk < /dev/null > /dev/null
gawk: ./test-cases.awk:8: warning: subscript of array `encode_cases' is null string
gawk: ./test-cases.awk:22: warning: subscript of array `decode_cases' is null string

real 0m0.004s
user 0m0.002s
sys 0m0.002s
real 0m0.005s
user 0m0.001s
sys 0m0.003s

exit 0

Expand All @@ -69,9 +70,9 @@ ok 12 decode all the letters
ok 13 decode with too many spaces
ok 14 decode with no spaces

real 0m0.479s
user 0m0.319s
sys 0m0.187s
real 0m0.597s
user 0m0.394s
sys 0m0.231s

exit 0

Expand Down Expand Up @@ -238,9 +239,9 @@ Test 26:

26 out of 26 tests passed!

real 0m0.009s
real 0m0.008s
user 0m0.003s
sys 0m0.006s
sys 0m0.005s

Tests Passed!

Expand All @@ -250,9 +251,9 @@ exit 0

Running: misspell .

real 0m0.033s
real 0m0.042s
user 0m0.041s
sys 0m0.009s
sys 0m0.022s

===============================================================================

0 comments on commit d1bf24b

Please sign in to comment.