mirror of
https://github.com/Xevion/exercism.git
synced 2025-12-14 02:11:41 -06:00
PowerShell - nucleotide-count
This commit is contained in:
36
powershell/nucleotide-count/NucleotideCount.ps1
Normal file
36
powershell/nucleotide-count/NucleotideCount.ps1
Normal file
@@ -0,0 +1,36 @@
|
||||
Function Get-NucleotideCount() {
|
||||
<#
|
||||
.SYNOPSIS
|
||||
Given a single stranded DNA string, compute how many times each nucleotide occurs in the string.
|
||||
|
||||
.DESCRIPTION
|
||||
The genetic language of every living thing on the planet is DNA.
|
||||
DNA is a large molecule that is built from an extremely long sequence of individual elements called nucleotides.
|
||||
4 types exist in DNA and these differ only slightly and can be represented as the following symbols: 'A' for adenine, 'C' for cytosine, 'G' for guanine, and 'T' thymine.
|
||||
|
||||
The function counts the occurances of A, C, G and T in the supplied strand. It then outputs in the format:
|
||||
|
||||
A:0, C:0, G:0, T:0
|
||||
|
||||
.PARAMETER Strand
|
||||
The DNA strand to count
|
||||
|
||||
.EXAMPLE
|
||||
Get-NucleotideCount -Strand "ACGTAGCTT"
|
||||
|
||||
Retuns: A:2 C:2 G:2 T:3
|
||||
#>
|
||||
[CmdletBinding()]
|
||||
Param(
|
||||
[string]$Strand
|
||||
)
|
||||
|
||||
if ($Strand -notmatch "^[ACGT]*$") {
|
||||
Throw "Invalid DNA Strand"
|
||||
}
|
||||
|
||||
$counts = @{"A" = 0; "C" = 0; "G" = 0; "T" = 0}
|
||||
foreach ($char in $Strand.ToCharArray()) { $counts[$char.ToString()] += 1 }
|
||||
|
||||
return "A:$($counts['A']) C:$($counts['C']) G:$($counts['G']) T:$($counts['T'])"
|
||||
}
|
||||
Reference in New Issue
Block a user