diff --git a/strings/dna.py b/strings/dna.py deleted file mode 100644 index 46e271d689db..000000000000 --- a/strings/dna.py +++ /dev/null @@ -1,26 +0,0 @@ -import re - - -def dna(dna: str) -> str: - - """ - https://en.wikipedia.org/wiki/DNA - Returns the second side of a DNA strand - - >>> dna("GCTA") - 'CGAT' - >>> dna("ATGC") - 'TACG' - >>> dna("CTGA") - 'GACT' - >>> dna("GFGG") - 'Invalid Strand' - """ - - r = len(re.findall("[ATCG]", dna)) != len(dna) - val = dna.translate(dna.maketrans("ATCG", "TAGC")) - return "Invalid Strand" if r else val - - -if __name__ == "__main__": - __import__("doctest").testmod() diff --git a/strings/dna_matching_strand.py b/strings/dna_matching_strand.py new file mode 100644 index 000000000000..6de07b202073 --- /dev/null +++ b/strings/dna_matching_strand.py @@ -0,0 +1,26 @@ +def dna_matching_strand(dna: str) -> str: + """ + https://en.wikipedia.org/wiki/DNA + Returns the second side of a DNA strand + + >>> dna_matching_strand("GCTA") + 'CGAT' + >>> dna_matching_strand("ATGC") + 'TACG' + >>> dna_matching_strand("CTGA") + 'GACT' + >>> dna_matching_strand("GFGG") + Traceback (most recent call last): + ... + ValueError: GFGG is not a valid strand of DNA + """ + dna = dna.upper() + if not all(c in "ATGC" for c in dna): + raise ValueError(f"{dna} is not a valid strand of DNA") + return dna.translate(str.maketrans("ATGC", "TACG")) + + +if __name__ == "__main__": + import doctest + + doctest.testmod()