Skip to content

Commit

Permalink
awk/etl: classic awk solution
Browse files Browse the repository at this point in the history
  • Loading branch information
vpayno committed Sep 11, 2023
1 parent 695b898 commit ec32680
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 13 deletions.
1 change: 1 addition & 0 deletions awk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,4 @@
- [resistor-color-duo](./resistor-color-duo/README.md)
- [resistor-color-trio](./resistor-color-trio/README.md)
- [scrabble-score](./scrabble-score/README.md)
- [etl](./etl/README.md)
Empty file added awk/etl/.lint_default_vars
Empty file.
9 changes: 8 additions & 1 deletion awk/etl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,11 @@ This exercise will try to mimic this, using inconsistent input formats that your

### Based on

Based on an exercise by the JumpstartLab team for students at The Turing School of Software and Design. - https://turing.edu
Based on an exercise by the JumpstartLab team for students at The Turing School of Software and Design. - https://turing.edu

### My Solution

- [my solution](./etl.awk)
- [awkunit tests](./etl_test.awk)
- [test cases](./test-cases.awk)
- [run-tests](./run-tests-awk.txt)
1 change: 1 addition & 0 deletions awk/etl/awkunit.awk
58 changes: 56 additions & 2 deletions awk/etl/etl.awk
Original file line number Diff line number Diff line change
@@ -1,4 +1,58 @@
#!/usr/bin/gawk --bignum --lint --file

function etl(input) {
uppercase = ""
score = 0

if (length(input) == 0) {
return uppercase "," score
}

_ = split(input, chars, "")

for (i=1; i<=length(input); ++i) {
letter = toupper(chars[i])

# intentionally using strings with fallthrough cases and single regex cases
switch(letter) {
case /^[AEIOULNRST]$/:
score += 1
break
case "D":
case "G":
score += 2
break
case /^[BCMP]$/:
score += 3
break
case /^[FHVWY]$/:
score += 4
break
case "K":
score += 5
break
case /^[JX]$/:
score += 8
break
case /^[QZ]$/:
score += 10
break
default:
break
}

uppercase = uppercase letter
}

return uppercase "," score
}

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

{
print etl($0)
}

END {
}
20 changes: 10 additions & 10 deletions awk/etl/test-etl.bats
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ END_INPUT
[[ $BATS_RUN_SKIPPED == "true" ]] || skip
run gawk -f etl.awk << END_INPUT
1: A
7:
7:
END_INPUT

assert_success
Expand All @@ -57,7 +57,7 @@ END_INPUT
run gawk -f etl.awk << END_INPUT
2:"D","G"
1: "E", "A"
END_INPUT

Expand All @@ -71,16 +71,16 @@ END_INPUT

@test 'multiple scores with differing numbers of letters' {
[[ $BATS_RUN_SKIPPED == "true" ]] || skip

# there are tab characters below
run gawk -f etl.awk << END_INPUT
1: "A", "E", "I", "O", "U", "L", "N", "R", "S", "T"
2: "D", "G"
4: "F", "H", "V", "W", "Y"
3: "m", "B", "C", "P"
5: "K"
8: "J", "X"
10: "Q", "Z"
1: "A", "E", "I", "O", "U", "L", "N", "R", "S", "T"
2: "D", "G"
4: "F", "H", "V", "W", "Y"
3: "m", "B", "C", "P"
5: "K"
8: "J", "X"
10: "Q", "Z"
END_INPUT

assert_success
Expand Down

0 comments on commit ec32680

Please sign in to comment.