Baseline: tree as shared
This commit is contained in:
1679 files changed
+1024562
No files matched your search
@@ -0,0 +1,20 @@
|
||||
# origin: Programming+ exercise best-position
|
||||
# status: unverified; `corpus.py verify` checks it on the pinned compiler
|
||||
# probe: the position of the highest quality score; the first one, if several tie
|
||||
# expect-stdout: 1
|
||||
def best_position(q: List[Int]) -> Int:
|
||||
# requires: len(q) >= 1
|
||||
# ensures: 0 <= result < len(q)
|
||||
# ensures: forall k in [0, len(q)) : q[k] <= q[result]
|
||||
var best: Int = 0
|
||||
for i in range(1, len(q)):
|
||||
# invariant: 0 <= best < i
|
||||
# invariant: forall k in [0, i) : q[k] <= q[best]
|
||||
if q[i] > q[best]:
|
||||
best = i
|
||||
return best
|
||||
|
||||
|
||||
def main():
|
||||
var scores: List[Int] = [12, 30, 25, 30, 8]
|
||||
print(best_position(scores))
|
||||
@@ -0,0 +1,24 @@
|
||||
# origin: Programming+ exercise binary-search
|
||||
# status: unverified; `corpus.py verify` checks it on the pinned compiler
|
||||
# probe: is x in the sorted list l? halve the range that could hold it each time
|
||||
# expect-stdout: True
|
||||
def contains_sorted(l: List[Int], x: Int) -> Bool:
|
||||
# requires: forall k in [0, len(l) - 1) : l[k] <= l[k + 1]
|
||||
var lo: Int = 0
|
||||
var hi: Int = len(l)
|
||||
while lo < hi:
|
||||
# invariant: 0 <= lo <= hi <= len(l)
|
||||
# decreases: hi - lo
|
||||
var mid = (lo + hi) // 2
|
||||
if l[mid] == x:
|
||||
return True
|
||||
elif l[mid] < x:
|
||||
lo = mid + 1
|
||||
else:
|
||||
hi = mid
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
var sorted_values: List[Int] = [2, 5, 8, 13, 21]
|
||||
print(contains_sorted(sorted_values, 13))
|
||||
@@ -0,0 +1,16 @@
|
||||
# origin: Programming+ exercise count-base-proved
|
||||
# status: unverified; `corpus.py verify` checks it on the pinned compiler
|
||||
# probe: count_base with its claims as comments, as the matho pane states them
|
||||
# expect-stdout: 3
|
||||
def count_base(b: String, t: String) -> Int:
|
||||
# ensures: result == |{ k in [0, |t|) : t[k] == b }|
|
||||
var total: Int = 0
|
||||
for i in range(t.count_codepoints()):
|
||||
# invariant: total == |{ k in [0, i) : t[k] == b }|
|
||||
if String(t[codepoint=i]) == b:
|
||||
total += 1
|
||||
return total
|
||||
|
||||
|
||||
def main():
|
||||
print(count_base("A", "GATTACA"))
|
||||
@@ -0,0 +1,14 @@
|
||||
# origin: Programming+ exercise count-base (was translations.mojo, by course staff)
|
||||
# status: unverified; Stage 1 verifies it on the pinned compiler
|
||||
# probe: does the course's hand-written count_base compile and agree with the pseudo?
|
||||
# expect-stdout: 3
|
||||
def count_base(b: String, t: String) -> Int:
|
||||
var total = 0
|
||||
for i in range(t.count_codepoints()):
|
||||
if String(t[codepoint=i]) == b:
|
||||
total += 1
|
||||
return total
|
||||
|
||||
|
||||
def main():
|
||||
print(count_base("A", "GATTACA"))
|
||||
@@ -0,0 +1,239 @@
|
||||
# origin: Programming+ demo playground
|
||||
# status: unverified; `corpus.py verify` checks it on the pinned compiler
|
||||
# probe: Smith-Waterman local alignment with affine gaps (Gotoh): a read with a 2-letter deletion, scored two ways; three tables allocated, filled, traced back, and freed at their last use
|
||||
# mode: both
|
||||
# args: -O0 --debug-level=line-tables
|
||||
# expect-stdout: read GATCAGGT against reference TTGATTACAGGTCC
|
||||
# expect-stdout: 1. align with affine gaps
|
||||
# expect-stdout: + alloc H (best score ending here) 15x9
|
||||
# expect-stdout: + alloc E (ends in a gap in the reference) 15x9
|
||||
# expect-stdout: + alloc F (ends in a gap in the read) 15x9
|
||||
# expect-stdout: - free F (ends in a gap in the read)
|
||||
# expect-stdout: - free E (ends in a gap in the reference)
|
||||
# expect-stdout: - free H (best score ending here)
|
||||
# expect-stdout: affine gaps: opening a gap costs 5, each further letter 1
|
||||
# expect-stdout: score 18 | reference letters 3 to 12
|
||||
# expect-stdout: reference GATTACAGGT
|
||||
# expect-stdout: ||| |||||
|
||||
# expect-stdout: read GAT--CAGGT
|
||||
# expect-stdout: 2. align the same read with linear gaps
|
||||
# expect-stdout: + alloc H (best score ending here) 15x9
|
||||
# expect-stdout: + alloc E (ends in a gap in the reference) 15x9
|
||||
# expect-stdout: + alloc F (ends in a gap in the read) 15x9
|
||||
# expect-stdout: - free F (ends in a gap in the read)
|
||||
# expect-stdout: - free E (ends in a gap in the reference)
|
||||
# expect-stdout: - free H (best score ending here)
|
||||
# expect-stdout: linear gaps: every gap letter costs 2
|
||||
# expect-stdout: score 20 | reference letters 3 to 12
|
||||
# expect-stdout: reference GATTACAGGT
|
||||
# expect-stdout: || | |||||
|
||||
# expect-stdout: read GA-T-CAGGT
|
||||
# expect-stdout: the models disagree: one 2-letter deletion, or two 1-letter deletions?
|
||||
# Far below any real score: a gap state that cannot have been reached yet.
|
||||
comptime NEG_INF = -1_000_000
|
||||
|
||||
|
||||
def cell(diagonal: Int, gap_in_reference: Int, gap_in_read: Int) -> Int:
|
||||
"""The Smith-Waterman recurrence for one cell: the best of the three ways in, but never
|
||||
below 0, because a local alignment may start afresh anywhere."""
|
||||
var best: Int = 0
|
||||
if diagonal > best:
|
||||
best = diagonal
|
||||
if gap_in_reference > best:
|
||||
best = gap_in_reference
|
||||
if gap_in_read > best:
|
||||
best = gap_in_read
|
||||
return best
|
||||
|
||||
|
||||
def larger(a: Int, b: Int) -> Int:
|
||||
if a > b:
|
||||
return a
|
||||
return b
|
||||
|
||||
|
||||
@fieldwise_init
|
||||
struct Scoring(Copyable, Movable):
|
||||
"""How an alignment is scored. With gap_open == gap_extend every gap letter costs the
|
||||
same (linear gaps); with gap_open below gap_extend one long gap is cheaper than
|
||||
several short ones (affine gaps), which is what real insertions and deletions look like.
|
||||
"""
|
||||
|
||||
var name: String
|
||||
var match_score: Int
|
||||
var mismatch_score: Int
|
||||
var gap_open: Int
|
||||
var gap_extend: Int
|
||||
|
||||
def pair(self, same: Bool) -> Int:
|
||||
if same:
|
||||
return self.match_score
|
||||
return self.mismatch_score
|
||||
|
||||
|
||||
struct Grid(Movable):
|
||||
"""A dynamic-programming table that owns its memory, row by row in one List,
|
||||
and says when that memory is allocated and freed."""
|
||||
|
||||
var name: String
|
||||
var cols: Int
|
||||
var cells: List[Int]
|
||||
|
||||
def __init__(out self, name: String, rows: Int, cols: Int, start: Int):
|
||||
self.name = name
|
||||
self.cols = cols
|
||||
self.cells = List[Int]()
|
||||
for _ in range(rows * cols):
|
||||
self.cells.append(start)
|
||||
print(" + alloc", name, String(rows) + "x" + String(cols))
|
||||
|
||||
def __init__(out self, *, deinit move: Self):
|
||||
self.name = move.name^
|
||||
self.cols = move.cols
|
||||
self.cells = move.cells^
|
||||
|
||||
def __deinit__(deinit self):
|
||||
print(" - free ", self.name)
|
||||
|
||||
def at(self, i: Int, j: Int) -> Int:
|
||||
return self.cells[i * self.cols + j]
|
||||
|
||||
def put(mut self, i: Int, j: Int, value: Int):
|
||||
self.cells[i * self.cols + j] = value
|
||||
|
||||
|
||||
@fieldwise_init
|
||||
struct Alignment(Copyable, Movable):
|
||||
"""The best local alignment: its score, where it sits in the reference, and the three
|
||||
printed rows (reference, match marks, read)."""
|
||||
|
||||
var score: Int
|
||||
var start: Int
|
||||
var end: Int
|
||||
var top: String
|
||||
var marks: String
|
||||
var bottom: String
|
||||
|
||||
|
||||
def align(reference: String, seq_read: String, s: Scoring) -> Alignment:
|
||||
"""Smith-Waterman local alignment with affine gaps (Gotoh's three tables).
|
||||
|
||||
H[i][j]: the best score of an alignment ending at reference letter i and read letter j.
|
||||
E[i][j]: the same, but ending in a gap in the reference (a letter of the read left over).
|
||||
F[i][j]: the same, but ending in a gap in the read (a letter of the reference skipped).
|
||||
"""
|
||||
var n = reference.count_codepoints()
|
||||
var m = seq_read.count_codepoints()
|
||||
var h = Grid("H (best score ending here)", n + 1, m + 1, 0)
|
||||
var e = Grid("E (ends in a gap in the reference)", n + 1, m + 1, NEG_INF)
|
||||
var f = Grid("F (ends in a gap in the read)", n + 1, m + 1, NEG_INF)
|
||||
|
||||
# fill: every cell from its three neighbours
|
||||
var best = 0
|
||||
var best_i = 0
|
||||
var best_j = 0
|
||||
for i in range(1, n + 1):
|
||||
for j in range(1, m + 1):
|
||||
e.put(
|
||||
i,
|
||||
j,
|
||||
larger(
|
||||
e.at(i, j - 1) + s.gap_extend, h.at(i, j - 1) + s.gap_open
|
||||
),
|
||||
)
|
||||
f.put(
|
||||
i,
|
||||
j,
|
||||
larger(
|
||||
f.at(i - 1, j) + s.gap_extend, h.at(i - 1, j) + s.gap_open
|
||||
),
|
||||
)
|
||||
var same = reference[codepoint=i - 1] == seq_read[codepoint=j - 1]
|
||||
var diagonal = h.at(i - 1, j - 1) + s.pair(same)
|
||||
var here = cell(diagonal, e.at(i, j), f.at(i, j))
|
||||
h.put(i, j, here)
|
||||
if here > best:
|
||||
best = here
|
||||
best_i = i
|
||||
best_j = j
|
||||
|
||||
# traceback: walk back from the best cell, switching tables where a gap opened
|
||||
var top = String("")
|
||||
var marks = String("")
|
||||
var bottom = String("")
|
||||
var i = best_i
|
||||
var j = best_j
|
||||
var table = 0 # 0: in H, 1: in E, 2: in F
|
||||
while i > 0 and j > 0:
|
||||
if table == 0:
|
||||
if h.at(i, j) == 0:
|
||||
break
|
||||
var same = reference[codepoint=i - 1] == seq_read[codepoint=j - 1]
|
||||
if h.at(i, j) == h.at(i - 1, j - 1) + s.pair(same):
|
||||
top = String(reference[codepoint=i - 1]) + top
|
||||
bottom = String(seq_read[codepoint=j - 1]) + bottom
|
||||
if same:
|
||||
marks = "|" + marks
|
||||
else:
|
||||
marks = "." + marks
|
||||
i -= 1
|
||||
j -= 1
|
||||
elif h.at(i, j) == e.at(i, j):
|
||||
table = 1
|
||||
else:
|
||||
table = 2
|
||||
elif table == 1:
|
||||
top = "-" + top
|
||||
marks = " " + marks
|
||||
bottom = String(seq_read[codepoint=j - 1]) + bottom
|
||||
if e.at(i, j) == h.at(i, j - 1) + s.gap_open:
|
||||
table = 0
|
||||
j -= 1
|
||||
else:
|
||||
top = String(reference[codepoint=i - 1]) + top
|
||||
marks = " " + marks
|
||||
bottom = "-" + bottom
|
||||
if f.at(i, j) == h.at(i - 1, j) + s.gap_open:
|
||||
table = 0
|
||||
i -= 1
|
||||
return Alignment(best, i + 1, best_i, top^, marks^, bottom^)
|
||||
|
||||
|
||||
def report(name: String, a: Alignment):
|
||||
print(" " + name)
|
||||
print(" score", a.score, "| reference letters", a.start, "to", a.end)
|
||||
print(" reference ", a.top)
|
||||
print(" ", a.marks)
|
||||
print(" read ", a.bottom)
|
||||
|
||||
|
||||
def main():
|
||||
# A sequencing read that lost two letters (TA) of the reference: a 2-letter deletion.
|
||||
var reference = "TTGATTACAGGTCC"
|
||||
var read = "GATCAGGT"
|
||||
print("read", read, "against reference", reference)
|
||||
|
||||
var affine = Scoring(
|
||||
"affine gaps: opening a gap costs 5, each further letter 1",
|
||||
3,
|
||||
-2,
|
||||
-5,
|
||||
-1,
|
||||
)
|
||||
var linear = Scoring("linear gaps: every gap letter costs 2", 3, -2, -2, -2)
|
||||
|
||||
print("1. align with affine gaps")
|
||||
var first = align(reference, read, affine)
|
||||
report(affine.name, first)
|
||||
|
||||
print("2. align the same read with linear gaps")
|
||||
var second = align(reference, read, linear)
|
||||
report(linear.name, second)
|
||||
|
||||
if first.top == second.top and first.bottom == second.bottom:
|
||||
print("the two models agree")
|
||||
else:
|
||||
print(
|
||||
"the models disagree: one 2-letter deletion, or two 1-letter"
|
||||
" deletions?"
|
||||
)
|
||||
@@ -0,0 +1,30 @@
|
||||
# origin: Programming+ exercise edit-distance
|
||||
# status: unverified; `corpus.py verify` checks it on the pinned compiler
|
||||
# probe: the fewest single-letter edits (substitute, insert, delete) that turn a into b
|
||||
# expect-stdout: 3
|
||||
def edit_distance(a: String, b: String) -> Int:
|
||||
var n = a.count_codepoints()
|
||||
var m = b.count_codepoints()
|
||||
var d = List[List[Int]]()
|
||||
for _ in range(n + 1):
|
||||
var row = List[Int]()
|
||||
for _ in range(m + 1):
|
||||
row.append(0)
|
||||
d.append(row^)
|
||||
for i in range(n + 1):
|
||||
d[i][0] = i
|
||||
for j in range(m + 1):
|
||||
d[0][j] = j
|
||||
for i in range(1, n + 1):
|
||||
for j in range(1, m + 1):
|
||||
var cost = 1
|
||||
if a[codepoint=i - 1] == b[codepoint=j - 1]:
|
||||
cost = 0
|
||||
d[i][j] = min(
|
||||
d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost
|
||||
)
|
||||
return d[n][m]
|
||||
|
||||
|
||||
def main():
|
||||
print(edit_distance("GATTACA", "GCATGCA"))
|
||||
@@ -0,0 +1,17 @@
|
||||
# origin: Programming+ exercise first-unknown
|
||||
# status: unverified; `corpus.py verify` checks it on the pinned compiler
|
||||
# probe: the position of the first N in t, or the length of t when there is none
|
||||
# expect-stdout: 3
|
||||
def first_unknown(t: String) -> Int:
|
||||
# ensures: 0 <= result <= |t|
|
||||
# ensures: forall k in [0, result) : t[k] != 'N'
|
||||
var n: Int = t.count_codepoints()
|
||||
var i: Int = 0
|
||||
while i < n and String(t[codepoint=i]) != "N":
|
||||
# invariant: forall k in [0, i) : t[k] != 'N'
|
||||
i += 1
|
||||
return i
|
||||
|
||||
|
||||
def main():
|
||||
print(first_unknown("GATNACA"))
|
||||
@@ -0,0 +1,16 @@
|
||||
# origin: Programming+ exercise five-bugs
|
||||
# status: unverified; `corpus.py verify` checks it on the pinned compiler
|
||||
# probe: count the G and C bases in t; this version has mistakes to find by stepping
|
||||
# expect-stdout: 1
|
||||
def count_gc(t: String) -> Int:
|
||||
var total: Int = 0
|
||||
for i in range(t.count_codepoints() - 1):
|
||||
total = 0
|
||||
var b: String = String(t[codepoint=i])
|
||||
if b == "G" or b == "C":
|
||||
total += 1
|
||||
return total
|
||||
|
||||
|
||||
def main():
|
||||
print(count_gc("GATTACA"))
|
||||
@@ -0,0 +1,16 @@
|
||||
# origin: Programming+ exercise gc-fraction
|
||||
# status: unverified; `corpus.py verify` checks it on the pinned compiler
|
||||
# probe: the fraction of t that is G or C, as a Float64
|
||||
# expect-stdout: 0.2857142857142857
|
||||
def gc_fraction(t: String) -> Float64:
|
||||
# requires: |t| > 0
|
||||
var gc = 0
|
||||
for cp in t.codepoint_slices():
|
||||
var b = String(cp)
|
||||
if b == "G" or b == "C":
|
||||
gc += 1
|
||||
return Float64(gc) / Float64(t.count_codepoints())
|
||||
|
||||
|
||||
def main():
|
||||
print(gc_fraction("GATTACA"))
|
||||
@@ -0,0 +1,17 @@
|
||||
# origin: Programming+ exercise hamming
|
||||
# status: unverified; `corpus.py verify` checks it on the pinned compiler
|
||||
# probe: how many positions differ between two reads of the same length
|
||||
# expect-stdout: 2
|
||||
def hamming(a: String, b: String) -> Int:
|
||||
# requires: |a| == |b|
|
||||
# ensures: result == |{ k in [0, |a|) : a[k] != b[k] }|
|
||||
var differences = 0
|
||||
for i in range(a.count_codepoints()):
|
||||
# invariant: differences == |{ k in [0, i) : a[k] != b[k] }|
|
||||
if a[codepoint=i] != b[codepoint=i]:
|
||||
differences += 1
|
||||
return differences
|
||||
|
||||
|
||||
def main():
|
||||
print(hamming("GATTACA", "GACTATA"))
|
||||
@@ -0,0 +1,21 @@
|
||||
# origin: Programming+ exercise kmers
|
||||
# status: unverified; `corpus.py verify` checks it on the pinned compiler
|
||||
# probe: how many times each word of k letters occurs in t
|
||||
# expect-stdout: {GAT: 2, ATT: 2, TTA: 1, TAC: 1, ACA: 1, CAG: 1, AGA: 1}
|
||||
def kmer_counts(t: String, k: Int) raises -> Dict[String, Int]:
|
||||
# requires: k >= 1
|
||||
var counts = Dict[String, Int]()
|
||||
var n = t.count_codepoints()
|
||||
for i in range(n - k + 1):
|
||||
var word = String("")
|
||||
for j in range(k):
|
||||
word += String(t[codepoint=i + j])
|
||||
if word in counts:
|
||||
counts[word] += 1
|
||||
else:
|
||||
counts[word] = 1
|
||||
return counts^
|
||||
|
||||
|
||||
def main() raises:
|
||||
print(kmer_counts("GATTACAGATT", 3))
|
||||
@@ -0,0 +1,21 @@
|
||||
# origin: Programming+ exercise match-off-by-one
|
||||
# status: unverified; `corpus.py verify` checks it on the pinned compiler
|
||||
# probe: one bound is wrong: the outer loop runs i past the last start a match could have
|
||||
# expect: fail
|
||||
def match_pattern(p: String, t: String) -> List[Int]:
|
||||
var n = t.count_codepoints()
|
||||
var m = p.count_codepoints()
|
||||
var answer = List[Int]()
|
||||
for i in range(n):
|
||||
var matched = True
|
||||
for j in range(m):
|
||||
if t[codepoint=i + j] != p[codepoint=j]:
|
||||
matched = False
|
||||
break
|
||||
if matched:
|
||||
answer.append(i)
|
||||
return answer^
|
||||
|
||||
|
||||
def main():
|
||||
print(match_pattern("ATT", "GATTACA"))
|
||||
@@ -0,0 +1,25 @@
|
||||
# origin: Programming+ exercise match-proved
|
||||
# status: unverified; `corpus.py verify` checks it on the pinned compiler
|
||||
# probe: match with its specification as comments
|
||||
# expect-stdout: [1]
|
||||
def match_pattern(p: String, t: String) -> List[Int]:
|
||||
# requires: |p| >= 1
|
||||
# ensures: result == sorted({ s in [0, |t| - |p| + 1) : forall k in [0, |p|) : t[s + k] == p[k] })
|
||||
var n = t.count_codepoints()
|
||||
var m = p.count_codepoints()
|
||||
var answer = List[Int]()
|
||||
for i in range(n - m + 1):
|
||||
# invariant: answer == sorted({ s in [0, i) : forall k in [0, m) : t[s + k] == p[k] })
|
||||
var matched = True
|
||||
for j in range(m):
|
||||
# invariant: matched == (forall k in [0, j) : t[i + k] == p[k])
|
||||
if t[codepoint=i + j] != p[codepoint=j]:
|
||||
matched = False
|
||||
break
|
||||
if matched:
|
||||
answer.append(i)
|
||||
return answer^
|
||||
|
||||
|
||||
def main():
|
||||
print(match_pattern("ATT", "GATTACA"))
|
||||
@@ -0,0 +1,21 @@
|
||||
# origin: Programming+ exercise match-reset
|
||||
# status: unverified; `corpus.py verify` checks it on the pinned compiler
|
||||
# probe: matched is reset in the wrong place, so one mismatch poisons every later window
|
||||
# expect-stdout: []
|
||||
def match_pattern(p: String, t: String) -> List[Int]:
|
||||
var n = t.count_codepoints()
|
||||
var m = p.count_codepoints()
|
||||
var answer = List[Int]()
|
||||
var matched = True
|
||||
for i in range(n - m + 1):
|
||||
for j in range(m):
|
||||
if t[codepoint=i + j] != p[codepoint=j]:
|
||||
matched = False
|
||||
break
|
||||
if matched:
|
||||
answer.append(i)
|
||||
return answer^
|
||||
|
||||
|
||||
def main():
|
||||
print(match_pattern("GC", "GATGCA"))
|
||||
@@ -0,0 +1,21 @@
|
||||
# origin: Programming+ exercise match
|
||||
# status: unverified; `corpus.py verify` checks it on the pinned compiler
|
||||
# probe: every position where pattern p occurs exactly in text t
|
||||
# expect-stdout: [1]
|
||||
def match_pattern(p: String, t: String) -> List[Int]:
|
||||
var n = t.count_codepoints()
|
||||
var m = p.count_codepoints()
|
||||
var answer = List[Int]()
|
||||
for i in range(n - m + 1):
|
||||
var matched = True
|
||||
for j in range(m):
|
||||
if t[codepoint=i + j] != p[codepoint=j]:
|
||||
matched = False
|
||||
break
|
||||
if matched:
|
||||
answer.append(i)
|
||||
return answer^
|
||||
|
||||
|
||||
def main():
|
||||
print(match_pattern("ATT", "GATTACA"))
|
||||
@@ -0,0 +1,26 @@
|
||||
# origin: Programming+ exercise revcomp
|
||||
# status: unverified; `corpus.py verify` checks it on the pinned compiler
|
||||
# probe: the reverse complement of a strand: read backwards, each base swapped for its partner
|
||||
# expect-stdout: TGTAATC
|
||||
def reverse_complement(t: String) -> String:
|
||||
# ensures: |result| == |t|
|
||||
var answer = String("")
|
||||
var i = t.count_codepoints() - 1
|
||||
while i >= 0:
|
||||
var b = String(t[codepoint=i])
|
||||
var partner = String("N")
|
||||
if b == "A":
|
||||
partner = "T"
|
||||
elif b == "T":
|
||||
partner = "A"
|
||||
elif b == "G":
|
||||
partner = "C"
|
||||
elif b == "C":
|
||||
partner = "G"
|
||||
answer += partner
|
||||
i -= 1
|
||||
return answer^
|
||||
|
||||
|
||||
def main():
|
||||
print(reverse_complement("GATTACA"))
|
||||
@@ -0,0 +1,17 @@
|
||||
# origin: Programming+ exercise tally
|
||||
# status: unverified; `corpus.py verify` checks it on the pinned compiler
|
||||
# probe: how many times each base appears in t, in a Dict
|
||||
# expect-stdout: {G: 1, A: 3, T: 2, C: 1}
|
||||
def tally_bases(t: String) raises -> Dict[String, Int]:
|
||||
var tally = Dict[String, Int]()
|
||||
for i in range(t.count_codepoints()):
|
||||
var b = String(t[codepoint=i])
|
||||
if b in tally:
|
||||
tally[b] += 1
|
||||
else:
|
||||
tally[b] = 1
|
||||
return tally^
|
||||
|
||||
|
||||
def main() raises:
|
||||
print(tally_bases("GATTACA"))
|
||||
@@ -0,0 +1,26 @@
|
||||
# origin: Programming+ exercise translate-codons
|
||||
# status: unverified; `corpus.py verify` checks it on the pinned compiler
|
||||
# probe: translate a reading frame into amino acids, stopping at the first stop codon
|
||||
# expect-stdout: MAK
|
||||
def translate(t: String) raises -> String:
|
||||
var stops = ["TAA", "TAG", "TGA"]
|
||||
var table = {"ATG": "M", "GCC": "A", "AAA": "K", "TTT": "F", "GGC": "G"}
|
||||
var protein = String("")
|
||||
var n = t.count_codepoints()
|
||||
var i = 0
|
||||
while i + 3 <= n:
|
||||
var codon = String("")
|
||||
for j in range(3):
|
||||
codon += String(t[codepoint=i + j])
|
||||
if codon in stops:
|
||||
break
|
||||
elif codon in table:
|
||||
protein += table[codon]
|
||||
else:
|
||||
protein += "X"
|
||||
i += 3
|
||||
return protein^
|
||||
|
||||
|
||||
def main() raises:
|
||||
print(translate("ATGGCCAAATAGTTT"))
|
||||
Reference in new issue
Block a user