Skip to content

Commit

Permalink
feat: new exercise hamming added
Browse files Browse the repository at this point in the history
  • Loading branch information
GroophyLifefor committed Mar 7, 2024
1 parent cae4135 commit 0d8f63b
Show file tree
Hide file tree
Showing 6 changed files with 242 additions and 0 deletions.
27 changes: 27 additions & 0 deletions exercises/practice/hamming/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Description

Calculate the Hamming Distance between two DNA strands.

Your body is made up of cells that contain DNA.
Those cells regularly wear out and need replacing, which they achieve by dividing into daughter cells.
In fact, the average human body experiences about 10 quadrillion cell divisions in a lifetime!

When cells divide, their DNA replicates too.
Sometimes during this process mistakes happen and single pieces of DNA get encoded with the incorrect information.
If we compare two strands of DNA and count the differences between them we can see how many mistakes occurred.
This is known as the "Hamming Distance".

We read DNA using the letters C,A,G and T.
Two strands might look like this:

GAGCCTACTAACGGGAT
CATCGTAATGACGGCCT
^ ^ ^ ^ ^ ^^

They have 7 differences, and therefore the Hamming Distance is 7.

The Hamming Distance is useful for lots of things in science, not just biology, so it's a nice phrase to be familiar with :)

## Implementation notes

The Hamming distance is only defined for sequences of equal length, so an attempt to calculate it between sequences of different lengths should not work.
43 changes: 43 additions & 0 deletions exercises/practice/hamming/.meta/Example.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
@echo off
setlocal enabledelayedexpansion

call :getLength "%~1" len[1]
call :getLength "%~2" len[2]

if "%len[1]%" NEQ "%len[2]%" (
if "%len[1]%" EQU "0" (
echo left strand must not be empty
exit /b 1
)
if "%len[2]%" EQU "0" (
echo right strand must not be empty
exit /b 1
)
echo left and right strands must be of equal length
exit /b 1
)

set "string[1]=%~1"
set "string[2]=%~2"
set /a distance=0
for /L %%A in (0,1,%len[1]%) do (
if not "!string[1]:~%%A,1!"=="!string[2]:~%%A,1!" (
set /a distance+=1
)
)
echo !distance!

exit /b

:getLength
setlocal EnableDelayedExpansion
set "s=#%~1"
set "len=0"
for %%N in (4096 2048 1024 512 256 128 64 32 16 8 4 2 1) do (
if "!s:~%%N,1!" neq "" (
set /a "len+=%%N"
set "s=!s:~%%N!"
)
)
endlocal&if "%~2" neq "" (set %~2=%len%) else echo %len%
exit /b
17 changes: 17 additions & 0 deletions exercises/practice/hamming/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": ["sintrode", "GroophyLifefor"],
"files": {
"solution": [
"Hamming.bat"
],
"test": [
"HammingTest.bat"
],
"example": [
".meta/Example.bat"
]
},
"blurb": "Calculate the Hamming difference between two DNA strands.",
"source": "The Calculating Point Mutations problem at Rosalind",
"source_url": "https://rosalind.info/problems/hamm/"
}
13 changes: 13 additions & 0 deletions exercises/practice/hamming/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[af9ffe10-dc13-42d8-a742-e7bdafac449d]
description = "Say Hi!"
11 changes: 11 additions & 0 deletions exercises/practice/hamming/Hamming.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
@echo off
setlocal enabledelayedexpansion

set "row1=%~1"
set "row2=%~2"
set "result="

REM Your code goes here


echo %result%
131 changes: 131 additions & 0 deletions exercises/practice/hamming/HammingTest.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
@echo off
REM ---------------------------------------------------
REM Hamming Unit Testing
REM
REM sUnit Testing Framework version: 0.2
REM ---------------------------------------------------

:Main
REM Initalize result variable
set "slug=Hamming"

CALL :Initialize

REM --------------------
REM Test Case Start \/\/
REM Resource: https://github.com/exercism/problem-specifications/blob/main/exercises/hamming/canonical-data.json
REM --------------------
set "expected=0"
set "if_success=Test passed"
set "if_failed=Test failed: empty strands."
CALL :Assert "" ""

set "expected=0"
set "if_success=Test passed"
set "if_failed=Test failed: single letter identical strands."
CALL :Assert "A" "A"

set "expected=1"
set "if_success=Test passed"
set "if_failed=Test failed: single letter different strands."
CALL :Assert "G" "T"

set "expected=0"
set "if_success=Test passed"
set "if_failed=Test failed: long identical strands."
CALL :Assert "GGACTGAAATCTG" "GGACTGAAATCTG"

set "expected=9"
set "if_success=Test passed"
set "if_failed=Test failed: long different strands."
CALL :Assert "GGACGGATTCTG" "AGGACGGATTCT"

set "expected=left and right strands must be of equal length"
set "if_success=Test passed"
set "if_failed=Test failed: disallow first strand longer."
CALL :Assert "AATG" "AAA"

set "expected=left and right strands must be of equal length"
set "if_success=Test passed"
set "if_failed=Test failed: disallow second strand longer."
CALL :Assert "ATA" "AGTG"

set "expected=left strand must not be empty"
set "if_success=Test passed"
set "if_failed=Test failed: disallow left empty strand."
CALL :Assert "" "G"

set "expected=right strand must not be empty"
set "if_success=Test passed"
set "if_failed=Test failed: disallow right empty strand."
CALL :Assert "G" ""

REM --------------------
REM Test Case End /\/\/\
REM --------------------

CALL :ResolveStatus
exit /b %errorlevel%
REM End of Main

REM ---------------------------------------------------
REM Assert [..Parameters(up to 9)]
REM ---------------------------------------------------
GOTO :End REM Prevents the code below from being executed
:Assert
set "stdout="

REM Run the program and capture the output then delete the file
CALL %slug%.bat %1 %2 %3 %4 %5 %6 %7 %8 %9 > stdout.bin 2>&1
set /p stdout=<stdout.bin
del stdout.bin

REM Check if the result is correct
if "%stdout%" == "%expected%" (
if defined if_success (
echo %if_success%

REM Reset the variable to avoid duplicating the message.
set "if_success="
set "if_failed="
)

REM If the result is correct, exit with code 0
set /a successCount+=1
exit /b 0
) else (
if defined if_failed (
echo %if_failed%

REM Reset the variable to avoid duplicating the message.
set "if_success="
set "if_failed="
)

REM If the result is incorrect, exit with code 1
set /a failCount+=1
exit /b 1
)
GOTO :EOF REM Go back to the line after the call to :Assert

:Initialize
REM It's for initialize, not about checking empty file
set "successCount=0"
set "failCount=0"
GOTO :EOF REM Go back to the line after the call to :CheckEmptyFile

:ResolveStatus
set "status="
if %failCount% gtr 0 (
REM status: Fail
REM message: The test failed.
exit /b 1

) else (
REM status: Pass
exit /b 0

)
GOTO :EOF REM Go back to the line after the call to :ExportResultAsJson

:End

0 comments on commit 0d8f63b

Please sign in to comment.