Skip to content

Commit 14f2939

Browse files
committed
rna-transcription approach: a few improvements
- Renamed `chr` to `char` in the snippets because `chr` is a built-in function and although shadowing it in this case may not be a problem, it is still a bad practice when it can be avoided. - The dictionary-join approach mentions list comprehensions, but instead it uses a generator expression. Replaced this in the explanation and expanded to give the list comprehension based implementation along with a brief comparison. - The overview mentions one approach is four times faster. In a brief comparison, it varies from 2.5x for a very short string and up to 60x faster for a 10^6 long one. Probably not worth going into the details, but 4x is just innacurate.
1 parent a093eaa commit 14f2939

File tree

3 files changed

+23
-7
lines changed

3 files changed

+23
-7
lines changed

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

Lines changed: 20 additions & 4 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[chr] for chr in dna_strand)
8+
return ''.join(LOOKUP[char] for char in dna_strand)
99

1010
```
1111

@@ -16,15 +16,31 @@ but the `LOOKUP` dictionary is defined with all uppercase letters, which is the
1616
It indicates that the value is not intended to be changed.
1717

1818
In the `to_rna()` function, the [`join()`][join] method is called on an empty string,
19-
and is passed the list created from a [list comprehension][list-comprehension].
19+
and is passed the list created from a [generator expression][generator-expression].
2020

21-
The list comprehension iterates each character in the input,
21+
The generator expression iterates each character in the input,
2222
looks up the DNA character in the look-up dictionary, and outputs its matching RNA character as an element in the list.
2323

24-
The `join()` method collects the list of RNA characters back into a string.
24+
The `join()` method collects the RNA characters back into a string.
2525
Since an empty string is the separator for the `join()`, there are no spaces between the RNA characters in the string.
2626

27+
A generator expression is similar to a [list comprehension][list-comprehension], but instead of creating a list, it returns a generator, and iterating that generator yields the elements on the fly.
28+
29+
A variant that uses a list comprehension is almost identical, but note the additional square brackets inside the `join()`:
30+
31+
```python
32+
LOOKUP = {"G": "C", "C": "G", "T": "A", "A": "U"}
33+
34+
35+
def to_rna(dna_strand):
36+
return ''.join([LOOKUP[char] for char in dna_strand])
37+
```
38+
39+
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.
40+
2741
[dictionaries]: https://docs.python.org/3/tutorial/datastructures.html?#dictionaries
2842
[const]: https://realpython.com/python-constants/
2943
[join]: https://docs.python.org/3/library/stdtypes.html?#str.join
3044
[list-comprehension]: https://realpython.com/list-comprehension-python/#using-list-comprehensions
45+
[list-comprehension-choose-generator-expression]: https://realpython.com/list-comprehension-python/#choose-generators-for-large-datasets
46+
[generator-expression]: https://realpython.com/introduction-to-python-generators/#building-generators-with-generator-expressions

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[chr] for chr in dna_strand)
5+
return ''.join(LOOKUP[char] for char in dna_strand)

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

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

3131

3232
def to_rna(dna_strand):
33-
return ''.join(LOOKUP[chr] for chr in dna_strand)
33+
return ''.join(LOOKUP[char] for char 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 over four times faster than the dictionary look-up with `join()` approach.
41+
The `translate()` with `maketrans()` approach benchmarked many times faster than the dictionary look-up with `join()` approach.
4242

4343
[ASCII]: https://www.asciitable.com/
4444
[approach-translate-maketrans]: https://exercism.org/tracks/python/exercises/rna-transcription/approaches/translate-maketrans

0 commit comments

Comments
 (0)