Skip to content

Commit

Permalink
Add nucleotide-count (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras authored Feb 13, 2024
1 parent 703bf83 commit 8d2008d
Show file tree
Hide file tree
Showing 8 changed files with 420 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,14 @@
],
"difficulty": 1
},
{
"slug": "nucleotide-count",
"name": "Nucleotide Count",
"uuid": "21826136-f2ec-4b5c-968b-e2ec2dfe9171",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "protein-translation",
"name": "Protein Translation",
Expand Down
23 changes: 23 additions & 0 deletions exercises/practice/nucleotide-count/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# 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:

```text
"GATTACA" -> 'A': 3, 'C': 1, 'G': 1, 'T': 2
"INVALID" -> error
```
19 changes: 19 additions & 0 deletions exercises/practice/nucleotide-count/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"nucleotide-count.red"
],
"test": [
"nucleotide-count-test.red"
],
"example": [
".meta/example.red"
]
},
"blurb": "Given a DNA string, compute how many times each nucleotide occurs in the string.",
"source": "The Calculating DNA Nucleotides_problem at Rosalind",
"source_url": "https://rosalind.info/problems/dna/"
}
24 changes: 24 additions & 0 deletions exercises/practice/nucleotide-count/.meta/example.red
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Red [
description: {"Nucleotide Count" exercise solution for exercism platform}
author: "BNAndras"
]

nucleotide-counts: function [
strand
] [
counts: make map! [A 0 C 0 G 0 T 0]

foreach char strand [
switch/default char [
#"A" [set 'counts/A 1 + get 'counts/A]
#"C" [set 'counts/C 1 + get 'counts/C]
#"G" [set 'counts/G 1 + get 'counts/G]
#"T" [set 'counts/T 1 + get 'counts/T]
] [
cause-error 'user 'message "Invalid nucleotide in strand"
]
]

counts
]

25 changes: 25 additions & 0 deletions exercises/practice/nucleotide-count/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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.

[3e5c30a8-87e2-4845-a815-a49671ade970]
description = "empty strand"

[a0ea42a6-06d9-4ac6-828c-7ccaccf98fec]
description = "can count one nucleotide in single-character input"

[eca0d565-ed8c-43e7-9033-6cefbf5115b5]
description = "strand with repeated nucleotide"

[40a45eac-c83f-4740-901a-20b22d15a39f]
description = "strand with multiple nucleotides"

[b4c47851-ee9e-4b0a-be70-a86e343bd851]
description = "strand with invalid nucleotides"
93 changes: 93 additions & 0 deletions exercises/practice/nucleotide-count/nucleotide-count-test.red
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
Red [
description: {Tests for "Nucleotide Count" Exercism exercise}
author: "loziniak"
]

#include %testlib.red

test-init/limit %nucleotide-count.red 1
; test-init/limit %.meta/example.red 1 ; test example solution

canonical-cases: [#(
description: "empty strand"
input: #(
strand: ""
)
expected: #(
A: 0
C: 0
G: 0
T: 0
)
function: "nucleotide-counts"
uuid: "3e5c30a8-87e2-4845-a815-a49671ade970"
) #(
description: {can count one nucleotide in single-character input}
input: #(
strand: "G"
)
expected: #(
A: 0
C: 0
G: 1
T: 0
)
function: "nucleotide-counts"
uuid: "a0ea42a6-06d9-4ac6-828c-7ccaccf98fec"
) #(
description: "strand with repeated nucleotide"
input: #(
strand: "GGGGGGG"
)
expected: #(
A: 0
C: 0
G: 7
T: 0
)
function: "nucleotide-counts"
uuid: "eca0d565-ed8c-43e7-9033-6cefbf5115b5"
) #(
description: "strand with multiple nucleotides"
input: #(
strand: {AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC}
)
expected: #(
A: 20
C: 12
G: 17
T: 21
)
function: "nucleotide-counts"
uuid: "40a45eac-c83f-4740-901a-20b22d15a39f"
) #(
description: "strand with invalid nucleotides"
input: #(
strand: "AGXXACT"
)
expected: #(
error: "Invalid nucleotide in strand"
)
function: "nucleotide-counts"
uuid: "b4c47851-ee9e-4b0a-be70-a86e343bd851"
)]


foreach c-case canonical-cases [
expect-code: compose [
(to word! c-case/function) (values-of c-case/input)
]
case-code: reduce
either all [
map? c-case/expected
string? c-case/expected/error
] [
['expect-error/message quote 'user expect-code c-case/expected/error]
] [
['expect c-case/expected expect-code]
]

test c-case/description case-code
]

test-results/print
11 changes: 11 additions & 0 deletions exercises/practice/nucleotide-count/nucleotide-count.red
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
Red [
description: {"Nucleotide Count" exercise solution for exercism platform}
author: "" ; you can write your name here, in quotes
]

nucleotide-counts: function [
strand
] [
cause-error 'user 'message "You need to implement nucleotide-counts function."
]

Loading

0 comments on commit 8d2008d

Please sign in to comment.