Skip to content

Commit e91b43c

Browse files
petremBethanyG
authored andcommitted
several updates following discussions
- Replaced `char` with `nucleotide` as this is terminology from the domain. - Rephrased a "see also" link to be more screen reader friendly. - A note about the exercise not requiring tests for invalid characters is preset in one of the approaches. Copied it over to the other approach, for uniformity. - Rephrased mention about performance and speedup. - Replaced mention of ASCII with Unicode adding a brief explanation and links.
1 parent a809576 commit e91b43c

File tree

4 files changed

+16
-9
lines changed

4 files changed

+16
-9
lines changed

exercises/practice/rna-transcription/.approaches/dictionary-join/content.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ LOOKUP = {"G": "C", "C": "G", "T": "A", "A": "U"}
55

66

77
def to_rna(dna_strand):
8-
return ''.join(LOOKUP[char] for char in dna_strand)
8+
return ''.join(LOOKUP[nucleotide] for nucleotide in dna_strand)
99

1010
```
1111

@@ -33,7 +33,7 @@ LOOKUP = {"G": "C", "C": "G", "T": "A", "A": "U"}
3333

3434

3535
def to_rna(dna_strand):
36-
return ''.join([LOOKUP[char] for char in dna_strand])
36+
return ''.join([LOOKUP[nucleotide] for nucleotide in dna_strand])
3737
```
3838

3939
For a relatively small number of elements, using lists is fine and is usually even faster, but as the size of the data increases, the memory consumption increases and performance decreases. See also [this discussion][list-comprehension-choose-generator-expression] regarding when to choose one over the other.

exercises/practice/rna-transcription/.approaches/dictionary-join/snippet.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@ LOOKUP = {"G": "C", "C": "G", "T": "A", "A": "U"}
22

33

44
def to_rna(dna_strand):
5-
return ''.join(LOOKUP[char] for char in dna_strand)
5+
return ''.join(LOOKUP[nucleotide] for nucleotide in dna_strand)

exercises/practice/rna-transcription/.approaches/introduction.md

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ Another approach is to do a dictionary lookup on each character and join the res
77
## General guidance
88

99
Whichever approach is used needs to return the RNA complement for each DNA value.
10-
The `translate()` method with `maketrans()` transcribes using the [ASCII][ASCII] values of the characters.
10+
The `translate()` method with `maketrans()` transcribes using the [Unicode][Unicode] code points of the characters.
1111
Using a dictionary look-up with `join()` transcribes using the string values of the characters.
1212

1313
## Approach: `translate()` with `maketrans()`
@@ -30,16 +30,17 @@ LOOKUP = {"G": "C", "C": "G", "T": "A", "A": "U"}
3030

3131

3232
def to_rna(dna_strand):
33-
return ''.join(LOOKUP[char] for char in dna_strand)
33+
return ''.join(LOOKUP[nucleotide] for nucleotide in dna_strand)
3434

3535
```
3636

3737
For more information, check the [dictionary look-up with `join()` approach][approach-dictionary-join].
3838

3939
## Which approach to use?
4040

41-
The `translate()` with `maketrans()` approach benchmarked many times faster than the dictionary look-up with `join()` approach.
41+
If performance matters, consider using the [`translate()` with `maketrans()` approach][approach-translate-maketrans].
42+
How an implementation behaves in terms of performance may depend on the actual data being processed, on hardware, and other factors.
4243

43-
[ASCII]: https://www.asciitable.com/
44+
[Unicode]: https://en.wikipedia.org/wiki/Unicode
4445
[approach-translate-maketrans]: https://exercism.org/tracks/python/exercises/rna-transcription/approaches/translate-maketrans
4546
[approach-dictionary-join]: https://exercism.org/tracks/python/exercises/rna-transcription/approaches/dictionary-join

exercises/practice/rna-transcription/.approaches/translate-maketrans/content.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,12 @@ Python doesn't _enforce_ having real constant values,
1515
but the `LOOKUP` translation table is defined with all uppercase letters, which is the naming convention for a Python [constant][const].
1616
It indicates that the value is not intended to be changed.
1717

18-
The translation table that is created uses the [ASCII][ASCII] values (also called the ordinal values) for each letter in the two strings.
19-
The ASCII value for "G" in the first string is the key for the ASCII value of "C" in the second string, and so on.
18+
The translation table that is created uses the [Unicode][Unicode] _code points_ (sometimes called the ordinal values) for each letter in the two strings.
19+
As Unicode was designed to be backwards compatible with [ASCII][ASCII] and because the exercise uses Latin letters, the code points in the translation table can be interpreted as ASCII.
20+
However, the functions can deal with any Unicode character.
21+
You can learn more by reading about [strings and their representation in the Exercism Python syllabus][concept-string].
22+
23+
The Unicode value for "G" in the first string is the key for the Unicode value of "C" in the second string, and so on.
2024

2125
In the `to_rna()` function, the [`translate()`][translate] method is called on the input,
2226
and is passed the translation table.
@@ -32,3 +36,5 @@ As of this writing, no invalid DNA characters are in the argument to `to_rna()`,
3236
[const]: https://realpython.com/python-constants/
3337
[translate]: https://docs.python.org/3/library/stdtypes.html?#str.translate
3438
[ASCII]: https://www.asciitable.com/
39+
[Unicode]: https://en.wikipedia.org/wiki/Unicode
40+
[concept-strings]: https://exercism.org/tracks/python/concepts/strings

0 commit comments

Comments
 (0)