From b6f26a52794cdc85d811fd68b844f5057b7e880d Mon Sep 17 00:00:00 2001 From: Xevion Date: Fri, 26 Nov 2021 02:30:19 -0600 Subject: [PATCH] PowerShell - nucleotide-count --- .../nucleotide-count/.exercism/config.json | 22 ++++++++++ .../nucleotide-count/.exercism/metadata.json | 1 + powershell/nucleotide-count/.version | 1 + powershell/nucleotide-count/HELP.md | 33 +++++++++++++++ .../nucleotide-count/NucleotideCount.ps1 | 36 +++++++++++++++++ .../NucleotideCount.tests.ps1 | 25 ++++++++++++ powershell/nucleotide-count/README.md | 40 +++++++++++++++++++ 7 files changed, 158 insertions(+) create mode 100644 powershell/nucleotide-count/.exercism/config.json create mode 100644 powershell/nucleotide-count/.exercism/metadata.json create mode 100644 powershell/nucleotide-count/.version create mode 100644 powershell/nucleotide-count/HELP.md create mode 100644 powershell/nucleotide-count/NucleotideCount.ps1 create mode 100644 powershell/nucleotide-count/NucleotideCount.tests.ps1 create mode 100644 powershell/nucleotide-count/README.md diff --git a/powershell/nucleotide-count/.exercism/config.json b/powershell/nucleotide-count/.exercism/config.json new file mode 100644 index 0000000..be89d7d --- /dev/null +++ b/powershell/nucleotide-count/.exercism/config.json @@ -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/" +} diff --git a/powershell/nucleotide-count/.exercism/metadata.json b/powershell/nucleotide-count/.exercism/metadata.json new file mode 100644 index 0000000..cf538aa --- /dev/null +++ b/powershell/nucleotide-count/.exercism/metadata.json @@ -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} \ No newline at end of file diff --git a/powershell/nucleotide-count/.version b/powershell/nucleotide-count/.version new file mode 100644 index 0000000..c227083 --- /dev/null +++ b/powershell/nucleotide-count/.version @@ -0,0 +1 @@ +0 \ No newline at end of file diff --git a/powershell/nucleotide-count/HELP.md b/powershell/nucleotide-count/HELP.md new file mode 100644 index 0000000..de9c2bf --- /dev/null +++ b/powershell/nucleotide-count/HELP.md @@ -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/ \ No newline at end of file diff --git a/powershell/nucleotide-count/NucleotideCount.ps1 b/powershell/nucleotide-count/NucleotideCount.ps1 new file mode 100644 index 0000000..51fd10d --- /dev/null +++ b/powershell/nucleotide-count/NucleotideCount.ps1 @@ -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'])" +} diff --git a/powershell/nucleotide-count/NucleotideCount.tests.ps1 b/powershell/nucleotide-count/NucleotideCount.tests.ps1 new file mode 100644 index 0000000..1942e88 --- /dev/null +++ b/powershell/nucleotide-count/NucleotideCount.tests.ps1 @@ -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 + } +} diff --git a/powershell/nucleotide-count/README.md b/powershell/nucleotide-count/README.md new file mode 100644 index 0000000..e5996d1 --- /dev/null +++ b/powershell/nucleotide-count/README.md @@ -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/ \ No newline at end of file