I was so pleased with myself after finding a fun new solution, I almost didn’t notice it passes tests for the wrong reason. This is my iteration 3:
def hamming:
if length % 2 == 1 then "strands must be of equal length" | halt_error
elif . == "" then 0
else ([.[:1], .[-1:]] | index(last)) + (.[1:-1] | hamming)
end
;
.strand1 + (.strand2/"" | reverse | add) | hamming
It passes tests, because all the tests with wrong length have strands differing by only 1 character. If one had a strand 2,4,6,8… characters longer, it would fail. And I tested this way because I realized I can glue the strands together and recursively keep comparing the first and last character.
I don’t think a new test is needed. You’re not actually comparing the lengths of the strands. It’s more of a bug in the solution instead of a gap in the requirements.
I concur with glennj. If the jq track maintainers want a track-specific test for this, that’s up to them to decide, but I’m against adding such a case to the shared problem-specifications repo.
Well, the same algorithm would work in Python, and recursion limit of 1000 wouldn’t matter in this case ( hamming/0 benefits from tail call in jq). The error check in iteration 3 only detects strands that differ by odd number of characters.
We could add a test where the length differs by two … but you can then change your solution to check length % 3 != 0 and we’d need to check for a difference of 3. Then 5 and 7 and all the primes!
The tests aren’t meant to be exhaustive and prevent any and all bugs; the tests are meant to give people good guidance.