haskell: rna-transcription

This commit is contained in:
James Walker 2021-09-17 18:12:01 -04:00
parent 63750e5664
commit 4d0b3654be
Signed by: walkah
GPG Key ID: 3C127179D6086E93
7 changed files with 252 additions and 0 deletions

View File

@ -0,0 +1,88 @@
# Help
## Running the tests
To run the test suite, execute the following command:
```bash
stack test
```
#### If you get an error message like this...
```
No .cabal file found in directory
```
or
```
RedownloadInvalidResponse Request {
...
}
"/home/username/.stack/build-plan/lts-xx.yy.yaml" (Response {responseStatus = Status {statusCode = 404, statusMessage = "Not Found"},
```
You are probably running an old stack version and need
to upgrade it. Try running:
```bash
stack upgrade
```
Or see other options for upgrading at [Stack documentation](https://docs.haskellstack.org/en/stable/install_and_upgrade/#upgrade).
#### Otherwise, if you get an error message like this...
```
No compiler found, expected minor version match with...
Try running "stack setup" to install the correct GHC...
```
Just do as it says and it will download and install
the correct compiler version:
```bash
stack setup
```
If you want to play with your solution in GHCi, just run the command:
```bash
stack ghci
```
## Submitting your solution
You can submit your solution using the `exercism submit src/DNA.hs` 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 [Haskell track's documentation](https://exercism.org/docs/tracks/haskell)
- [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.
## Getting Started
Please refer to the [installation](https://exercism.io/tracks/haskell/installation)
and [learning](https://exercism.io/tracks/haskell/learning) help pages.
## Feedback, Issues, Pull Requests
The [exercism/haskell](https://github.com/exercism/haskell) repository on
GitHub is the home for all of the Haskell exercises.
If you have feedback about an exercise, or want to help implementing a new
one, head over there and create an issue. We'll do our best to help you!
{{ with .Spec.Credits }}

View File

@ -0,0 +1,51 @@
# Rna Transcription
Welcome to Rna Transcription on Exercism's Haskell Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
Given a DNA strand, return its RNA complement (per RNA transcription).
Both DNA and RNA strands are a sequence of nucleotides.
The four nucleotides found in DNA are adenine (**A**), cytosine (**C**),
guanine (**G**) and thymine (**T**).
The four nucleotides found in RNA are adenine (**A**), cytosine (**C**),
guanine (**G**) and uracil (**U**).
Given a DNA strand, its transcribed RNA strand is formed by replacing
each nucleotide with its complement:
* `G` -> `C`
* `C` -> `G`
* `T` -> `A`
* `A` -> `U`
Given invalid output, your program should return the first invalid character.
## Source
### Created by
- @etrepum
### Contributed to by
- @b-mehta
- @eijynagai
- @iHiD
- @kytrinyx
- @MazeChaZer
- @petertseng
- @ppartarr
- @rbasso
- @rpottsoh
- @samjonester
- @sshine
- @tejasbubane
### Based on
Hyperphysics - http://hyperphysics.phy-astr.gsu.edu/hbase/Organic/transcription.html

View File

@ -0,0 +1,18 @@
name: rna-transcription
dependencies:
- base
library:
exposed-modules: DNA
source-dirs: src
dependencies:
- containers
tests:
test:
main: Tests.hs
source-dirs: test
dependencies:
- rna-transcription
- hspec

View File

@ -0,0 +1,21 @@
name: rna-transcription
version: 1.3.0.10
dependencies:
- base
library:
exposed-modules: DNA
source-dirs: src
ghc-options: -Wall
# dependencies:
# - foo # List here the packages you
# - bar # want to use in your solution.
tests:
test:
main: Tests.hs
source-dirs: test
dependencies:
- rna-transcription
- hspec

View File

@ -0,0 +1,12 @@
module DNA (toRNA) where
toRNA :: String -> Either Char String
toRNA xs = traverse fromDNA xs
fromDNA :: Char -> Either Char Char
fromDNA c
| c == 'G' = Right 'C'
| c == 'C' = Right 'G'
| c == 'T' = Right 'A'
| c == 'A' = Right 'U'
| otherwise = Left c

View File

@ -0,0 +1 @@
resolver: lts-16.21

View File

@ -0,0 +1,61 @@
{-# LANGUAGE RecordWildCards #-}
import Data.Foldable (for_)
import Test.Hspec (Spec, describe, it, shouldBe)
import Test.Hspec.Runner (configFastFail, defaultConfig, hspecWith)
import DNA (toRNA)
main :: IO ()
main = hspecWith defaultConfig {configFastFail = True} specs
specs :: Spec
specs = describe "toRNA" $ for_ cases test
where
test Case{..} = it description $ toRNA dna `shouldBe` expected
data Case = Case { description :: String
, dna :: String
, expected :: Either Char String
}
cases :: [Case]
cases = [ Case { description = "Empty RNA sequence"
, dna = ""
, expected = Right ""
}
, Case { description = "RNA complement of cytosine is guanine"
, dna = "C"
, expected = Right "G"
}
, Case { description = "RNA complement of guanine is cytosine"
, dna = "G"
, expected = Right "C"
}
, Case { description = "RNA complement of thymine is adenine"
, dna = "T"
, expected = Right "A"
}
, Case { description = "RNA complement of adenine is uracil"
, dna = "A"
, expected = Right "U"
}
, Case { description = "RNA complement"
, dna = "ACGTGGTCTTAA"
, expected = Right "UGCACCAGAAUU"
}
, Case { description = "correctly handles invalid input (RNA instead of DNA)"
, dna = "U"
, expected = Left 'U'
}
, Case { description = "correctly handles completely invalid DNA input"
, dna = "XXX"
, expected = Left 'X'
}
, Case { description = "correctly handles partially invalid DNA input"
, dna = "ACGTXXXCTTAA"
, expected = Left 'X'
}
]
-- b02fedbb302892cb17dfa608df335b530ae2bc92