PowerShell - nucleotide-count

This commit is contained in:
Xevion
2021-11-26 02:30:19 -06:00
parent 596aa4f029
commit b6f26a5279
7 changed files with 158 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
{
"blurb": "Given a DNA string, compute how many times each nucleotide occurs in the string.",
"authors": [
"gyssels"
],
"contributors": [
"kchenery"
],
"files": {
"solution": [
"NucleotideCount.ps1"
],
"test": [
"NucleotideCount.tests.ps1"
],
"example": [
".meta/NucleotideCount.example.ps1"
]
},
"source": "The Calculating DNA Nucleotides_problem at Rosalind",
"source_url": "http://rosalind.info/problems/dna/"
}

View File

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

View File

@@ -0,0 +1 @@
0

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 NucleotideCount.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,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'])"
}

View File

@@ -0,0 +1,25 @@
BeforeAll {
. ".\NucleotideCount.ps1"
}
Describe "NucleotideCountTests" {
It "empty strand" {
Get-NucleotideCount -Strand "" | Should -BeExactly "A:0 C:0 G:0 T:0"
}
It "can count one nucleotide in single-character input" {
Get-NucleotideCount -Strand "G" | Should -BeExactly "A:0 C:0 G:1 T:0"
}
It "strand with repeated nucleotide" {
Get-NucleotideCount -Strand "GGGGGGG" | Should -BeExactly "A:0 C:0 G:7 T:0"
}
It "strand with multiple nucleotides" {
Get-NucleotideCount -Strand "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC" | Should -BeExactly "A:20 C:12 G:17 T:21"
}
It "strand with invalid nucleotides" {
{ Get-NucleotideCount -Strand "AGXXACT" } | Should -Throw
}
}

View File

@@ -0,0 +1,40 @@
# Nucleotide Count
Welcome to Nucleotide Count on Exercism's PowerShell Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Each of us inherits from our biological parents a set of chemical instructions known as DNA that influence how our bodies are constructed. All known life depends on DNA!
> Note: You do not need to understand anything about nucleotides or DNA to complete this exercise.
DNA is a long chain of other chemicals and the most important are the four nucleotides, adenine, cytosine, guanine and thymine. A single DNA chain can contain billions of these four nucleotides and the order in which they occur is important!
We call the order of these nucleotides in a bit of DNA a "DNA sequence".
We represent a DNA sequence as an ordered collection of these four nucleotides and a common way to do that is with a string of characters such as "ATTACG" for a DNA sequence of 6 nucleotides.
'A' for adenine, 'C' for cytosine, 'G' for guanine, and 'T' for thymine.
Given a string representing a DNA sequence, count how many of each nucleotide is present.
If the string contains characters that aren't A, C, G, or T then it is invalid and you should signal an error.
For example:
```
"GATTACA" -> 'A': 3, 'C': 1, 'G': 1, 'T': 2
"INVALID" -> error
```
## Source
### Created by
- @gyssels
### Contributed to by
- @kchenery
### Based on
The Calculating DNA Nucleotides_problem at Rosalind - http://rosalind.info/problems/dna/