22 lines
718 B
Mojo
22 lines
718 B
Mojo
# 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))
|