From 3f819deb867b42227bfd0abc1eb983c01ecbd5a3 Mon Sep 17 00:00:00 2001 From: Or Golan <golanor@gmail.com> Date: Thu, 25 Jan 2024 21:10:15 +0200 Subject: [PATCH 01/12] Added protein translation practice exercise --- config.json | 119 +++++++++--------- .../protein-translation/.docs/instructions.md | 45 +++++++ .../protein-translation/.meta/config.json | 16 +++ .../protein-translation/.meta/example.jl | 42 +++++++ .../protein-translation/.meta/tests.toml | 100 +++++++++++++++ .../protein-translation.jl | 3 + .../practice/protein-translation/runtests.jl | 84 +++++++++++++ 7 files changed, 353 insertions(+), 56 deletions(-) create mode 100644 exercises/practice/protein-translation/.docs/instructions.md create mode 100644 exercises/practice/protein-translation/.meta/config.json create mode 100644 exercises/practice/protein-translation/.meta/example.jl create mode 100644 exercises/practice/protein-translation/.meta/tests.toml create mode 100644 exercises/practice/protein-translation/protein-translation.jl create mode 100644 exercises/practice/protein-translation/runtests.jl diff --git a/config.json b/config.json index fdf85667..c39ec367 100644 --- a/config.json +++ b/config.json @@ -32,29 +32,12 @@ ".meta/exemplar.jl" ] }, - "concepts": [ - { - "uuid": "32384ed4-ef26-4118-8ea3-4d44c331e828", - "slug": "functions", - "name": "Functions" - }, - { - "slug": "integer-introduction", - "name": "Integers and Arithmetic Operations", - "uuid": "006ebce8-87cd-4695-87e6-8a7b8dc2f239" - }, - { - "slug": "comments", - "name": "Comments", - "uuid": "90365822-f58c-4e45-88be-430978b77e65" - } - ], "exercises": { "concept": [ { - "uuid": "39ebdd04-4f84-4817-bf9d-f1f9e066c283", - "name": "Lasagna", "slug": "lasagna", + "name": "Lasagna", + "uuid": "39ebdd04-4f84-4817-bf9d-f1f9e066c283", "concepts": [ "functions", "integer-introduction", @@ -82,12 +65,12 @@ "uuid": "e0142403-af9e-406e-827a-1fd37187d1ea", "practices": [], "prerequisites": [], + "difficulty": 5, "topics": [ "iterator-protocol", "vectors", "conditionals" - ], - "difficulty": 5 + ] }, { "slug": "leap", @@ -297,11 +280,11 @@ ] }, { + "slug": "reverse-string", + "name": "Reverse String", "uuid": "e73f19f7-17bc-4d51-a00a-c2b58c1fa8e6", "practices": [], "prerequisites": [], - "slug": "reverse-string", - "name": "Reverse String", "difficulty": 3, "topics": [ "generics", @@ -317,11 +300,11 @@ ] }, { + "slug": "allergies", + "name": "Allergies", "uuid": "4eced190-062f-4a7b-8c92-87ff02f1e343", "practices": [], "prerequisites": [], - "slug": "allergies", - "name": "Allergies", "difficulty": 1, "topics": [ "loops", @@ -554,12 +537,13 @@ ] }, { + "slug": "dnd-character", + "name": "D&D Character", "uuid": "fcb9ef0e-ed97-4420-a38f-9bf690c23a3b", "practices": [], "prerequisites": [], - "slug": "dnd-character", - "name": "D&D Character", "difficulty": 5, + "status": "deprecated", "topics": [ "variables", "integers", @@ -568,8 +552,7 @@ "randomness", "math", "test_driven_development" - ], - "status": "deprecated" + ] }, { "slug": "robot-simulator", @@ -583,11 +566,11 @@ ] }, { + "slug": "circular-buffer", + "name": "Circular Buffer", "uuid": "3dc9bbde-d344-4a36-a7e1-433637974dfc", "practices": [], "prerequisites": [], - "slug": "circular-buffer", - "name": "Circular Buffer", "difficulty": 8, "topics": [ "data_structures", @@ -617,8 +600,7 @@ "difficulty": 1, "topics": [ "math", - "integers", - "math" + "integers" ] }, { @@ -687,14 +669,14 @@ "practices": [], "prerequisites": [], "difficulty": 2, + "status": "deprecated", "topics": [ "broadcast", "conditionals", "variables", "integers", "arrays" - ], - "status": "deprecated" + ] }, { "slug": "bob", @@ -730,20 +712,20 @@ "practices": [], "prerequisites": [], "difficulty": 2, + "status": "deprecated", "topics": [ "arrays", "conditionals", "loops", "strings" - ], - "status": "deprecated" + ] }, { + "slug": "perfect-numbers", + "name": "Perfect Numbers", "uuid": "4fc89443-1c5c-492a-850f-7990869caae6", "practices": [], "prerequisites": [], - "slug": "perfect-numbers", - "name": "Perfect Numbers", "difficulty": 3, "topics": [ "loops", @@ -770,11 +752,11 @@ ] }, { + "slug": "all-your-base", + "name": "All Your Base", "uuid": "6fa91607-d772-47af-9520-de26bbb8addb", "practices": [], "prerequisites": [], - "slug": "all-your-base", - "name": "All Your Base", "difficulty": 4, "topics": [ "integers", @@ -782,11 +764,11 @@ ] }, { + "slug": "matching-brackets", + "name": "Matching Brackets", "uuid": "bf5c1f49-963a-4397-bb67-b58e1b7d45e3", "practices": [], "prerequisites": [], - "slug": "matching-brackets", - "name": "Matching Brackets", "difficulty": 5, "topics": [ "loops", @@ -801,11 +783,11 @@ ] }, { + "slug": "pythagorean-triplet", + "name": "Pythagorean Triplet", "uuid": "a534b8ed-da88-4945-bb25-f6633977740b", "practices": [], "prerequisites": [], - "slug": "pythagorean-triplet", - "name": "Pythagorean Triplet", "difficulty": 7, "topics": [ "conditionals", @@ -823,19 +805,19 @@ ] }, { + "slug": "alphametics", + "name": "Alphametics", "uuid": "32af807f-ba90-4a36-bf6b-5d71a0fae7ae", "practices": [], "prerequisites": [], - "slug": "alphametics", - "name": "Alphametics", "difficulty": 10 }, { + "slug": "largest-series-product", + "name": "Largest Series Product", "uuid": "4ce25733-55ab-48fd-84e2-c4b7f2f3f529", "practices": [], "prerequisites": [], - "slug": "largest-series-product", - "name": "Largest Series Product", "difficulty": 4, "topics": [ "conditionals", @@ -850,11 +832,11 @@ ] }, { + "slug": "saddle-points", + "name": "Saddle Points", "uuid": "f26bf680-b8fb-40dd-a82c-884ae391b8b6", "practices": [], "prerequisites": [], - "slug": "saddle-points", - "name": "Saddle Points", "difficulty": 4, "topics": [ "conditionals", @@ -869,11 +851,11 @@ ] }, { + "slug": "square-root", + "name": "Square Root", "uuid": "587ad0fa-ea7e-4962-9c71-3a50d2877e6b", "practices": [], "prerequisites": [], - "slug": "square-root", - "name": "Square Root", "difficulty": 2, "topics": [ "algorithms", @@ -883,9 +865,34 @@ "conditionals", "variables" ] + }, + { + "slug": "protein-translation", + "name": "Protein Translation", + "uuid": "98b2f717-2630-4f28-a5f2-e51047b59a56", + "practices": [], + "prerequisites": [], + "difficulty": 1 } ] }, + "concepts": [ + { + "uuid": "32384ed4-ef26-4118-8ea3-4d44c331e828", + "slug": "functions", + "name": "Functions" + }, + { + "uuid": "006ebce8-87cd-4695-87e6-8a7b8dc2f239", + "slug": "integer-introduction", + "name": "Integers and Arithmetic Operations" + }, + { + "uuid": "90365822-f58c-4e45-88be-430978b77e65", + "slug": "comments", + "name": "Comments" + } + ], "key_features": [ { "title": "Fast", @@ -920,15 +927,15 @@ ], "tags": [ "execution_mode/compiled", - "typing/dynamic", "paradigm/declarative", "paradigm/functional", "paradigm/imperative", "paradigm/object_oriented", "paradigm/procedural", - "platform/windows", - "platform/mac", "platform/linux", + "platform/mac", + "platform/windows", + "typing/dynamic", "used_for/artificial_intelligence", "used_for/cross_platform_development", "used_for/financial_systems", diff --git a/exercises/practice/protein-translation/.docs/instructions.md b/exercises/practice/protein-translation/.docs/instructions.md new file mode 100644 index 00000000..7dc34d2e --- /dev/null +++ b/exercises/practice/protein-translation/.docs/instructions.md @@ -0,0 +1,45 @@ +# Instructions + +Translate RNA sequences into proteins. + +RNA can be broken into three nucleotide sequences called codons, and then translated to a polypeptide like so: + +RNA: `"AUGUUUUCU"` => translates to + +Codons: `"AUG", "UUU", "UCU"` +=> which become a polypeptide with the following sequence => + +Protein: `"Methionine", "Phenylalanine", "Serine"` + +There are 64 codons which in turn correspond to 20 amino acids; however, all of the codon sequences and resulting amino acids are not important in this exercise. +If it works for one codon, the program should work for all of them. +However, feel free to expand the list in the test suite to include them all. + +There are also three terminating codons (also known as 'STOP' codons); if any of these codons are encountered (by the ribosome), all translation ends and the protein is terminated. + +All subsequent codons after are ignored, like this: + +RNA: `"AUGUUUUCUUAAAUG"` => + +Codons: `"AUG", "UUU", "UCU", "UAA", "AUG"` => + +Protein: `"Methionine", "Phenylalanine", "Serine"` + +Note the stop codon `"UAA"` terminates the translation and the final methionine is not translated into the protein sequence. + +Below are the codons and resulting Amino Acids needed for the exercise. + +| Codon | Protein | +| :----------------- | :------------ | +| AUG | Methionine | +| UUU, UUC | Phenylalanine | +| UUA, UUG | Leucine | +| UCU, UCC, UCA, UCG | Serine | +| UAU, UAC | Tyrosine | +| UGU, UGC | Cysteine | +| UGG | Tryptophan | +| UAA, UAG, UGA | STOP | + +Learn more about [protein translation on Wikipedia][protein-translation]. + +[protein-translation]: https://en.wikipedia.org/wiki/Translation_(biology) diff --git a/exercises/practice/protein-translation/.meta/config.json b/exercises/practice/protein-translation/.meta/config.json new file mode 100644 index 00000000..782b7044 --- /dev/null +++ b/exercises/practice/protein-translation/.meta/config.json @@ -0,0 +1,16 @@ +{ + "authors": [], + "files": { + "solution": [ + "protein-translation.jl" + ], + "test": [ + "runtests.jl" + ], + "example": [ + ".meta/example.jl" + ] + }, + "blurb": "Translate RNA sequences into proteins.", + "source": "Tyler Long" +} diff --git a/exercises/practice/protein-translation/.meta/example.jl b/exercises/practice/protein-translation/.meta/example.jl new file mode 100644 index 00000000..21d6a1d9 --- /dev/null +++ b/exercises/practice/protein-translation/.meta/example.jl @@ -0,0 +1,42 @@ +codon_protein_dict = Dict( + "AUG" => "Methionine", + "UUU" => "Phenylalanine", + "UUC" => "Phenylalanine", + "UUA" => "Leucine", + "UUG" => "Leucine", + "UCU" => "Serine", + "UCC" => "Serine", + "UCA" => "Serine", + "UCG" => "Serine", + "UAU" => "Tyrosine", + "UAC" => "Tyrosine", + "UGU" => "Cysteine", + "UGC" => "Cysteine", + "UGG" => "Tryptophan", + "UAA" => "STOP", + "UAG" => "STOP", + "UGA" => "STOP" +) + +struct TranslationError <: Exception + message::String +end + +macro rna_str(str) + n = 3 + result = [] + for i=1:n:length(str) + substring = SubString(str, i, i+n-1) + protein = string_to_protein(substring) + protein == "STOP" && break + push!(result, protein) + end + return result +end + + +function string_to_protein(str) + p = get(codon_protein_dict, str, nothing) + p === nothing && throw(TranslationError("invalid codon")) + return p +end \ No newline at end of file diff --git a/exercises/practice/protein-translation/.meta/tests.toml b/exercises/practice/protein-translation/.meta/tests.toml new file mode 100644 index 00000000..5fb18907 --- /dev/null +++ b/exercises/practice/protein-translation/.meta/tests.toml @@ -0,0 +1,100 @@ +# This is an auto-generated file. +# +# Regenerating this file via `configlet sync` will: +# - Recreate every `description` key/value pair +# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications +# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) +# - Preserve any other key/value pair +# +# As user-added comments (using the # character) will be removed when this file +# is regenerated, comments can be added via a `comment` key. + +[2c44f7bf-ba20-43f7-a3bf-f2219c0c3f98] +description = "Empty RNA sequence results in no proteins" + +[96d3d44f-34a2-4db4-84cd-fff523e069be] +description = "Methionine RNA sequence" + +[1b4c56d8-d69f-44eb-be0e-7b17546143d9] +description = "Phenylalanine RNA sequence 1" + +[81b53646-bd57-4732-b2cb-6b1880e36d11] +description = "Phenylalanine RNA sequence 2" + +[42f69d4f-19d2-4d2c-a8b0-f0ae9ee1b6b4] +description = "Leucine RNA sequence 1" + +[ac5edadd-08ed-40a3-b2b9-d82bb50424c4] +description = "Leucine RNA sequence 2" + +[8bc36e22-f984-44c3-9f6b-ee5d4e73f120] +description = "Serine RNA sequence 1" + +[5c3fa5da-4268-44e5-9f4b-f016ccf90131] +description = "Serine RNA sequence 2" + +[00579891-b594-42b4-96dc-7ff8bf519606] +description = "Serine RNA sequence 3" + +[08c61c3b-fa34-4950-8c4a-133945570ef6] +description = "Serine RNA sequence 4" + +[54e1e7d8-63c0-456d-91d2-062c72f8eef5] +description = "Tyrosine RNA sequence 1" + +[47bcfba2-9d72-46ad-bbce-22f7666b7eb1] +description = "Tyrosine RNA sequence 2" + +[3a691829-fe72-43a7-8c8e-1bd083163f72] +description = "Cysteine RNA sequence 1" + +[1b6f8a26-ca2f-43b8-8262-3ee446021767] +description = "Cysteine RNA sequence 2" + +[1e91c1eb-02c0-48a0-9e35-168ad0cb5f39] +description = "Tryptophan RNA sequence" + +[e547af0b-aeab-49c7-9f13-801773a73557] +description = "STOP codon RNA sequence 1" + +[67640947-ff02-4f23-a2ef-816f8a2ba72e] +description = "STOP codon RNA sequence 2" + +[9c2ad527-ebc9-4ace-808b-2b6447cb54cb] +description = "STOP codon RNA sequence 3" + +[f4d9d8ee-00a8-47bf-a1e3-1641d4428e54] +description = "Sequence of two protein codons translates into proteins" + +[dd22eef3-b4f1-4ad6-bb0b-27093c090a9d] +description = "Sequence of two different protein codons translates into proteins" + +[d0f295df-fb70-425c-946c-ec2ec185388e] +description = "Translate RNA strand into correct protein list" + +[e30e8505-97ec-4e5f-a73e-5726a1faa1f4] +description = "Translation stops if STOP codon at beginning of sequence" + +[5358a20b-6f4c-4893-bce4-f929001710f3] +description = "Translation stops if STOP codon at end of two-codon sequence" + +[ba16703a-1a55-482f-bb07-b21eef5093a3] +description = "Translation stops if STOP codon at end of three-codon sequence" + +[4089bb5a-d5b4-4e71-b79e-b8d1f14a2911] +description = "Translation stops if STOP codon in middle of three-codon sequence" + +[2c2a2a60-401f-4a80-b977-e0715b23b93d] +description = "Translation stops if STOP codon in middle of six-codon sequence" + +[1e75ea2a-f907-4994-ae5c-118632a1cb0f] +description = "Non-existing codon can't translate" + +[9eac93f3-627a-4c90-8653-6d0a0595bc6f] +description = "Unknown amino acids, not part of a codon, can't translate" + +[9d73899f-e68e-4291-b1e2-7bf87c00f024] +description = "Incomplete RNA sequence can't translate" + +[43945cf7-9968-402d-ab9f-b8a28750b050] +description = "Incomplete RNA sequence can translate if valid until a STOP codon" diff --git a/exercises/practice/protein-translation/protein-translation.jl b/exercises/practice/protein-translation/protein-translation.jl new file mode 100644 index 00000000..77b09a3d --- /dev/null +++ b/exercises/practice/protein-translation/protein-translation.jl @@ -0,0 +1,3 @@ +macro rna_str() + # I'm a ribosome macro! +end \ No newline at end of file diff --git a/exercises/practice/protein-translation/runtests.jl b/exercises/practice/protein-translation/runtests.jl new file mode 100644 index 00000000..4a2ded7e --- /dev/null +++ b/exercises/practice/protein-translation/runtests.jl @@ -0,0 +1,84 @@ +using Test + +include("protein-translation.jl") + +@testset "Protein Translation" begin + + @test "Empty RNA sequence returns an empty list" begin + @test rna"" == [] + end + + @test "Methionine RNA sequence is decoded as Methionine" begin + @test rna"AUG" == ["Methionine"] + end + + @test "Phenylalanine RNA sequence is decoded as Phenylalanine" begin + @test rna"UUU" == ["Phenylalanine"] + end + + @test "Other Phenylalanine RNA sequence is decoded as Phenylalanine" begin + @test rna"UUC" == ["Phenylalanine"] + end + + @test "Leucine RNA sequence is decoded as Leucine" begin + @test rna"UUA" == ["Leucine"] + end + + @test "Leucine RNA sequence is decoded as Leucine" begin + @test rna"UUG" == ["Leucine"] + end + + @test "Serine RNA sequence is decoded as Serine" begin + @test rna"UCUUCCUCAUCG" == ["Serine", "Serine", "Serine", "Serine"] + end + + @test "Tyrosine RNA sequence is decoded as Tyrosine" begin + @test rna"UAUUAC" == ["Tyrosine", "Tyrosine"] + end + + @test "Cysteine RNA sequence is decoded as Cysteine" begin + @test rna"UGUUGC" == ["Cysteine", "Cysteine"] + end + + @test "Tryptophan RNA sequence is decoded as Tryptophan" begin + @test rna"UGG" == ["Tryptophan"] + end + + @test "STOP codon terminates translation" begin + @test rna"UAA" == [] + @test rna"UAG" == [] + @test rna"UGA" == [] + end + + @test "Sequence of two codons translates into proteins" begin + @test rna"UUUUUU" == ["Phenylalanine", "Phenylalanine"] + end + + @test "Sequence of two different codons translates into proteins" begin + @test rna"UUAUUG" == ["Leucine", "Phenylalanine"] + end + + @test "Translation stops if STOP codon appears in middle of sequence" begin + @test rna"UGGUAG" == ["Tryptophan"] + end + + @test "Translation stops if STOP codon appears at beginning of sequence" begin + @test rna"UAGUUUUGG" == [] + end + + @test "Translation stops if STOP codon appears at end of two-codon sequence" begin + @test rna"UGGUGUUAUUAAUGGUUU" == ["Tryptophan", "Cysteine", "Tyrosine"] + end + + @test "Non existent codon causes translation exception" begin + @test_throws TranslationError rna"AAA" + end + + @test "Incomplete codon causes translation exception" begin + @test_throws TranslationError rna"UGGU" + end + + @test "Incomplete RNA sequence can translate if given a stop codon" begin + @test rna"UGGUAGUAAAA" == ["Tryptophan"] + end +end \ No newline at end of file From a06f43892a9832cb0ef495ab7fc87afa7adde513 Mon Sep 17 00:00:00 2001 From: Or Golan <golanor@gmail.com> Date: Tue, 30 Jan 2024 10:21:13 +0200 Subject: [PATCH 02/12] fixed tests --- .../practice/protein-translation/runtests.jl | 38 +++++++++---------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/exercises/practice/protein-translation/runtests.jl b/exercises/practice/protein-translation/runtests.jl index 4a2ded7e..e066ec86 100644 --- a/exercises/practice/protein-translation/runtests.jl +++ b/exercises/practice/protein-translation/runtests.jl @@ -4,81 +4,81 @@ include("protein-translation.jl") @testset "Protein Translation" begin - @test "Empty RNA sequence returns an empty list" begin + @testset "Empty RNA sequence returns an empty list" begin @test rna"" == [] end - @test "Methionine RNA sequence is decoded as Methionine" begin + @testset "Methionine RNA sequence is decoded as Methionine" begin @test rna"AUG" == ["Methionine"] end - @test "Phenylalanine RNA sequence is decoded as Phenylalanine" begin + @testset "Phenylalanine RNA sequence is decoded as Phenylalanine" begin @test rna"UUU" == ["Phenylalanine"] end - @test "Other Phenylalanine RNA sequence is decoded as Phenylalanine" begin + @testset "Other Phenylalanine RNA sequence is decoded as Phenylalanine" begin @test rna"UUC" == ["Phenylalanine"] end - @test "Leucine RNA sequence is decoded as Leucine" begin + @testset "Leucine RNA sequence is decoded as Leucine" begin @test rna"UUA" == ["Leucine"] end - @test "Leucine RNA sequence is decoded as Leucine" begin + @testset "Leucine RNA sequence is decoded as Leucine" begin @test rna"UUG" == ["Leucine"] end - @test "Serine RNA sequence is decoded as Serine" begin + @testset "Serine RNA sequence is decoded as Serine" begin @test rna"UCUUCCUCAUCG" == ["Serine", "Serine", "Serine", "Serine"] end - @test "Tyrosine RNA sequence is decoded as Tyrosine" begin + @testset "Tyrosine RNA sequence is decoded as Tyrosine" begin @test rna"UAUUAC" == ["Tyrosine", "Tyrosine"] end - @test "Cysteine RNA sequence is decoded as Cysteine" begin + @testset "Cysteine RNA sequence is decoded as Cysteine" begin @test rna"UGUUGC" == ["Cysteine", "Cysteine"] end - @test "Tryptophan RNA sequence is decoded as Tryptophan" begin + @testset "Tryptophan RNA sequence is decoded as Tryptophan" begin @test rna"UGG" == ["Tryptophan"] end - @test "STOP codon terminates translation" begin + @testset "STOP codon terminates translation" begin @test rna"UAA" == [] @test rna"UAG" == [] @test rna"UGA" == [] end - @test "Sequence of two codons translates into proteins" begin + @testset "Sequence of two codons translates into proteins" begin @test rna"UUUUUU" == ["Phenylalanine", "Phenylalanine"] end - @test "Sequence of two different codons translates into proteins" begin + @testset "Sequence of two different codons translates into proteins" begin @test rna"UUAUUG" == ["Leucine", "Phenylalanine"] end - @test "Translation stops if STOP codon appears in middle of sequence" begin + @testset "Translation stops if STOP codon appears in middle of sequence" begin @test rna"UGGUAG" == ["Tryptophan"] end - @test "Translation stops if STOP codon appears at beginning of sequence" begin + @testset "Translation stops if STOP codon appears at beginning of sequence" begin @test rna"UAGUUUUGG" == [] end - @test "Translation stops if STOP codon appears at end of two-codon sequence" begin + @testset "Translation stops if STOP codon appears at end of two-codon sequence" begin @test rna"UGGUGUUAUUAAUGGUUU" == ["Tryptophan", "Cysteine", "Tyrosine"] end - @test "Non existent codon causes translation exception" begin + @testset "Non existent codon causes translation exception" begin @test_throws TranslationError rna"AAA" end - @test "Incomplete codon causes translation exception" begin + @testset "Incomplete codon causes translation exception" begin @test_throws TranslationError rna"UGGU" end - @test "Incomplete RNA sequence can translate if given a stop codon" begin + @testset "Incomplete RNA sequence can translate if given a stop codon" begin @test rna"UGGUAGUAAAA" == ["Tryptophan"] end end \ No newline at end of file From 7bbbe6170697adf405685f258312951e2c5ea195 Mon Sep 17 00:00:00 2001 From: Or Golan <golanor@gmail.com> Date: Tue, 30 Jan 2024 10:41:02 +0200 Subject: [PATCH 03/12] fixed example to pass tests --- exercises/practice/protein-translation/.meta/example.jl | 8 ++++++-- exercises/practice/protein-translation/runtests.jl | 6 +++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/exercises/practice/protein-translation/.meta/example.jl b/exercises/practice/protein-translation/.meta/example.jl index 21d6a1d9..c151a6a9 100644 --- a/exercises/practice/protein-translation/.meta/example.jl +++ b/exercises/practice/protein-translation/.meta/example.jl @@ -26,7 +26,11 @@ macro rna_str(str) n = 3 result = [] for i=1:n:length(str) - substring = SubString(str, i, i+n-1) + substring = try + SubString(str, i, i+n-1) + catch + throw(TranslationError("invalid rna string")) + end protein = string_to_protein(substring) protein == "STOP" && break push!(result, protein) @@ -39,4 +43,4 @@ function string_to_protein(str) p = get(codon_protein_dict, str, nothing) p === nothing && throw(TranslationError("invalid codon")) return p -end \ No newline at end of file +end diff --git a/exercises/practice/protein-translation/runtests.jl b/exercises/practice/protein-translation/runtests.jl index e066ec86..0930b12f 100644 --- a/exercises/practice/protein-translation/runtests.jl +++ b/exercises/practice/protein-translation/runtests.jl @@ -55,7 +55,7 @@ include("protein-translation.jl") end @testset "Sequence of two different codons translates into proteins" begin - @test rna"UUAUUG" == ["Leucine", "Phenylalanine"] + @test rna"UUAUUU" == ["Leucine", "Phenylalanine"] end @testset "Translation stops if STOP codon appears in middle of sequence" begin @@ -71,11 +71,11 @@ include("protein-translation.jl") end @testset "Non existent codon causes translation exception" begin - @test_throws TranslationError rna"AAA" + @test_throws TranslationError @macroexpand rna"AAA" end @testset "Incomplete codon causes translation exception" begin - @test_throws TranslationError rna"UGGU" + @test_throws TranslationError @macroexpand rna"UGGU" end @testset "Incomplete RNA sequence can translate if given a stop codon" begin From 857570d1cf7b6327cdad4b68afe3cc2c14819c99 Mon Sep 17 00:00:00 2001 From: Or Golan <golanor@gmail.com> Date: Tue, 30 Jan 2024 14:30:08 +0200 Subject: [PATCH 04/12] changes to the structure of the exercise --- exercises/practice/protein-translation/.meta/example.jl | 7 ++++++- .../practice/protein-translation/protein-translation.jl | 7 ++++++- exercises/practice/protein-translation/runtests.jl | 4 ++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/exercises/practice/protein-translation/.meta/example.jl b/exercises/practice/protein-translation/.meta/example.jl index c151a6a9..df345b33 100644 --- a/exercises/practice/protein-translation/.meta/example.jl +++ b/exercises/practice/protein-translation/.meta/example.jl @@ -23,6 +23,10 @@ struct TranslationError <: Exception end macro rna_str(str) + rna_translator(str) +end + +function rna_translator(str) n = 3 result = [] for i=1:n:length(str) @@ -35,7 +39,8 @@ macro rna_str(str) protein == "STOP" && break push!(result, protein) end - return result + result + end diff --git a/exercises/practice/protein-translation/protein-translation.jl b/exercises/practice/protein-translation/protein-translation.jl index 77b09a3d..60ad4687 100644 --- a/exercises/practice/protein-translation/protein-translation.jl +++ b/exercises/practice/protein-translation/protein-translation.jl @@ -1,3 +1,8 @@ macro rna_str() # I'm a ribosome macro! -end \ No newline at end of file +end + + +function rna_translator() + # I'm a ribosome function! +end diff --git a/exercises/practice/protein-translation/runtests.jl b/exercises/practice/protein-translation/runtests.jl index 0930b12f..ddc727a6 100644 --- a/exercises/practice/protein-translation/runtests.jl +++ b/exercises/practice/protein-translation/runtests.jl @@ -71,11 +71,11 @@ include("protein-translation.jl") end @testset "Non existent codon causes translation exception" begin - @test_throws TranslationError @macroexpand rna"AAA" + @test_throws TranslationError rna_translator("AAA") end @testset "Incomplete codon causes translation exception" begin - @test_throws TranslationError @macroexpand rna"UGGU" + @test_throws TranslationError rna_translator("UGUU") end @testset "Incomplete RNA sequence can translate if given a stop codon" begin From c37adfe44d7a6b6299c7acbcd6bc7f18500fb775 Mon Sep 17 00:00:00 2001 From: Or Golan <golanor@gmail.com> Date: Fri, 2 Feb 2024 08:15:33 +0200 Subject: [PATCH 05/12] removed the macro --- .../protein-translation/.meta/example.jl | 5 +-- .../protein-translation.jl | 7 +--- .../practice/protein-translation/runtests.jl | 38 +++++++++---------- 3 files changed, 19 insertions(+), 31 deletions(-) diff --git a/exercises/practice/protein-translation/.meta/example.jl b/exercises/practice/protein-translation/.meta/example.jl index df345b33..e4d1e2d3 100644 --- a/exercises/practice/protein-translation/.meta/example.jl +++ b/exercises/practice/protein-translation/.meta/example.jl @@ -1,4 +1,4 @@ -codon_protein_dict = Dict( +const codon_protein_dict = Dict( "AUG" => "Methionine", "UUU" => "Phenylalanine", "UUC" => "Phenylalanine", @@ -22,9 +22,6 @@ struct TranslationError <: Exception message::String end -macro rna_str(str) - rna_translator(str) -end function rna_translator(str) n = 3 diff --git a/exercises/practice/protein-translation/protein-translation.jl b/exercises/practice/protein-translation/protein-translation.jl index 60ad4687..e54925f0 100644 --- a/exercises/practice/protein-translation/protein-translation.jl +++ b/exercises/practice/protein-translation/protein-translation.jl @@ -1,8 +1,3 @@ -macro rna_str() - # I'm a ribosome macro! -end - - -function rna_translator() +function rna_translator(rna::String) # I'm a ribosome function! end diff --git a/exercises/practice/protein-translation/runtests.jl b/exercises/practice/protein-translation/runtests.jl index ddc727a6..1b8f3ef7 100644 --- a/exercises/practice/protein-translation/runtests.jl +++ b/exercises/practice/protein-translation/runtests.jl @@ -9,65 +9,61 @@ include("protein-translation.jl") end @testset "Methionine RNA sequence is decoded as Methionine" begin - @test rna"AUG" == ["Methionine"] + @test rna_translator("AUG") == ["Methionine"] end @testset "Phenylalanine RNA sequence is decoded as Phenylalanine" begin - @test rna"UUU" == ["Phenylalanine"] - end - - @testset "Other Phenylalanine RNA sequence is decoded as Phenylalanine" begin - @test rna"UUC" == ["Phenylalanine"] + @test rna_translator("UUUUUC") == ["Phenylalanine", "Phenylalanine"] end @testset "Leucine RNA sequence is decoded as Leucine" begin - @test rna"UUA" == ["Leucine"] + @test rna_translator("UUA") == ["Leucine"] end @testset "Leucine RNA sequence is decoded as Leucine" begin - @test rna"UUG" == ["Leucine"] + @test rna_translator("UUG") == ["Leucine"] end @testset "Serine RNA sequence is decoded as Serine" begin - @test rna"UCUUCCUCAUCG" == ["Serine", "Serine", "Serine", "Serine"] + @test rna_translator("UCUUCCUCAUCG") == ["Serine", "Serine", "Serine", "Serine"] end @testset "Tyrosine RNA sequence is decoded as Tyrosine" begin - @test rna"UAUUAC" == ["Tyrosine", "Tyrosine"] + @test rna_translator("UAUUAC") == ["Tyrosine", "Tyrosine"] end @testset "Cysteine RNA sequence is decoded as Cysteine" begin - @test rna"UGUUGC" == ["Cysteine", "Cysteine"] + @test rna_translator("UGUUGC") == ["Cysteine", "Cysteine"] end @testset "Tryptophan RNA sequence is decoded as Tryptophan" begin - @test rna"UGG" == ["Tryptophan"] + @test rna_translator("UGG") == ["Tryptophan"] end @testset "STOP codon terminates translation" begin - @test rna"UAA" == [] - @test rna"UAG" == [] - @test rna"UGA" == [] + @test rna_translator("UAA") == [] + @test rna_translator("UAG") == [] + @test rna_translator("UGA") == [] end @testset "Sequence of two codons translates into proteins" begin - @test rna"UUUUUU" == ["Phenylalanine", "Phenylalanine"] + @test rna_translator("UUUUUUUGA") == ["Phenylalanine", "Phenylalanine"] end @testset "Sequence of two different codons translates into proteins" begin - @test rna"UUAUUU" == ["Leucine", "Phenylalanine"] + @test rna_translator("UUAUUUUAG") == ["Leucine", "Phenylalanine"] end @testset "Translation stops if STOP codon appears in middle of sequence" begin - @test rna"UGGUAG" == ["Tryptophan"] + @test rna_translator("UGGUAAUGCAUG") == ["Tryptophan"] end @testset "Translation stops if STOP codon appears at beginning of sequence" begin - @test rna"UAGUUUUGG" == [] + @test rna_translator("UAGUAUUCGUCAUCU") == [] end @testset "Translation stops if STOP codon appears at end of two-codon sequence" begin - @test rna"UGGUGUUAUUAAUGGUUU" == ["Tryptophan", "Cysteine", "Tyrosine"] + @test rna_translator("UGGUGUUGA") == ["Tryptophan", "Cysteine", "Tyrosine"] end @testset "Non existent codon causes translation exception" begin @@ -79,6 +75,6 @@ include("protein-translation.jl") end @testset "Incomplete RNA sequence can translate if given a stop codon" begin - @test rna"UGGUAGUAAAA" == ["Tryptophan"] + @test rna_translator("UGGUGAUG") == ["Tryptophan"] end end \ No newline at end of file From ebaf324fa288a77fc3c846267a41d3f641f2bfc1 Mon Sep 17 00:00:00 2001 From: Or Golan <golanor@gmail.com> Date: Fri, 2 Feb 2024 08:17:49 +0200 Subject: [PATCH 06/12] fixed forgotten test --- exercises/practice/protein-translation/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/protein-translation/runtests.jl b/exercises/practice/protein-translation/runtests.jl index 1b8f3ef7..9835aaf9 100644 --- a/exercises/practice/protein-translation/runtests.jl +++ b/exercises/practice/protein-translation/runtests.jl @@ -5,7 +5,7 @@ include("protein-translation.jl") @testset "Protein Translation" begin @testset "Empty RNA sequence returns an empty list" begin - @test rna"" == [] + @test rna_translator("") == [] end @testset "Methionine RNA sequence is decoded as Methionine" begin From 3ee6b41886fa6438082c358a09159cae13c7ef56 Mon Sep 17 00:00:00 2001 From: Or Golan <golanor@gmail.com> Date: Fri, 2 Feb 2024 08:28:46 +0200 Subject: [PATCH 07/12] tests pass --- exercises/practice/protein-translation/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/exercises/practice/protein-translation/runtests.jl b/exercises/practice/protein-translation/runtests.jl index 9835aaf9..95e03ec0 100644 --- a/exercises/practice/protein-translation/runtests.jl +++ b/exercises/practice/protein-translation/runtests.jl @@ -63,7 +63,7 @@ include("protein-translation.jl") end @testset "Translation stops if STOP codon appears at end of two-codon sequence" begin - @test rna_translator("UGGUGUUGA") == ["Tryptophan", "Cysteine", "Tyrosine"] + @test rna_translator("UGGUGUUGA") == ["Tryptophan", "Cysteine"] end @testset "Non existent codon causes translation exception" begin From 196fa18a37e71d065f00dbaf7ede400c29412aa7 Mon Sep 17 00:00:00 2001 From: Or Golan <golanor@gmail.com> Date: Wed, 7 Feb 2024 10:46:07 +0200 Subject: [PATCH 08/12] rename function --- .../protein-translation/.meta/example.jl | 2 +- .../protein-translation.jl | 2 +- .../practice/protein-translation/runtests.jl | 40 +++++++++---------- 3 files changed, 22 insertions(+), 22 deletions(-) diff --git a/exercises/practice/protein-translation/.meta/example.jl b/exercises/practice/protein-translation/.meta/example.jl index e4d1e2d3..07b72b33 100644 --- a/exercises/practice/protein-translation/.meta/example.jl +++ b/exercises/practice/protein-translation/.meta/example.jl @@ -23,7 +23,7 @@ struct TranslationError <: Exception end -function rna_translator(str) +function rna_to_amino_acids(str) n = 3 result = [] for i=1:n:length(str) diff --git a/exercises/practice/protein-translation/protein-translation.jl b/exercises/practice/protein-translation/protein-translation.jl index e54925f0..2f299350 100644 --- a/exercises/practice/protein-translation/protein-translation.jl +++ b/exercises/practice/protein-translation/protein-translation.jl @@ -1,3 +1,3 @@ -function rna_translator(rna::String) +function rna_to_amino_acids(rna::String) # I'm a ribosome function! end diff --git a/exercises/practice/protein-translation/runtests.jl b/exercises/practice/protein-translation/runtests.jl index 95e03ec0..75a8276a 100644 --- a/exercises/practice/protein-translation/runtests.jl +++ b/exercises/practice/protein-translation/runtests.jl @@ -5,76 +5,76 @@ include("protein-translation.jl") @testset "Protein Translation" begin @testset "Empty RNA sequence returns an empty list" begin - @test rna_translator("") == [] + @test rna_to_amino_acids("") == [] end @testset "Methionine RNA sequence is decoded as Methionine" begin - @test rna_translator("AUG") == ["Methionine"] + @test rna_to_amino_acids("AUG") == ["Methionine"] end @testset "Phenylalanine RNA sequence is decoded as Phenylalanine" begin - @test rna_translator("UUUUUC") == ["Phenylalanine", "Phenylalanine"] + @test rna_to_amino_acids("UUUUUC") == ["Phenylalanine", "Phenylalanine"] end @testset "Leucine RNA sequence is decoded as Leucine" begin - @test rna_translator("UUA") == ["Leucine"] + @test rna_to_amino_acids("UUA") == ["Leucine"] end @testset "Leucine RNA sequence is decoded as Leucine" begin - @test rna_translator("UUG") == ["Leucine"] + @test rna_to_amino_acids("UUG") == ["Leucine"] end @testset "Serine RNA sequence is decoded as Serine" begin - @test rna_translator("UCUUCCUCAUCG") == ["Serine", "Serine", "Serine", "Serine"] + @test rna_to_amino_acids("UCUUCCUCAUCG") == ["Serine", "Serine", "Serine", "Serine"] end @testset "Tyrosine RNA sequence is decoded as Tyrosine" begin - @test rna_translator("UAUUAC") == ["Tyrosine", "Tyrosine"] + @test rna_to_amino_acids("UAUUAC") == ["Tyrosine", "Tyrosine"] end @testset "Cysteine RNA sequence is decoded as Cysteine" begin - @test rna_translator("UGUUGC") == ["Cysteine", "Cysteine"] + @test rna_to_amino_acids("UGUUGC") == ["Cysteine", "Cysteine"] end @testset "Tryptophan RNA sequence is decoded as Tryptophan" begin - @test rna_translator("UGG") == ["Tryptophan"] + @test rna_to_amino_acids("UGG") == ["Tryptophan"] end @testset "STOP codon terminates translation" begin - @test rna_translator("UAA") == [] - @test rna_translator("UAG") == [] - @test rna_translator("UGA") == [] + @test rna_to_amino_acids("UAA") == [] + @test rna_to_amino_acids("UAG") == [] + @test rna_to_amino_acids("UGA") == [] end @testset "Sequence of two codons translates into proteins" begin - @test rna_translator("UUUUUUUGA") == ["Phenylalanine", "Phenylalanine"] + @test rna_to_amino_acids("UUUUUUUGA") == ["Phenylalanine", "Phenylalanine"] end @testset "Sequence of two different codons translates into proteins" begin - @test rna_translator("UUAUUUUAG") == ["Leucine", "Phenylalanine"] + @test rna_to_amino_acids("UUAUUUUAG") == ["Leucine", "Phenylalanine"] end @testset "Translation stops if STOP codon appears in middle of sequence" begin - @test rna_translator("UGGUAAUGCAUG") == ["Tryptophan"] + @test rna_to_amino_acids("UGGUAAUGCAUG") == ["Tryptophan"] end @testset "Translation stops if STOP codon appears at beginning of sequence" begin - @test rna_translator("UAGUAUUCGUCAUCU") == [] + @test rna_to_amino_acids("UAGUAUUCGUCAUCU") == [] end @testset "Translation stops if STOP codon appears at end of two-codon sequence" begin - @test rna_translator("UGGUGUUGA") == ["Tryptophan", "Cysteine"] + @test rna_to_amino_acids("UGGUGUUGA") == ["Tryptophan", "Cysteine"] end @testset "Non existent codon causes translation exception" begin - @test_throws TranslationError rna_translator("AAA") + @test_throws TranslationError rna_to_amino_acids("AAA") end @testset "Incomplete codon causes translation exception" begin - @test_throws TranslationError rna_translator("UGUU") + @test_throws TranslationError rna_to_amino_acids("UGUU") end @testset "Incomplete RNA sequence can translate if given a stop codon" begin - @test rna_translator("UGGUGAUG") == ["Tryptophan"] + @test rna_to_amino_acids("UGGUGAUG") == ["Tryptophan"] end end \ No newline at end of file From 2962f22788f3b6d463d24fdb099a102f14ac16e2 Mon Sep 17 00:00:00 2001 From: Or Golan <golanor@gmail.com> Date: Wed, 7 Feb 2024 10:52:17 +0200 Subject: [PATCH 09/12] added macro as bonus --- .../.docs/instructions.append.md | 13 +++++++++++++ exercises/practice/protein-translation/runtests.jl | 10 +++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 exercises/practice/protein-translation/.docs/instructions.append.md diff --git a/exercises/practice/protein-translation/.docs/instructions.append.md b/exercises/practice/protein-translation/.docs/instructions.append.md new file mode 100644 index 00000000..61f37647 --- /dev/null +++ b/exercises/practice/protein-translation/.docs/instructions.append.md @@ -0,0 +1,13 @@ +# Instructions append + +This is a good exercise to experiment with non-standard string literals. + +A short introduction to non-standard string literals can be found in this [blog post](https://web.archive.org/web/20170625222109/https://iaindunning.com/blog/julia-unicode.html). + +You can extend your solution by adding the functionality described below. + +## Bonus +Implement a string literal that translates RNA strings as: +```julia +rna"AUGUGU" == ["Methionine", "Cysteine"] +``` diff --git a/exercises/practice/protein-translation/runtests.jl b/exercises/practice/protein-translation/runtests.jl index 75a8276a..3c538e46 100644 --- a/exercises/practice/protein-translation/runtests.jl +++ b/exercises/practice/protein-translation/runtests.jl @@ -77,4 +77,12 @@ include("protein-translation.jl") @testset "Incomplete RNA sequence can translate if given a stop codon" begin @test rna_to_amino_acids("UGGUGAUG") == ["Tryptophan"] end -end \ No newline at end of file + + # Bonus + if isdefined(@__MODULE__, Symbol("@rna_str")) + @eval @testset "Bonus: rna string macro" begin + @test rna"AUGUUUUUAUGGUACUAG" == ["Methionine", "Phenylalanine", "Leucine", "Tryptophan", "Tyrosine"] + end + end + +end From ac65c312f731e52618145c95c893ebaab5b7536f Mon Sep 17 00:00:00 2001 From: Colin Caine <cmcaine@gmail.com> Date: Wed, 3 Apr 2024 18:30:38 +0100 Subject: [PATCH 10/12] Fix typo --- config.json | 1 - 1 file changed, 1 deletion(-) diff --git a/config.json b/config.json index d77b5aeb..aa367eb4 100644 --- a/config.json +++ b/config.json @@ -881,7 +881,6 @@ "practices": [], "prerequisites": [], "difficulty": 2 - } ] }, From 8335d87f201feef6be73c2324db6c3bb2cc60cfb Mon Sep 17 00:00:00 2001 From: Or Golan <golanor@users.noreply.github.com> Date: Fri, 19 Apr 2024 13:46:11 +0300 Subject: [PATCH 11/12] use argument error instead of a new user-defined error Co-authored-by: Colin Caine <cmcaine@gmail.com> --- exercises/practice/protein-translation/runtests.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/exercises/practice/protein-translation/runtests.jl b/exercises/practice/protein-translation/runtests.jl index 3c538e46..17bfe027 100644 --- a/exercises/practice/protein-translation/runtests.jl +++ b/exercises/practice/protein-translation/runtests.jl @@ -67,11 +67,11 @@ include("protein-translation.jl") end @testset "Non existent codon causes translation exception" begin - @test_throws TranslationError rna_to_amino_acids("AAA") + @test_throws ArgumentError rna_to_amino_acids("AAA") end @testset "Incomplete codon causes translation exception" begin - @test_throws TranslationError rna_to_amino_acids("UGUU") + @test_throws ArgumentError rna_to_amino_acids("UGUU") end @testset "Incomplete RNA sequence can translate if given a stop codon" begin From 2667d93f0e1445d976d43e57170cdb6633086596 Mon Sep 17 00:00:00 2001 From: Or Golan <golanor@users.noreply.github.com> Date: Fri, 19 Apr 2024 13:46:47 +0300 Subject: [PATCH 12/12] improve wording of the bonus part Co-authored-by: Colin Caine <cmcaine@gmail.com> --- .../.docs/instructions.append.md | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/exercises/practice/protein-translation/.docs/instructions.append.md b/exercises/practice/protein-translation/.docs/instructions.append.md index 61f37647..542c9dc7 100644 --- a/exercises/practice/protein-translation/.docs/instructions.append.md +++ b/exercises/practice/protein-translation/.docs/instructions.append.md @@ -1,13 +1,19 @@ # Instructions append -This is a good exercise to experiment with non-standard string literals. +## Bonus -A short introduction to non-standard string literals can be found in this [blog post](https://web.archive.org/web/20170625222109/https://iaindunning.com/blog/julia-unicode.html). +You might like to use this exercise as an excuse to experiment with [non-standard string literals][nssl]. +A short introduction to non-standard string literals can be found in this [blog post][nssl-blog]. -You can extend your solution by adding the functionality described below. +To pass the bonus tests, define a macro `rna_str` as explained in the links above, then your users could write code like this: -## Bonus -Implement a string literal that translates RNA strings as: ```julia rna"AUGUGU" == ["Methionine", "Cysteine"] + +rna""" +AUGUUUUCUUAAAUG +""" == ["Methionine", "Phenylalanine", "Serine"] ``` + +[nssl]: https://docs.julialang.org/en/v1/manual/metaprogramming/#meta-non-standard-string-literals +[nssl-blog]: https://web.archive.org/web/20170625222109/https://iaindunning.com/blog/julia-unicode.html