Skip to content

Commit

Permalink
awk/raindrops: 1st iteration
Browse files Browse the repository at this point in the history
  • Loading branch information
vpayno committed Sep 3, 2023
1 parent 82f1e99 commit 4c5d944
Show file tree
Hide file tree
Showing 7 changed files with 457 additions and 4 deletions.
1 change: 1 addition & 0 deletions awk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@
- [luhn](./luhn/README.md)
- [series](./series/README.md)
- [atbash-cipher](./atbash-cipher/README.md)
- [raindrops](./raindrops/README.md)
9 changes: 8 additions & 1 deletion awk/raindrops/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,11 @@ You'll need to implement the logic inside the `BEGIN` block.

### Based on

A variation on FizzBuzz, a famous technical interview question that is intended to weed out potential candidates. That question is itself derived from Fizz Buzz, a popular children's game for teaching division. - https://en.wikipedia.org/wiki/Fizz_buzz
A variation on FizzBuzz, a famous technical interview question that is intended to weed out potential candidates. That question is itself derived from Fizz Buzz, a popular children's game for teaching division. - https://en.wikipedia.org/wiki/Fizz_buzz

### My Solution

- [my solution](./raindrops.awk)
- [awkunit tests](./raindrops_test.awk)
- [test cases](./test-cases.awk)
- [run-tests](./run-tests-awk.txt)
1 change: 1 addition & 0 deletions awk/raindrops/awkunit.awk
54 changes: 51 additions & 3 deletions awk/raindrops/raindrops.awk
Original file line number Diff line number Diff line change
@@ -1,7 +1,55 @@
#!/usr/bin/gawk --lint --file

@load "ordchr"

# These variables are initialized on the command line (using '-v'):
# - num
# - num -> ^([0-9]+$

function raindrops(number) {
sounds = ""

if (length(number) == 0) {
return "error: nothing has no sound"
}

if (number == 0) {
return number
}

if (number % 3 == 0) {
sounds = sounds "Pling"
}

if (number % 5 == 0) {
sounds = sounds "Plang"
}

if (number % 7 == 0) {
sounds = sounds "Plong"
}

if (length(sounds) == 0) {
sounds = number
}

return sounds
}

BEGIN {
print "Implement this solution" > "/dev/stderr"
exit 1
# let's make sure direction isn't being used to inject code
if (! match(num, /^[0-9]+$/)) {
print "error: invalid number [" num "], the only supported numbers are ^[0-9]+$"
exit 1
}

print raindrops(num)

exit 0
}

{
# using this section got us stuck waiting for input that is only given via variables
}

END {
}
75 changes: 75 additions & 0 deletions awk/raindrops/raindrops_test.awk
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 "raindrops"

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 testRaindrops_empty() {
input = ""
want = "error: nothing has no sound"

# _ = split(input, a, " ")

_debugTestPre()
got = raindrops(input)

assertEquals(got, want)
_debugTestPost()
}

function casesRaindrops() {
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 = raindrops(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
testRaindrops_empty()

# running tests with reduced duplication
casesRaindrops()

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

# add exit here to keep it from looping
exit 0
}
Loading

0 comments on commit 4c5d944

Please sign in to comment.