Skip to content

Commit

Permalink
applied code review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
ErwinOlie2 committed Oct 31, 2023
1 parent 4cfa9f6 commit b429b81
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,35 @@ class Dna(val sequence: String) {
```

## Verification
We receive a sequence as input, which we have to verificate.
We receive a sequence as input, which we have to verify.
Because the sequence is part of the primary constructor, which can't contain runnable code, we have to place the check in an [_initializer block_][stdlib-constructors].
In Kotlin it's idiomatic to use the [`require`][stdlib-require] keyword for verification.
`require` will throw an `IllegalArgumentException` if the given value is `false`.
```kotlin
init {
require(sequence.all { it in "ACGT" })
}
```

## Base Map
We define a base map with a zero value for every nucleotide.
We do this to ensure that every nucleotide will be present in the output, even if there is no count.
Afterward we can overwrite some of the values by using the `+` operator.
Note that the [`+` operator on maps][map-plus] does not add the values but overwrites them.
```kotlin
val nucleotideCounts
get() = mapOf('A' to 0, 'C' to 0, 'G' to 0, 'T' to 0)
```

## Counting Nucleotides
We use the `.groupingBy` function to create a [`Grouping`][stdlib-grouping] of the nucleotides.
By using the `.eachCount()` function we turn the [`Grouping`][stdlib-grouping] in a Map.
For example: `"ACA".groupingBy { it }.eachCount()` results in `{A=2, C=1}`.
After [adding][map-plus] the maps together we receive our final answer.

```kotlin
val nucleotideCounts
get() = mapOf('A' to 0, 'C' to 0, 'G' to 0, 'T' to 0) + sequence.groupingBy { it }.eachCount()
```

[stdlib-require]: https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/require.html
[stdlib-constructors]: https://kotlinlang.org/docs/classes.html#constructors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ Each solution has to contain the following three elements:
One approach is to use `groupBy` and the `+` operator:
```kotlin
class Dna(val sequence: String) {

init {
require(sequence.all { it in "ACGT" })
}
Expand Down

0 comments on commit b429b81

Please sign in to comment.