Last active
January 10, 2025 11:04
-
-
Save jedavidson/2ca7b94079670b12ba2dc5efbc30df52 to your computer and use it in GitHub Desktop.
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
#include <cassert> | |
#include <string> | |
// A storage class for DNA sequences, which are sequences (i.e. strings) | |
// consisting of one of four bases: A, C, T and G. | |
class dna_store { | |
public: | |
// Adds a sequence to the store. You may assume that the sequence | |
// is a nonempty string made up only of the letters {A, C, T, G}. | |
void add(const std::string &seq) { | |
return; | |
} | |
// Searches for a DNA sequence in the store. | |
bool contains(const std::string &seq) { | |
return false; | |
} | |
// As for contains, except allows method callers to add wildcards, by | |
// writing . anywhere in a search sequence to stand for any letter. | |
bool contains_with_wildcard(const std::string &seq) const { | |
return false; | |
} | |
}; | |
// Compile: clang++ -std=c++20 dna.cpp -o dna | |
int main() { | |
auto store = dna_store{}; | |
for (const auto &seq : {"AC", "AT", "ACTG", "ACTGAGT", "A", "C"}) { | |
store.add(seq); | |
} | |
for (const auto &seq : {"AC", "AT", "ACTG", "ACTGAGT", "A", "C"}) { | |
assert(store.contains(seq)); | |
} | |
assert(!store.contains("CC")); | |
assert(store.contains_with_wildcard(".")); | |
assert(!store.contains_with_wildcard("...")); | |
assert(store.contains_with_wildcard("AC..AG.")); | |
for (const auto &seq : {"AC", "AT", "ACTG", "ACTGAGT", "A", "C"}) { | |
assert(store.contains_with_wildcard(seq)); | |
} | |
assert(!store.contains_with_wildcard("CC")); | |
} |
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
class DNAStore: | |
def __init__(self): | |
""" | |
Creates a new DNA store. | |
""" | |
pass | |
def add(self, sequence: str): | |
""" | |
Adds a sequence to the DNA store. You may assume that the sequence | |
is a nonempty string made up only of the letters {A, C, T, G}. | |
""" | |
pass | |
def contains(self, sequence: str) -> bool: | |
""" | |
Searches for a DNA sequence in the DNA store. | |
""" | |
return True | |
def contains_with_wildcard(self, sequence: str) -> bool: | |
""" | |
As for contains, except allows method callers to add wildcards, by | |
writing . anywhere in a search sequence to stand for any letter. | |
""" | |
return True | |
if __name__ == "__main__": | |
store = DNAStore() | |
for sequence in ("AC", "AT", "ACTG", "ACTGAGT", "A", "C"): | |
store.add(sequence) | |
for sequence in ("AC", "AT", "ACTG", "ACTGAGT", "A", "C"): | |
assert store.contains(sequence) | |
assert not store.contains("CC") | |
assert store.contains_with_wildcard(".") | |
assert not store.contains_with_wildcard("...") | |
assert store.contains_with_wildcard("AC..AG.") | |
for sequence in ("AC", "AT", "ACTG", "ACTGAGT", "A", "C"): | |
assert store.contains_with_wildcard(sequence) | |
assert not store.contains_with_wildcard("CC") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Sample C++ solution:
Sample Python solution:
Time complexity is
insert
andcontains
, andcontains_with_wildcard
, which is realised when the store contains all