-
-
Notifications
You must be signed in to change notification settings - Fork 13
Add new practice exercise: hamming #113
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
# Instructions | ||
|
||
Calculate the Hamming distance between two DNA strands. | ||
|
||
We read DNA using the letters C, A, G and T. | ||
Two strands might look like this: | ||
|
||
GAGCCTACTAACGGGAT | ||
CATCGTAATGACGGCCT | ||
^ ^ ^ ^ ^ ^^ | ||
|
||
They have 7 differences, and therefore the Hamming distance is 7. | ||
|
||
## Implementation notes | ||
|
||
The Hamming distance is only defined for sequences of equal length, so an attempt to calculate it between sequences of different lengths should not work. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# Introduction | ||
|
||
Your body is made up of cells that contain DNA. | ||
Those cells regularly wear out and need replacing, which they achieve by dividing into daughter cells. | ||
In fact, the average human body experiences about 10 quadrillion cell divisions in a lifetime! | ||
|
||
When cells divide, their DNA replicates too. | ||
Sometimes during this process mistakes happen and single pieces of DNA get encoded with the incorrect information. | ||
If we compare two strands of DNA and count the differences between them, we can see how many mistakes occurred. | ||
This is known as the "Hamming distance". | ||
|
||
The Hamming distance is useful in many areas of science, not just biology, so it's a nice phrase to be familiar with :) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"jimmytty" | ||
], | ||
"files": { | ||
"solution": [ | ||
"hamming.sql" | ||
], | ||
"test": [ | ||
"hamming_test.sql" | ||
], | ||
"example": [ | ||
".meta/example.sql" | ||
] | ||
}, | ||
"blurb": "Calculate the Hamming distance between two DNA strands.", | ||
"source": "The Calculating Point Mutations problem at Rosalind", | ||
"source_url": "https://rosalind.info/problems/hamm/" | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
UPDATE hamming | ||
SET error = 'strands must be of equal length' | ||
WHERE LENGTH(strand1) != LENGTH(strand2) | ||
; | ||
|
||
UPDATE hamming | ||
SET result = ( | ||
WITH RECURSIVE rcte(string1, string2, char1, char2) AS ( | ||
VALUES(strand1, strand2, '', '') | ||
UNION ALL | ||
SELECT SUBSTRING(string1, 2), SUBSTRING(string2, 2), | ||
SUBSTRING(string1, 1, 1), SUBSTRING(string2, 1, 1) | ||
FROM rcte | ||
WHERE string1 <> '' | ||
) | ||
SELECT COUNT(*) | ||
FROM rcte | ||
WHERE char1 != char2 | ||
) | ||
WHERE LENGTH(strand1) = LENGTH(strand2) | ||
; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# 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. | ||
|
||
[f6dcb64f-03b0-4b60-81b1-3c9dbf47e887] | ||
description = "empty strands" | ||
|
||
[54681314-eee2-439a-9db0-b0636c656156] | ||
description = "single letter identical strands" | ||
|
||
[294479a3-a4c8-478f-8d63-6209815a827b] | ||
description = "single letter different strands" | ||
|
||
[9aed5f34-5693-4344-9b31-40c692fb5592] | ||
description = "long identical strands" | ||
|
||
[cd2273a5-c576-46c8-a52b-dee251c3e6e5] | ||
description = "long different strands" | ||
|
||
[919f8ef0-b767-4d1b-8516-6379d07fcb28] | ||
description = "disallow first strand longer" | ||
include = false | ||
|
||
[b9228bb1-465f-4141-b40f-1f99812de5a8] | ||
description = "disallow first strand longer" | ||
reimplements = "919f8ef0-b767-4d1b-8516-6379d07fcb28" | ||
|
||
[8a2d4ed0-ead5-4fdd-924d-27c4cf56e60e] | ||
description = "disallow second strand longer" | ||
include = false | ||
|
||
[dab38838-26bb-4fff-acbe-3b0a9bfeba2d] | ||
description = "disallow second strand longer" | ||
reimplements = "8a2d4ed0-ead5-4fdd-924d-27c4cf56e60e" | ||
|
||
[5dce058b-28d4-4ca7-aa64-adfe4e17784c] | ||
description = "disallow left empty strand" | ||
include = false | ||
|
||
[db92e77e-7c72-499d-8fe6-9354d2bfd504] | ||
description = "disallow left empty strand" | ||
include = false | ||
reimplements = "5dce058b-28d4-4ca7-aa64-adfe4e17784c" | ||
|
||
[b764d47c-83ff-4de2-ab10-6cfe4b15c0f3] | ||
description = "disallow empty first strand" | ||
reimplements = "db92e77e-7c72-499d-8fe6-9354d2bfd504" | ||
|
||
[38826d4b-16fb-4639-ac3e-ba027dec8b5f] | ||
description = "disallow right empty strand" | ||
include = false | ||
|
||
[920cd6e3-18f4-4143-b6b8-74270bb8f8a3] | ||
description = "disallow right empty strand" | ||
include = false | ||
reimplements = "38826d4b-16fb-4639-ac3e-ba027dec8b5f" | ||
|
||
[9ab9262f-3521-4191-81f5-0ed184a5aa89] | ||
description = "disallow empty second strand" | ||
reimplements = "920cd6e3-18f4-4143-b6b8-74270bb8f8a3" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
DROP TABLE IF EXISTS hamming; | ||
CREATE TABLE hamming ( | ||
strand1 TEXT NOT NULL, | ||
strand2 TEXT NOT NULL, | ||
result INTEGER, | ||
error TEXT | ||
); | ||
|
||
.mode csv | ||
.import ./data.csv hamming | ||
|
||
UPDATE hamming SET result = NULL, error = NULL; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
DROP TABLE IF EXISTS tests; | ||
CREATE TABLE IF NOT EXISTS tests ( | ||
-- uuid and description are taken from the test.toml file | ||
uuid TEXT PRIMARY KEY, | ||
description TEXT NOT NULL, | ||
-- The following section is needed by the online test-runner | ||
status TEXT DEFAULT 'fail', | ||
message TEXT, | ||
output TEXT, | ||
test_code TEXT, | ||
task_id INTEGER DEFAULT NULL, | ||
-- Here are columns for the actual tests | ||
strand1 TEXT NOT NULL, | ||
strand2 TEXT NOT NULL, | ||
expected_result INTEGER, | ||
expected_error TEXT | ||
); | ||
|
||
INSERT INTO tests (uuid, description, strand1, strand2, expected_result, expected_error) | ||
VALUES | ||
('f6dcb64f-03b0-4b60-81b1-3c9dbf47e887', 'empty strands', '', '', 0, null), | ||
('54681314-eee2-439a-9db0-b0636c656156', 'single letter identical strands', 'A', 'A', 0, null), | ||
('294479a3-a4c8-478f-8d63-6209815a827b', 'single letter different strands', 'G', 'T', 1, null), | ||
('9aed5f34-5693-4344-9b31-40c692fb5592', 'long identical strands', 'GGACTGAAATCTG', 'GGACTGAAATCTG', 0, null), | ||
('cd2273a5-c576-46c8-a52b-dee251c3e6e5', 'long different strands', 'GGACGGATTCTG', 'AGGACGGATTCT', 9, null), | ||
('b9228bb1-465f-4141-b40f-1f99812de5a8', 'disallow first strand longer', 'AATG', 'AAA', null, 'strands must be of equal length'), | ||
('dab38838-26bb-4fff-acbe-3b0a9bfeba2d', 'disallow second strand longer', 'ATA', 'AGTG', null, 'strands must be of equal length'), | ||
('b764d47c-83ff-4de2-ab10-6cfe4b15c0f3', 'disallow empty first strand', '', 'G', null, 'strands must be of equal length'), | ||
('9ab9262f-3521-4191-81f5-0ed184a5aa89', 'disallow empty second strand', 'G', '', null, 'strands must be of equal length'); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"","","","" | ||
"A","A","","" | ||
"G","T","","" | ||
"GGACTGAAATCTG","GGACTGAAATCTG","","" | ||
"GGACGGATTCTG","AGGACGGATTCT","","" | ||
"AATG","AAA","","" | ||
"ATA","AGTG","","" | ||
"","G","","" | ||
"G","","","" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
-- Schema: | ||
-- CREATE TABLE hamming ( | ||
-- strand1 TEXT NOT NULL, | ||
-- strand2 TEXT NOT NULL, | ||
-- result INTEGER, | ||
-- error TEXT | ||
-- ); | ||
-- | ||
-- Task: update the hamming table and set the result column or the error column based on the comparison between strand1 and strand2. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
-- Create database: | ||
.read ./create_fixture.sql | ||
|
||
-- Read user student solution and save any output as markdown in user_output.md: | ||
.mode markdown | ||
.output user_output.md | ||
.read ./hamming.sql | ||
.output | ||
|
||
-- Create a clean testing environment: | ||
.read ./create_test_table.sql | ||
|
||
-- Comparison of user input and the tests updates the status for each test: | ||
UPDATE tests | ||
SET status = 'pass' | ||
FROM (SELECT strand1, strand2, result, error FROM hamming) AS actual | ||
WHERE (actual.strand1, actual.strand2) = (tests.strand1, tests.strand2) AND (actual.result = tests.expected_result OR COALESCE(actual.result, tests.expected_result) ISNULL) AND (actual.error = tests.expected_error OR COALESCE(actual.error, tests.expected_error) ISNULL); | ||
|
||
-- Update message for failed tests to give helpful information: | ||
UPDATE tests | ||
SET message = ( | ||
'Result for "' | ||
|| PRINTF('strand1=''%s'' and strand2=''%s''', actual.strand1, actual.strand2) | ||
|| '"' | ||
|| ' is <' || PRINTF('result=%s and error="%s"', COALESCE(actual.result, 'NULL'), COALESCE(actual.error, 'NULL')) | ||
|| '> but should be <' || PRINTF('result=%s and error="%s"', COALESCE(tests.expected_result, '"NULL"'), COALESCE(tests.expected_error, 'NULL')) || '>' | ||
) | ||
FROM (SELECT strand1, strand2, result, error FROM hamming) AS actual | ||
WHERE (actual.strand1, actual.strand2) = (tests.strand1, tests.strand2) AND tests.status = 'fail'; | ||
|
||
-- Save results to ./output.json (needed by the online test-runner) | ||
.mode json | ||
.once './output.json' | ||
SELECT description, status, message, output, test_code, task_id | ||
FROM tests; | ||
|
||
-- Display test results in readable form for the student: | ||
.mode table | ||
SELECT description, status, message | ||
FROM tests; |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.