PowerShell - hamming

This commit is contained in:
Xevion
2021-11-26 02:29:57 -06:00
parent c90524ae8e
commit 596aa4f029
7 changed files with 201 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
{
"blurb": "Calculate the Hamming difference between two DNA strands.",
"authors": [
"spuder"
],
"contributors": [
"cmccandless",
"daveyarwood",
"gyssels",
"kchenery",
"kytrinyx",
"sjwarner"
],
"files": {
"solution": [
"HammingDifference.ps1"
],
"test": [
"HammingDifference.tests.ps1"
],
"example": [
".meta/HammingDifference.example.ps1"
]
},
"source": "The Calculating Point Mutations problem at Rosalind",
"source_url": "http://rosalind.info/problems/hamm/"
}

View File

@@ -0,0 +1 @@
{"track":"powershell","exercise":"hamming","id":"e1d305d9542d4b7db55a294b3887ef4e","url":"https://exercism.org/tracks/powershell/exercises/hamming","handle":"Xevion","is_requester":true,"auto_approve":false}

View File

@@ -0,0 +1 @@
1

View File

@@ -0,0 +1,33 @@
# Help
## Running the tests
To run the tests run the command `Invoke-Pester` from within the exercise directory.
## Submitting your solution
You can submit your solution using the `exercism submit HammingDifference.ps1` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [PowerShell track's documentation](https://exercism.org/docs/tracks/powershell)
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
To get help if you are having trouble, you can use one of the following resources:
- [Powershell Documentation][powershell docs]
[Add more resources]: TODO
[powershell docs]: https://docs.microsoft.com/en-us/powershell/

View File

@@ -0,0 +1,10 @@
function Get-HammingDifference([string]$A, [string]$B) {
if ($A.Length -ne $B.Length) {
Throw("Left and right strands must be of equal length.")
}
[int] $hamming = 0
0..$A.Length | % { if ($A[$_] -ne $B[$_]) { $hamming++ } }
return $hamming
}

View File

@@ -0,0 +1,83 @@
BeforeAll {
$ScriptFile = ".\HammingDifference.ps1"
$CommandName = "Get-HammingDifference"
# Remove the function if its already found
If (Get-Command $CommandName -ErrorAction SilentlyContinue){
Write-Verbose "Removing the existing $CommandName function from memory as it is already loaded"
Remove-Item -Path "Function:\$CommandName"
}
# Load the script file
If (Test-Path "$ScriptFile"){
Write-Output ("Loading: {0}" -f "$ScriptFile")
. "$ScriptFile"
}
Else {
# Display an error and stop the tests
Write-Error "The file $ScriptFile was not found. You need to create your answer in a file named $ScriptFile" -ErrorAction Stop
}
}
Describe "Get-Hamming Test cases" {
It "Empty strands" {
Get-HammingDifference '' '' | Should -Be 0
}
It "Empty strands" {
Get-HammingDifference 'A' 'A' | Should -Be 0
}
It "Long identical strands" {
Get-HammingDifference "GGACTGA" "GGACTGA" | Should -Be 0
}
It "Complete distance in single nucleotide strands" {
Get-HammingDifference "A" "G" | Should -Be 1
}
It "Complete distance in small strands" {
Get-HammingDifference "AG" "CT" | Should -Be 2
}
It "Small distance in small strands" {
Get-HammingDifference "AT" "CT" | Should -Be 1
}
It "Small distance" {
Get-HammingDifference "GGACG" "GGTCG" | Should -Be 1
}
It "Small distance in long strands" {
Get-HammingDifference "ACCAGGG" "ACTATGG" | Should -Be 2
}
It "Non-unique character in first strand" {
Get-HammingDifference "AAG" "AAA" | Should -Be 1
}
It "Non-unique character in second strand" {
Get-HammingDifference "AAA" "AAG" | Should -Be 1
}
It "Same nucleotides in different positions" {
Get-HammingDifference "TAG" "GAT" | Should -Be 2
}
It "Large distance" {
Get-HammingDifference "GATACA" "GCATAA" | Should -Be 4
}
It "Large distance in off-by-one strand" {
Get-HammingDifference "GGACGGATTCTG" "AGGACGGATTCT" | Should -Be 9
}
It "Disallow first strand longer" {
{ Get-HammingDifference "AATG" "AAA" } | Should -Throw "Left and right strands must be of equal length."
}
It "Disallow second strand longer" {
{ Get-HammingDifference "ATA" "AGTG" } | Should -Throw "Left and right strands must be of equal length."
}
}

View File

@@ -0,0 +1,46 @@
# Hamming
Welcome to Hamming on Exercism's PowerShell Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
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 :)
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. The general handling of this situation (e.g., raising an
exception vs returning a special value) may differ between languages.
## Source
### Created by
- @spuder
### Contributed to by
- @cmccandless
- @daveyarwood
- @gyssels
- @kchenery
- @kytrinyx
- @sjwarner
### Based on
The Calculating Point Mutations problem at Rosalind - http://rosalind.info/problems/hamm/