Skip to content

Commit

Permalink
awk/difference-of-squares: 1st iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
vpayno committed Apr 17, 2024
1 parent 5d07bcf commit 239b734
Show file tree
Hide file tree
Showing 8 changed files with 307 additions and 3 deletions.
1 change: 1 addition & 0 deletions awk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,4 @@
- [space-age](./space-age/README.md)
- [all-your-base](./all-your-base/README.md)
- [high-scores](./high-scores/README.md)
- [difference-of-squares](./difference-of-squares/README.md)
Empty file.
9 changes: 8 additions & 1 deletion awk/difference-of-squares/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ Finding the best algorithm for the problem is a key skill in software engineerin

### Based on

Problem 6 at Project Euler - https://projecteuler.net/problem=6
Problem 6 at Project Euler - https://projecteuler.net/problem=6

### My Solution

- [my solution](./difference-of-squares.awk)
- [awkunit tests](./difference-of-squares_test.awk)
- [test cases](./test-cases.awk)
- [run-tests](./run-tests-awk.txt)
1 change: 1 addition & 0 deletions awk/difference-of-squares/awkunit.awk
47 changes: 45 additions & 2 deletions awk/difference-of-squares/difference-of-squares.awk
Original file line number Diff line number Diff line change
@@ -1,4 +1,47 @@
#!/usr/bin/gawk -M --lint --file

function squareOfSum(number) {
return (number * (number + 1) / 2) ^ 2
}

function sumOfSquares(number) {
return (number * (number + 1) * (2 * number + 1)) / 6
}

function differenceOfSquares(number) {
return squareOfSum(number) - sumOfSquares(number)
}

BEGIN {
print "Implement this solution" > "/dev/stderr"
exit 1
}

{
if (match($0, /^difference,/)) {
_ = split($0, part, ",")

print differenceOfSquares(part[2])

exit 0
}

if (match($0, /^square_of_sum,/)) {
_ = split($0, part, ",")

print squareOfSum(part[2])

exit 0
}

if (match($0, /^sum_of_squares,/)) {
_ = split($0, part, ",")

print sumOfSquares(part[2])

exit 0
}

print "error"
}

END {
}
74 changes: 74 additions & 0 deletions awk/difference-of-squares/difference-of-squares_test.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/gawk --bignum --lint --file

@include "awkunit"
@include "test-cases"
@include "difference-of-squares"

passed = 0
testCount = 0

function _debugTestPre() {
printf "Test %s: %s\n", (passed + 1), test_name
printf " input -> [%s]\n", input
}

function _debugTestPost() {
passed = passed + 1
printf " output -> [%s]\n", got
printf " result -> passed\n\n"
}

function casesDifferenceOfSquares() {
printf "Running %d test cases\n\n", length(cases)
caseNum = 0

# orders array by index in for loop
PROCINFO["sorted_in"] = "@ind_str_asc"

# Associative arrays don't preserve insert order.
for (key in cases) {
input = key
want = cases[key]

test_name = "[" input "]"

_ = split(input, part, ",")

_debugTestPre()

got = 0

if (part[1] == "difference") {
got = differenceOfSquares(part[2])
} else if (part[1] == "square_of_sum") {
got = squareOfSum(part[2])
} else if (part[1] == "sum_of_squares") {
got = sumOfSquares(part[2])
}

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 reduced duplication
casesDifferenceOfSquares()

print "\n" passed " out of " testCount " tests passed!"

# add exit here to keep it from looping
exit 0
}
158 changes: 158 additions & 0 deletions awk/difference-of-squares/run-tests-awk.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
Running automated test file(s):


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

AWKLIBPATH=/usr/lib/x86_64-linux-gnu/gawk:../.lib

/usr/lib/x86_64-linux-gnu/gawk
filefuncs.so
fnmatch.so
fork.so
inplace.so
intdiv.so
ordchr.so
readdir.so
readfile.so
revoutput.so
revtwoway.so
rwarray.so
time.so

../.lib
awkunit.awk
awkunit.so

gawk --lint --file=./awkunit.awk < /dev/null > /dev/null
gawk: ./awkunit.awk:3: warning: `load' is a gawk extension
gawk: warning: function `assertEquals' defined but never called directly
gawk: warning: function `assert' defined but never called directly
gawk: ./awkunit.awk:26: warning: reference to uninitialized variable `_assert_exit'

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

gawk --lint --file=./difference-of-squares.awk < /dev/null > /dev/null

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

gawk --lint --file=./test-cases.awk < /dev/null > /dev/null

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

exit 0

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

Running: bats ./test-difference-of-squares.bats
1..9
ok 1 square of sum 1
ok 2 square of sum 5
ok 3 square of sum 100
ok 4 sum of squares 1
ok 5 sum of squares 5
ok 6 sum of squares 100
ok 7 difference of squares 1
ok 8 difference of squares 5
ok 9 difference of squares 100

real 0m0.351s
user 0m0.210s
sys 0m0.162s

exit 0

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

AWKLIBPATH=/usr/lib/x86_64-linux-gnu/gawk:../.lib

/usr/lib/x86_64-linux-gnu/gawk
filefuncs.so
fnmatch.so
fork.so
inplace.so
intdiv.so
ordchr.so
readdir.so
readfile.so
revoutput.so
revtwoway.so
rwarray.so
time.so

../.lib
awkunit.awk
awkunit.so

Running: gawk --file ./difference-of-squares_test.awk && printf \n%s\n Tests Passed! || printf \n%s\n Tests Failed!

Running 0 tests...

Running 8 test cases

Test 1: [difference,1]
input -> [difference,1]
output -> [0]
result -> passed

Test 2: [difference,100]
input -> [difference,100]
output -> [25164150]
result -> passed

Test 3: [difference,5]
input -> [difference,5]
output -> [170]
result -> passed

Test 4: [square_of_sum,1]
input -> [square_of_sum,1]
output -> [1]
result -> passed

Test 5: [square_of_sum,100]
input -> [square_of_sum,100]
output -> [25502500]
result -> passed

Test 6: [sum_of_squares,1]
input -> [sum_of_squares,1]
output -> [1]
result -> passed

Test 7: [sum_of_squares,100]
input -> [sum_of_squares,100]
output -> [338350]
result -> passed

Test 8: [sum_of_squares,5]
input -> [sum_of_squares,5]
output -> [55]
result -> passed


8 out of 8 tests passed!

real 0m0.009s
user 0m0.005s
sys 0m0.004s

Tests Passed!

exit 0

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

Running: misspell .

real 0m0.038s
user 0m0.044s
sys 0m0.014s

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

20 changes: 20 additions & 0 deletions awk/difference-of-squares/test-cases.awk
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/gawk --lint --file
# test-cases.awk

# key: input
# value: output

BEGIN {
cases["square_of_sum,1"]="1"
cases["square_of_sum,100"]="225"
cases["square_of_sum,100"]="25502500"
cases["sum_of_squares,1"]="1"
cases["sum_of_squares,5"]="55"
cases["sum_of_squares,100"]="338350"
cases["difference,1"]="0"
cases["difference,5"]="170"
cases["difference,100"]="25164150"

# add exit here to keep it from waiting for input
exit 0
}

0 comments on commit 239b734

Please sign in to comment.