range result in Tuple! and how to convert into assocArray by sort?
Ali Çehreli
acehreli at yahoo.com
Tue May 10 04:21:04 UTC 2022
On 5/9/22 20:38, rikki cattermole wrote:
> this is a super expensive
> method for doing something pretty simple.
Yes! :)
Assuming the data is indeed validated in some way, the following should
be even faster. It validates the data after the fact:
import std.stdio;
import std.range;
import std.exception;
import std.algorithm;
import std.format;
const ulong[] alphabet = [ 'A', 'C', 'G', 'T' ];
void main() {
string s = "ACGTACGT";
auto counts = new ulong[char.max];
foreach(char c; s) {
counts[c]++;
}
validateCounts(counts);
writeln(counts.indexed(alphabet));
}
void validateCounts(ulong[] counts) {
// The other elements should all be zero.
enforce(counts
.enumerate
.filter!(t => !alphabet.canFind(t.index))
.map!(t => t.value)
.sum == 0,
format!"There were illegal letters in the data: %s"(counts));
}
Ali
More information about the Digitalmars-d-learn
mailing list