Baseline: tree as shared
This commit is contained in:
1679 files changed
+1024562
No files matched your search
@@ -0,0 +1,68 @@
|
||||
{
|
||||
"count-base": [
|
||||
"6-8 declare-then-assign",
|
||||
"7-9 declare-then-assign",
|
||||
"11-13 codepoint-loop",
|
||||
"15 augmented-assign"
|
||||
],
|
||||
"count-base-proved": [
|
||||
"6-8 declare-then-assign",
|
||||
"7-9 declare-then-assign",
|
||||
"11-12 codepoint-loop",
|
||||
"13 augmented-assign"
|
||||
],
|
||||
"match": [
|
||||
"6-10 declare-then-assign",
|
||||
"7-11 declare-then-assign"
|
||||
],
|
||||
"match-proved": [
|
||||
"8-12 declare-then-assign",
|
||||
"9-13 declare-then-assign"
|
||||
],
|
||||
"match-off-by-one": [
|
||||
"6-10 declare-then-assign",
|
||||
"7-11 declare-then-assign"
|
||||
],
|
||||
"match-reset": [
|
||||
"5-9 declare-then-assign",
|
||||
"6-10 declare-then-assign",
|
||||
"7-11 declare-then-assign"
|
||||
],
|
||||
"first-unknown": [
|
||||
"7-8 declare-then-assign",
|
||||
"11 augmented-assign"
|
||||
],
|
||||
"gc-fraction": [
|
||||
"6-7 declare-then-assign",
|
||||
"8-9 codepoint-loop",
|
||||
"11 augmented-assign"
|
||||
],
|
||||
"hamming": [
|
||||
"7-8 declare-then-assign",
|
||||
"12 augmented-assign"
|
||||
],
|
||||
"best-position": [
|
||||
"8-9 declare-then-assign"
|
||||
],
|
||||
"tally": [
|
||||
"7-8 codepoint-loop"
|
||||
],
|
||||
"kmers": [],
|
||||
"revcomp": [
|
||||
"6-8 declare-then-assign",
|
||||
"20 augmented-assign"
|
||||
],
|
||||
"binary-search": [
|
||||
"6-9 declare-then-assign",
|
||||
"7-10 declare-then-assign"
|
||||
],
|
||||
"edit-distance": [
|
||||
"5-7 declare-then-assign",
|
||||
"6-8 declare-then-assign"
|
||||
],
|
||||
"translate-codons": [
|
||||
"11-18 declare-then-assign",
|
||||
"27 augmented-assign",
|
||||
"29 augmented-assign"
|
||||
]
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
|
||||
|
||||
# The position of the highest quality score; the first one, if several tie.
|
||||
def best_position(q: List[Int]) -> Int:
|
||||
# REQUIRE LENGTH(Q) >= 1
|
||||
# ENSURE 0 <= RESULT AND RESULT < LENGTH(Q)
|
||||
# ENSURE FORALL K IN [0, LENGTH(Q)) : Q[K] <= Q[RESULT]
|
||||
var best: Int
|
||||
best = 0
|
||||
# INVARIANT 0 <= Best AND Best < I
|
||||
# INVARIANT FORALL K IN [0, I) : Q[K] <= Q[Best]
|
||||
for i in range(1, len(q)):
|
||||
if q[i] > q[best]:
|
||||
best = i
|
||||
return best
|
||||
|
||||
|
||||
def main():
|
||||
print(best_position(List[Int](12, 30, 25, 30, 8)))
|
||||
@@ -0,0 +1,25 @@
|
||||
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
|
||||
|
||||
# Is X in the sorted list L? Halve the range that could hold it each time.
|
||||
def contains_sorted(l: List[Int], x: Int) -> Bool:
|
||||
# REQUIRE FORALL K IN [0, LENGTH(L) - 1) : L[K] <= L[K + 1]
|
||||
var lo: Int
|
||||
var hi: Int
|
||||
var mid: Int
|
||||
lo = 0
|
||||
hi = len(l)
|
||||
# INVARIANT 0 <= Lo AND Lo <= Hi AND Hi <= LENGTH(L)
|
||||
# DECREASES Hi - Lo
|
||||
while lo < hi:
|
||||
mid = (lo + hi) // 2
|
||||
if l[mid] == x:
|
||||
return True
|
||||
elif l[mid] < x:
|
||||
lo = mid + 1
|
||||
else:
|
||||
hi = mid
|
||||
return False
|
||||
|
||||
|
||||
def main():
|
||||
print(contains_sorted(List[Int](2, 5, 8, 13, 21), 13))
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
|
||||
|
||||
# How many positions of T hold the base B, with its specification.
|
||||
def count_base(b: String, t: String) -> Int:
|
||||
# ENSURE RESULT == LENGTH({ K IN [0, LENGTH(T)) : T[K] == B })
|
||||
var n: Int
|
||||
var total: Int
|
||||
n = t.count_codepoints()
|
||||
total = 0
|
||||
# INVARIANT Total == LENGTH({ K IN [0, I) : T[K] == B })
|
||||
for i in range(n):
|
||||
if String(t[codepoint=i]) == b:
|
||||
total = total + 1
|
||||
return total
|
||||
|
||||
|
||||
def main():
|
||||
print(count_base("A", "GATTACA"))
|
||||
@@ -0,0 +1,21 @@
|
||||
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
|
||||
|
||||
# How many positions of T hold the base B.
|
||||
def count_base(b: String, t: String) -> Int:
|
||||
# Set up
|
||||
var n: Int
|
||||
var total: Int
|
||||
n = t.count_codepoints()
|
||||
total = 0
|
||||
# Visit each position
|
||||
for i in range(n):
|
||||
# Test this position
|
||||
if String(t[codepoint=i]) == b:
|
||||
# Update the running total
|
||||
total = total + 1
|
||||
# Report
|
||||
return total
|
||||
|
||||
|
||||
def main():
|
||||
print(count_base("A", "GATTACA"))
|
||||
@@ -0,0 +1,31 @@
|
||||
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
|
||||
|
||||
# The fewest single-letter edits (substitute, insert, delete) that turn A into B.
|
||||
def edit_distance(a: String, b: String) -> Int:
|
||||
var n: Int
|
||||
var m: Int
|
||||
n = a.count_codepoints()
|
||||
m = b.count_codepoints()
|
||||
var d = List[List[Int]]()
|
||||
for _ in range(n + 1):
|
||||
var _row1 = List[Int]()
|
||||
for _ in range(m + 1):
|
||||
_row1.append(0)
|
||||
d.append(_row1^)
|
||||
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: Int
|
||||
if String(a[codepoint=i - 1]) == String(b[codepoint=j - 1]):
|
||||
cost = 0
|
||||
else:
|
||||
cost = 1
|
||||
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,16 @@
|
||||
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
|
||||
|
||||
# The position of the first N in T, or LENGTH(T) when there is none.
|
||||
def first_unknown(t: String) -> Int:
|
||||
# ENSURE 0 <= RESULT AND RESULT <= LENGTH(T)
|
||||
# ENSURE FORALL K IN [0, RESULT) : T[K] != 'N'
|
||||
var i: Int
|
||||
i = 0
|
||||
# INVARIANT FORALL K IN [0, I) : T[K] != 'N'
|
||||
while i < t.count_codepoints() and String(t[codepoint=i]) != "N":
|
||||
i = i + 1
|
||||
return i
|
||||
|
||||
|
||||
def main():
|
||||
print(first_unknown("GATNACA"))
|
||||
@@ -0,0 +1,16 @@
|
||||
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
|
||||
|
||||
# The fraction of T that is G or C.
|
||||
def gc_fraction(t: String) -> Float64:
|
||||
# REQUIRE LENGTH(T) > 0
|
||||
var gc: Int
|
||||
gc = 0
|
||||
for _b_pos1 in range(t.count_codepoints()):
|
||||
var b = String(t[codepoint=_b_pos1])
|
||||
if b == "G" or b == "C":
|
||||
gc = gc + 1
|
||||
return Float64(gc) / Float64(t.count_codepoints())
|
||||
|
||||
|
||||
def main():
|
||||
print(gc_fraction("GATTACA"))
|
||||
@@ -0,0 +1,17 @@
|
||||
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
|
||||
|
||||
# How many positions differ between two reads of the same length.
|
||||
def hamming(a: String, b: String) -> Int:
|
||||
# REQUIRE LENGTH(A) == LENGTH(B)
|
||||
# ENSURE RESULT == LENGTH({ K IN [0, LENGTH(A)) : A[K] != B[K] })
|
||||
var differences: Int
|
||||
differences = 0
|
||||
# INVARIANT Differences == LENGTH({ K IN [0, I) : A[K] != B[K] })
|
||||
for i in range(a.count_codepoints()):
|
||||
if String(a[codepoint=i]) != String(b[codepoint=i]):
|
||||
differences = differences + 1
|
||||
return differences
|
||||
|
||||
|
||||
def main():
|
||||
print(hamming("GATTACA", "GACTATA"))
|
||||
@@ -0,0 +1,21 @@
|
||||
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
|
||||
|
||||
# How many times each word of K letters occurs in T.
|
||||
def kmer_counts(t: String, k: Int) raises -> Dict[String, Int]:
|
||||
# REQUIRE K >= 1
|
||||
var counts = Dict[String, Int]()
|
||||
var word: String
|
||||
for i in range(t.count_codepoints() - k + 1):
|
||||
var _piece2 = String("")
|
||||
for _at1 in range(i, i + k):
|
||||
_piece2 += String(t[codepoint=_at1])
|
||||
word = _piece2
|
||||
if word in counts:
|
||||
counts[word] = counts[word] + 1
|
||||
else:
|
||||
counts[word] = 1
|
||||
return counts.copy()
|
||||
|
||||
|
||||
def main() raises:
|
||||
print(kmer_counts("GATTACAGATT", 3))
|
||||
@@ -0,0 +1,27 @@
|
||||
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
|
||||
|
||||
# One bound is wrong. Every position where pattern P occurs exactly in text T.
|
||||
def match_(p: String, t: String) -> List[Int]:
|
||||
# Set up
|
||||
var n: Int
|
||||
var m: Int
|
||||
var matched: Bool
|
||||
var answer = List[Int]()
|
||||
n = t.count_codepoints()
|
||||
m = p.count_codepoints()
|
||||
# Visit each legal start
|
||||
for i in range(n):
|
||||
# Test: does the pattern match here?
|
||||
matched = True
|
||||
for j in range(m):
|
||||
if String(t[codepoint=i + j]) != String(p[codepoint=j]):
|
||||
matched = False
|
||||
break
|
||||
# Update
|
||||
if matched:
|
||||
answer.append(i)
|
||||
return answer.copy()
|
||||
|
||||
|
||||
def main():
|
||||
print(match_("ATT", "GATTACA"))
|
||||
@@ -0,0 +1,28 @@
|
||||
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
|
||||
|
||||
# MATCH, with the specification from "Specifying the problem",
|
||||
# and the two invariants that prove it.
|
||||
def match_(p: String, t: String) -> List[Int]:
|
||||
# REQUIRE LENGTH(P) >= 1
|
||||
# ENSURE RESULT == SORTED({ S IN [0, LENGTH(T) - LENGTH(P) + 1) : FORALL K IN [0, LENGTH(P)) : T[S + K] == P[K] })
|
||||
var n: Int
|
||||
var m: Int
|
||||
var matched: Bool
|
||||
var answer = List[Int]()
|
||||
n = t.count_codepoints()
|
||||
m = p.count_codepoints()
|
||||
# INVARIANT Answer == SORTED({ S IN [0, I) : FORALL K IN [0, M) : T[S + K] == P[K] })
|
||||
for i in range(n - m + 1):
|
||||
matched = True
|
||||
# INVARIANT Matched == (FORALL K IN [0, J) : T[I + K] == P[K])
|
||||
for j in range(m):
|
||||
if String(t[codepoint=i + j]) != String(p[codepoint=j]):
|
||||
matched = False
|
||||
break
|
||||
if matched:
|
||||
answer.append(i)
|
||||
return answer.copy()
|
||||
|
||||
|
||||
def main():
|
||||
print(match_("ATT", "GATTACA"))
|
||||
@@ -0,0 +1,23 @@
|
||||
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
|
||||
|
||||
# Matched is reset in the wrong place.
|
||||
def match_(p: String, t: String) -> List[Int]:
|
||||
var n: Int
|
||||
var m: Int
|
||||
var matched: Bool
|
||||
var answer = List[Int]()
|
||||
n = t.count_codepoints()
|
||||
m = p.count_codepoints()
|
||||
matched = True
|
||||
for i in range(n - m + 1):
|
||||
for j in range(m):
|
||||
if String(t[codepoint=i + j]) != String(p[codepoint=j]):
|
||||
matched = False
|
||||
break
|
||||
if matched:
|
||||
answer.append(i)
|
||||
return answer.copy()
|
||||
|
||||
|
||||
def main():
|
||||
print(match_("GC", "GATGCA"))
|
||||
@@ -0,0 +1,27 @@
|
||||
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
|
||||
|
||||
# Every position where pattern P occurs exactly in text T.
|
||||
def match_(p: String, t: String) -> List[Int]:
|
||||
# Set up
|
||||
var n: Int
|
||||
var m: Int
|
||||
var matched: Bool
|
||||
var answer = List[Int]()
|
||||
n = t.count_codepoints()
|
||||
m = p.count_codepoints()
|
||||
# Visit each legal start
|
||||
for i in range(n - m + 1):
|
||||
# Test: does the pattern match here?
|
||||
matched = True
|
||||
for j in range(m):
|
||||
if String(t[codepoint=i + j]) != String(p[codepoint=j]):
|
||||
matched = False
|
||||
break
|
||||
# Update
|
||||
if matched:
|
||||
answer.append(i)
|
||||
return answer.copy()
|
||||
|
||||
|
||||
def main():
|
||||
print(match_("ATT", "GATTACA"))
|
||||
@@ -0,0 +1,25 @@
|
||||
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
|
||||
|
||||
# The reverse complement of a strand: read backwards, each base swapped for its partner.
|
||||
def reverse_complement(t: String) -> String:
|
||||
# ENSURE LENGTH(RESULT) == LENGTH(T)
|
||||
var answer: String
|
||||
var partner: String
|
||||
answer = ""
|
||||
for i in range(t.count_codepoints() - 1, -1, -1):
|
||||
if String(t[codepoint=i]) == "A":
|
||||
partner = "T"
|
||||
elif String(t[codepoint=i]) == "T":
|
||||
partner = "A"
|
||||
elif String(t[codepoint=i]) == "G":
|
||||
partner = "C"
|
||||
elif String(t[codepoint=i]) == "C":
|
||||
partner = "G"
|
||||
else:
|
||||
partner = "N"
|
||||
answer = answer + partner
|
||||
return answer
|
||||
|
||||
|
||||
def main():
|
||||
print(reverse_complement("GATTACA"))
|
||||
@@ -0,0 +1,17 @@
|
||||
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
|
||||
|
||||
# How many times each base appears in T.
|
||||
def tally_bases(t: String) raises -> Dict[String, Int]:
|
||||
var tally = Dict[String, Int]()
|
||||
var b: String
|
||||
for i in range(t.count_codepoints()):
|
||||
b = String(t[codepoint=i])
|
||||
if b in tally:
|
||||
tally[b] = tally[b] + 1
|
||||
else:
|
||||
tally[b] = 1 # first sighting of this base
|
||||
return tally.copy()
|
||||
|
||||
|
||||
def main() raises:
|
||||
print(tally_bases("GATTACA"))
|
||||
@@ -0,0 +1,34 @@
|
||||
# Generated by Programming+ from pseudo. Not reviewed for Mojo best practices.
|
||||
from std.collections import Set
|
||||
|
||||
# Translate a reading frame into amino acids, stopping at the first stop codon.
|
||||
def stops() -> Set[String]:
|
||||
return Set[String]("TAA", "TAG", "TGA")
|
||||
|
||||
|
||||
def translate(t: String) raises -> String:
|
||||
var table = Dict[String, String]()
|
||||
var protein: String
|
||||
var codon: String
|
||||
table["ATG"] = "M"
|
||||
table["GCC"] = "A"
|
||||
table["AAA"] = "K"
|
||||
table["TTT"] = "F"
|
||||
table["GGC"] = "G"
|
||||
protein = ""
|
||||
for i in range(0, t.count_codepoints() // 3 * 3, 3):
|
||||
var _piece2 = String("")
|
||||
for _at1 in range(i, i + 3):
|
||||
_piece2 += String(t[codepoint=_at1])
|
||||
codon = _piece2
|
||||
if codon in stops():
|
||||
break
|
||||
elif codon in table:
|
||||
protein = protein + table[codon]
|
||||
else:
|
||||
protein = protein + "X"
|
||||
return protein
|
||||
|
||||
|
||||
def main() raises:
|
||||
print(translate("ATGGCCAAATAGTTT"))
|
||||
Reference in new issue
Block a user