Skip to content

Commit

Permalink
Add nucleotide-count (#588)
Browse files Browse the repository at this point in the history
* Add `nucleotide-count`

* Break up long line

* Switch from errors to exceptions

* Instructions append

* Add info about Exception interface
  • Loading branch information
BNAndras authored Oct 10, 2024
1 parent 98bfa26 commit 3283134
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,14 @@
"math"
]
},
{
"slug": "nucleotide-count",
"name": "Nucleotide Count",
"uuid": "06ea660b-3869-46df-8fd3-a0651b66ed54",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "proverb",
"name": "Proverb",
Expand Down
14 changes: 14 additions & 0 deletions exercises/practice/nucleotide-count/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Instructions append

There are times when you need to handle unexpected issues that arise during code execution.
In Dart, you can use exceptions to handle these situations.

To throw an exception in Dart, use the `throw` keyword followed by an instance of the specific exception you want to raise.
For example, `throw Exception()` throws an instance of the base Exception type.

In this exercise, you need to define a custom exception called `InvalidNucleotideException`.
This exception should be raised when an invalid nucleotide is encountered.
The test suite only checks that an instance of `InvalidNucleotideException` is passed, so you have the freedom to implement it as you see fit as long as it is correctly thrown.

For more information on exceptions in Dart, you can refer to the [exceptions documentation](https://dart.dev/language/error-handling).
Custom exceptions can be created by [implementing the Exception interface](https://dart.dev/language#interfaces-and-abstract-classes).
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": [
"lib/nucleotide_count.dart"
],
"test": [
"test/nucleotide_count_test.dart"
],
"example": [
".meta/lib/example.dart"
]
},
"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/"
}
17 changes: 17 additions & 0 deletions exercises/practice/nucleotide-count/.meta/lib/example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class InvalidNucleotideException implements Exception {}

class NucleotideCount {
Map<String, int> count(String strand) {
final result = {"A": 0, "C": 0, "G": 0, "T": 0};

for (final nucleotide in strand.split('')) {
if (!result.containsKey(nucleotide)) {
throw InvalidNucleotideException();
}

result[nucleotide] = result[nucleotide]! + 1;
}

return result;
}
}
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"
18 changes: 18 additions & 0 deletions exercises/practice/nucleotide-count/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
analyzer:
strong-mode:
implicit-casts: false
implicit-dynamic: false
errors:
unused_element: error
unused_import: error
unused_local_variable: error
dead_code: error

linter:
rules:
# Error Rules
- avoid_relative_lib_imports
- avoid_types_as_parameter_names
- literal_only_boolean_expressions
- no_adjacent_strings_in_list
- valid_regexps
5 changes: 5 additions & 0 deletions exercises/practice/nucleotide-count/lib/nucleotide_count.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Implement a custom InvalidNucleotideException exception

class NucleotideCount {
// Put your code here
}
5 changes: 5 additions & 0 deletions exercises/practice/nucleotide-count/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: 'nucleotide_count'
environment:
sdk: '>=3.2.0 <4.0.0'
dev_dependencies:
test: '<2.0.0'
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import 'package:nucleotide_count/nucleotide_count.dart';
import 'package:test/test.dart';

void main() {
final counter = NucleotideCount();

group('NucleotideCount', () {
test('Empty strand', () {
final strand = "";
final result = counter.count(strand);
final expected = {"A": 0, "C": 0, "G": 0, "T": 0};
expect(result, equals(expected));
}, skip: false);

test('Can count one nucleotide in single-character input', () {
final strand = "G";
final result = counter.count(strand);
final expected = {"A": 0, "C": 0, "G": 1, "T": 0};
expect(result, equals(expected));
}, skip: true);

test('Strand with repeated nucleotide', () {
final strand = "GGGGGGG";
final result = counter.count(strand);
final expected = {"A": 0, "C": 0, "G": 7, "T": 0};
expect(result, equals(expected));
}, skip: true);

test('Strand with multiple nucleotides', () {
final strand = "AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC";
final result = counter.count(strand);
final expected = {"A": 20, "C": 12, "G": 17, "T": 21};
expect(result, equals(expected));
}, skip: true);

test('Handles invalid nucleotides', () {
final strand = "AGXXACT";
expect(() => counter.count(strand), throwsA(const TypeMatcher<InvalidNucleotideException>()));
}, skip: true);
});
}

0 comments on commit 3283134

Please sign in to comment.