commit a0cfe56809451cd6a0cbb9ec7fcf81ff73c29103
parent 187b3dde878ef3d80008069a2adbaf3b107e083d
Author: Walther Chen <walther.chen@gmail.com>
Date:   Wed, 23 Oct 2024 22:49:17 -0400

refactor, pull out frequency_table

Diffstat:
Mba1b.c3 | 22++++++++--------------
Mjustfile | 2+-
Autil.c3 | 18++++++++++++++++++
3 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/ba1b.c3 b/ba1b.c3 @@ -4,7 +4,7 @@ import std::io; import std::io::file; import std::collections; -def KmerCounts = HashMap(<String, int>); +import util; fn void! main(String[] args) { if (args.len != 2) { @@ -23,26 +23,19 @@ fn void! main(String[] args) { fn String[] frequent_words(String text, int k, Allocator alloc= allocator::heap()) { @pool() { - KmerCounts kmer_counts; - for (int i = 0; i <= text.len - k; i += 1) { - // TODO get_or_update? Annoying to get twice - kmer_counts.@get_or_set(text[i:k], 0); - if (try int* count = kmer_counts.get_ref(text[i:k])) { - *count += 1; - } - } + FrequencyTable freq_map = util::frequency_table(text, k, allocator::temp()); - int[] counts = kmer_counts.value_tlist(); + int[] counts = freq_map.value_tlist(); int max = 0; - foreach (count: counts) { + freq_map.@each(; String _k, int count) { if (count > max) max = count; - } + }; List(<String>) res; res.temp_init(); - kmer_counts.@each(; String key, int v) { + freq_map.@each(; String key, int v) { if (v == max) { - res.push(key); + res.push(key.copy(alloc)); } }; return res.to_new_array(alloc); @@ -50,5 +43,6 @@ fn String[] frequent_words(String text, int k, Allocator alloc= allocator::heap( } fn void test_frequent_words() @test { + io::printfn("%s", frequent_words("ACGTTGCATGTCGCATGATGCATGAGAGCT", 4)); assert(frequent_words("ACGTTGCATGTCGCATGATGCATGAGAGCT", 4) == {"CATG", "GCAT"}); } diff --git a/justfile b/justfile @@ -6,7 +6,7 @@ # redirects outputs to stderr @build problem *args="": - c3c compile -O3 {{problem}}.c3 1>&2 + c3c compile -O3 {{problem}}.c3 util.c3 1>&2 # using compile-run prints a bunch of logs run problem *args="": diff --git a/util.c3 b/util.c3 @@ -0,0 +1,18 @@ +module util; +import std::collections; + +def FrequencyTable = HashMap(<String, int>); + +// counts of kmers in a text. +fn FrequencyTable frequency_table(String text, int k, Allocator alloc = allocator::heap()) { + FrequencyTable kmer_counts; + kmer_counts.new_init(allocator: alloc); + for (int i = 0; i <= text.len - k; i += 1) { + // TODO get_or_update? Annoying to get twice + kmer_counts.@get_or_set(text[i:k].copy(alloc), 0); + if (try int* count = kmer_counts.get_ref(text[i:k])) { + *count += 1; + } + } + return kmer_counts; +}