Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add nucleotide-count #194

Merged
merged 3 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,14 @@
"difficulty": 1,
"topics": null
},
{
"slug": "nucleotide-count",
"name": "Nucleotide Count",
"uuid": "cd5fd765-2d79-4cb8-b1f0-a5891a149672",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "queen-attack",
"name": "Queen Attack",
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
```
6 changes: 6 additions & 0 deletions exercises/practice/nucleotide-count/.meta/Example.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* Here is an example solution for the NucleotideCount exercise
*/
component {

}
7 changes: 7 additions & 0 deletions exercises/practice/nucleotide-count/.meta/ExampleTest.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
component extends="NucleotideCountTest" {

function beforeAll(){
SUT = createObject( 'Solution' );
}

}
20 changes: 20 additions & 0 deletions exercises/practice/nucleotide-count/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"NucleotideCount.cfc"
],
"test": [
"NucleotideCountTest.cfc"
],
"example": [
".meta/Example.cfc",
".meta/ExampleTest.cfc"
]
},
"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/"
}
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"
32 changes: 32 additions & 0 deletions exercises/practice/nucleotide-count/NucleotideCount.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/**
* Your implementation of the NucleotideCount exercise
*/
component {

/**
* @returns
*/
function nucleotideCounts( strand ) {
a = 0
c = 0
g = 0
t = 0
cfloop(index="i" from="1" to="#len(strand)#") {
char = strand[i]
if (char == "a") {
a += 1
} else if (char == "c") {
c += 1
} else if (char == "g") {
g += 1
} else if (char == "t") {
t += 1
} else {
Throw(message='Invalid nucleotide in strand')
}
}

return {"A":a,"C":c,"G":g,"T":t}
BNAndras marked this conversation as resolved.
Show resolved Hide resolved
}

}
35 changes: 35 additions & 0 deletions exercises/practice/nucleotide-count/NucleotideCountTest.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
component extends="testbox.system.BaseSpec" {

function beforeAll(){
SUT = createObject( 'NucleotideCount' );
}

function run(){

describe( "My NucleotideCount class", function(){

it( 'empty strand', function(){
expect( SUT.nucleotideCounts( strand='' ) ).toBe( {"A":0,"C":0,"G":0,"T":0} );
});

it( 'can count one nucleotide in single-character input', function(){
expect( SUT.nucleotideCounts( strand='G' ) ).toBe( {"A":0,"C":0,"G":1,"T":0} );
});

it( 'strand with repeated nucleotide', function(){
expect( SUT.nucleotideCounts( strand='GGGGGGG' ) ).toBe( {"A":0,"C":0,"G":7,"T":0} );
});

it( 'strand with multiple nucleotides', function(){
expect( SUT.nucleotideCounts( strand='AGCTTTTCATTCTGACTGCAACGGGCAATATGTCTCTGTGTGGATTAAAAAAAGAGTGTCTGATAGCAGC' ) ).toBe( {"A":20,"C":12,"G":17,"T":21} );
});

it( 'strand with invalid nucleotides', function(){
expect( function(){ SUT.nucleotideCounts( strand='AGXXACT' ); } ).toThrow( message='Invalid nucleotide in strand' );
});

});

}

}
103 changes: 103 additions & 0 deletions exercises/practice/nucleotide-count/TestRunner.cfc
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/**
* I am a CommandBox task runner which you can use to test your implementation of this exercise against the
* provided test suite. To use me, open the CommandBox CLI and run this:
*
* CommandBox> task run TestRunner
*
* To start up a test watcher that will automatically rerun the test suite every time you save a file change, run this:
*
* CommandBox> task run TestRunner --watcher
*
*/
component {

/**
* @solution Runs the tests against the solution
* @watcher Start up a file watch that re-runs the tests on file changes. Use Ctrl-C to stop
*/
function run( boolean solution=false, boolean watcher=false ) {

ensureTestBox();

if( watcher ) {

// Tabula rasa
command( 'cls' ).run();

// Start watcher
watch()
.paths( '*.cfc' )
.inDirectory( getCWD() )
.withDelay( 500 )
.onChange( function() {

// Clear the screen
command( 'cls' )
.run();

// This is neccessary so changes to tests get picked up right away.
pagePoolClear();

runTests( solution );

} )
.start();

} else {
runTests( solution );
}

}

/**
* Make sure the TestBox framework is installed
*/
private function ensureTestBox() {
var excerciseRoot = getCWD();
var testBoxRoot = excerciseRoot & '/testbox';

if( !directoryExists( testBoxRoot ) ) {

print.boldYellowLine( 'Installing some missing dependencies for you!' ).toConsole();
command( 'install' )
.inWorkingDirectory( excerciseRoot )
.run();
}

// Bootstrap TestBox framework
filesystemUtil.createMapping( '/testbox', testBoxRoot );
}

/**
* Invoke TestBox to run the test suite
*/
private function runTests( boolean solution=false ) {

// Create TestBox and run the tests
testData = new testbox.system.TestBox()
.runRaw( directory = {
// Find all CFCs...
mapping = filesystemUtil.makePathRelative( getCWD() ),
// ... in this directory ...
recurse = false,
// ... whose name ends in "test"
filter = function( path ) {
return path.reFind( ( solution ? 'Solution' : '' ) & 'Test.cfc$' );
}
} )
.getMemento();

// Print out the results with ANSI formatting for the CLI
getInstance( 'CLIRenderer@testbox-commands' )
.render( print, testData, true );

print.toConsole();

// Set proper exit code
if( testData.totalFail || testData.totalError ) {
setExitCode( 1 );
}
}

}

8 changes: 8 additions & 0 deletions exercises/practice/nucleotide-count/box.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"dependencies":{
"testbox":"^2.5.0+107"
},
"installPaths":{
"testbox":"testbox/"
}
}
37 changes: 37 additions & 0 deletions exercises/practice/nucleotide-count/index.cfm
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!---

This file will only be used if you want to start up a web server in this directory. You can do so by running:

$> box
CommandBox> install
CommandBox> server start

However, this is not necessary unless you really just want to use the HTML reporters on TestBox.
Ideally, you'll skip the need for this file entirely and just run the tests directly frm the CLI like this:

CommandBox> task run TestRunner

--->
<cfsetting showDebugOutput="false">
<cfparam name="url.reporter" default="simple">
<cfscript>
// get a list of all CFCs in this folder whose name looks like "XXXTest.cfc"
// And turn it into compnent path relative to the web root
url.bundles = directoryList(
path=expandPath( '/' ),
filter='*Test.cfc' )
.map( function( path ) {
return path
.replaceNoCase( expandPath( '/' ), '' )
.left( -4 )
} )
.toList();
</cfscript>

<!--- Ensure TestBox --->
<cfif fileExists( "/testbox/system/runners/HTMLRunner.cfm" )>
<!--- Include the TestBox HTML Runner --->
<cfinclude template="/testbox/system/runners/HTMLRunner.cfm">
<cfelse>
Oops, you don't have TestBox installed yet! Please run <b>box install</b> from the root of your excercise folder and refresh this page.
</cfif>