-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
180 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,65 @@ | ||
#!/usr/bin/gawk --lint --file | ||
|
||
# These variables are initialized on the command line (using '-v'): | ||
# -direction | ||
# # -direction | ||
|
||
function series(input) { | ||
arg_count = split(input, args, ":") | ||
|
||
if (arg_count != 2) { | ||
return "wrong number of arguments" | ||
} | ||
|
||
span_length = args[1] | ||
number_sequence = args[2] | ||
|
||
number_count = split(number_sequence, numbers, "") | ||
|
||
# could also test to make sure that length(sequence) == count | ||
if (number_count == 0) { | ||
# should be "sequence should have at least one number in it" | ||
return "error: series cannot be empty" | ||
} | ||
|
||
if (span_length <= 0) { | ||
# should be "span length needs to be greater than 0" | ||
return "error: invalid length" | ||
} | ||
|
||
if (span_length > length(number_sequence)) { | ||
# should be "span length can't be larger than sequence length" | ||
return "error: invalid length" | ||
} | ||
|
||
span_sequence = "" | ||
|
||
for (i = 1; i <= (length(input) - span_length - 1); i++) { | ||
span = substr(number_sequence, i, span_length) | ||
|
||
if (length(span_sequence) == 0) { | ||
span_sequence = span | ||
} else { | ||
span_sequence = span_sequence " " span | ||
} | ||
} | ||
|
||
return span_sequence | ||
} | ||
|
||
BEGIN { | ||
print "Implement this solution" > "/dev/stderr" | ||
exit 1 | ||
} | ||
|
||
{ | ||
result = series(len":"$0) | ||
|
||
if (match(result, /^error: /)) { | ||
_ = split(result, part, ":") | ||
print part[2] | ||
exit 1 | ||
} | ||
|
||
print result | ||
} | ||
|
||
END { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#!/usr/bin/gawk --lint --file | ||
|
||
@include "awkunit" | ||
@include "test-cases" | ||
@include "atbash-cipher" | ||
|
||
passed = 0 | ||
testCount = 0 | ||
|
||
function _debugTestPre() { | ||
printf "Test %s:\n", (passed + 1) | ||
printf " input -> [%s]\n", input | ||
} | ||
|
||
function _debugTestPost() { | ||
passed = passed + 1 | ||
printf " output -> [%s]\n", got | ||
printf " result -> passed\n\n" | ||
} | ||
|
||
function testAtbashCipher_zero() { | ||
input = "" | ||
want = "" | ||
|
||
# _ = split(input, a, " ") | ||
|
||
_debugTestPre() | ||
got = atbashCipher(input) | ||
|
||
assertEquals(got, want) | ||
_debugTestPost() | ||
} | ||
|
||
function casesAtbashCipher() { | ||
printf "Running %d test cases\n\n", length(cases) | ||
caseNum = 0 | ||
|
||
# Associative arrays don't preserve insert order. | ||
for (key in cases) { | ||
input = key | ||
want = cases[key] | ||
|
||
# _ = split(input, a, " ") | ||
|
||
_debugTestPre() | ||
got = atbashCipher(input) | ||
|
||
assertEquals(got, want) | ||
_debugTestPost() | ||
} | ||
} | ||
|
||
BEGIN { | ||
exit 0 | ||
} | ||
|
||
END { | ||
cmd = "grep --no-filename --count ^function\\ test *_test.awk" | ||
cmd | getline testCount | ||
|
||
printf "\nRunning %d tests...\n\n", testCount | ||
|
||
testCount = testCount + length(cases) | ||
|
||
# running tests with a lot of duplication | ||
testAtbashCipher_zero() | ||
|
||
# running tests with reduced duplication | ||
casesAtbashCipher() | ||
|
||
print "\n" passed " out of " testCount " tests passed!" | ||
|
||
# add exit here to keep it from looping | ||
exit 0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../.lib/awkunit.awk |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#!/usr/bin/gawk --lint --file | ||
# test-cases.awk | ||
|
||
# key: input | ||
# value: output | ||
|
||
BEGIN { | ||
encode_cases[""]="" # wow, this works with a warning | ||
encode_cases["abcdefghijklmnopqrstuvwxyz"]="zyxwvutsrqponmlkjihgfedcba" | ||
encode_cases["test"]="gvhg" | ||
encode_cases["x123 yes"]="c123b vh" | ||
encode_cases["yes"]="bvh" | ||
encode_cases["no"]="ml" | ||
encode_cases["OMG"]="lnt" | ||
encode_cases["O M G"]="lnt" | ||
encode_cases["mindblowingly"]="nrmwy oldrm tob" | ||
encode_cases["Testing,1 2 3, testing."]="gvhgr mt123 gvhgr mt" | ||
encode_cases["Truth is fiction."]="gifgs rhurx grlm" | ||
encode_cases["The quick brown fox jumps over"]="gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt" | ||
|
||
decode_cases[""]="" # wow, this works with a warning | ||
decode_cases["zyxwvutsrqponmlkjihgfedcba"]="abcdefghijklmnopqrstuvwxyz" | ||
decode_cases["gvhg"]="test" | ||
decode_cases["c123b vh"]="x123 yes" | ||
decode_cases["vcvix rhn"]="exercism" | ||
decode_cases["zmlyh gzxov rhlug vmzhg vkkrm thglm v"]="anobstacleisoftenasteppingstone" | ||
decode_cases["gvhgr mt123 gvhgr mt"]="testing123testing" | ||
decode_cases["gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"]="thequickbrownfoxjumpsoverthelazydog" | ||
decode_cases["vc vix r hn"]="exercism" | ||
decode_cases["zmlyhgzxovrhlugvmzhgvkkrmthglmv"]="anobstacleisoftenasteppingstone" | ||
|
||
# add exit here to keep it from waiting for input | ||
exit 0 | ||
} |